将三个表中的值合并为一个
Merging the values from three tables into one
我在竖线分隔符上拆分了三个字符串值,我需要将这些值合并为一个 table。
我已经设置了一个函数来拆分这些值并将它们放入三个单独的临时 tables。
我需要这些值合而为一 table。我该怎么做?
SET @QuestionData = '5|7|2'
SET @QuestionsDataCorrect = '0|1|0'
SET @QuestionsCount = '1|1|1'
DECLARE @QuestionsPrimaryTable Table (QuestionsId nvarchar(max))
INSERT INTO @QuestionsPrimaryTable
SELECT Item FROM SplitString(@QuestionData, '|')
DECLARE @QuestionsCorrectTemp Table (CorrectId nvarchar(max))
INSERT INTO @QuestionsCorrectTemp
SELECT Item FROM SplitString(@QuestionsDataCorrect, '|')
DECLARE @QuestionsCountTemp Table (CountId nvarchar(max))
INSERT INTO @QuestionsCountTemp
SELECT Item FROM SplitString(@QuestionsCount, '|')
select * from @QuestionsPrimaryTable
select * from @QuestionsCorrectTemp
select * from @QuestionsCountTemp
我的最终目标是拥有这样的价值观
5 0 1
7 1 1
2 0 1
您可以向临时 table 添加标识列,然后使用此列上的联接创建新的 table。
首先添加标识列:
DECLARE @QuestionsPrimaryTable Table (Id INT IDENTITY(1,1), QuestionsId nvarchar(max))
INSERT INTO @QuestionsPrimaryTable
SELECT Item FROM SplitString(@QuestionData, '|')
DECLARE @QuestionsCorrectTemp Table (Id INT IDENTITY(1,1), CorrectId nvarchar(max))
INSERT INTO @QuestionsCorrectTemp
SELECT Item FROM SplitString(@QuestionsDataCorrect, '|')
DECLARE @QuestionsCountTemp Table (Id INT IDENTITY(1,1), CountId nvarchar(max))
INSERT INTO @QuestionsCountTemp
SELECT Item FROM SplitString(@QuestionsCount, '|')
THen 声明一个新的 table 并插入来自其他三个 table 的所有值,然后通过 ID 加入:
DECLARE @QuestionsAll Table (QuestionsId nvarchar(max), CorrectId nvarchar(max), CountId nvarchar(max))
INSERT INTO @QuestionsAll (QuestionsId, CorrectId, CountId)
SELECT prim.QuestionsId, corr.CorrectId, cnt.CountId
FROM @QuestionsPrimaryTable prim
INNER JOIN @QuestionsCorrectTemp corr ON prim.Id = corr.Id
INNER JOIN @QuestionsCountTemp cnt ON prim.Id = count.Id
您需要在结果集中包含项目编号,这样您就可以使用它加入您的数据。所以,你的函数也需要 return 项目位置,然后你可以做这样的事情:
SET @QuestionData = '5|7|2'
SET @QuestionsDataCorrect = '0|1|0'
SET @QuestionsCount = '1|1|1'
DECLARE @QuestionsPrimaryTable Table (QuestionsId nvarchar(max), pos int)
INSERT INTO @QuestionsPrimaryTable
SELECT Item, ItemPos FROM SplitString(@QuestionData, '|')
DECLARE @QuestionsCorrectTemp Table (CorrectId nvarchar(max), pos int)
INSERT INTO @QuestionsCorrectTemp
SELECT Item, ItemPos FROM SplitString(@QuestionsDataCorrect, '|')
DECLARE @QuestionsCountTemp Table (CountId nvarchar(max), pos int)
INSERT INTO @QuestionsCountTemp
SELECT Item, ItemPos FROM SplitString(@QuestionsCount, '|')
select * from
@QuestionsPrimaryTable P
join @QuestionsCorrectTemp C on P.pos = C.pos
join @QuestionsCountTemp CO on P.pos = CO.pos
这假设所有这 3 个列表的项目数始终相等。
我在竖线分隔符上拆分了三个字符串值,我需要将这些值合并为一个 table。
我已经设置了一个函数来拆分这些值并将它们放入三个单独的临时 tables。
我需要这些值合而为一 table。我该怎么做?
SET @QuestionData = '5|7|2'
SET @QuestionsDataCorrect = '0|1|0'
SET @QuestionsCount = '1|1|1'
DECLARE @QuestionsPrimaryTable Table (QuestionsId nvarchar(max))
INSERT INTO @QuestionsPrimaryTable
SELECT Item FROM SplitString(@QuestionData, '|')
DECLARE @QuestionsCorrectTemp Table (CorrectId nvarchar(max))
INSERT INTO @QuestionsCorrectTemp
SELECT Item FROM SplitString(@QuestionsDataCorrect, '|')
DECLARE @QuestionsCountTemp Table (CountId nvarchar(max))
INSERT INTO @QuestionsCountTemp
SELECT Item FROM SplitString(@QuestionsCount, '|')
select * from @QuestionsPrimaryTable
select * from @QuestionsCorrectTemp
select * from @QuestionsCountTemp
我的最终目标是拥有这样的价值观
5 0 1
7 1 1
2 0 1
您可以向临时 table 添加标识列,然后使用此列上的联接创建新的 table。
首先添加标识列:
DECLARE @QuestionsPrimaryTable Table (Id INT IDENTITY(1,1), QuestionsId nvarchar(max))
INSERT INTO @QuestionsPrimaryTable
SELECT Item FROM SplitString(@QuestionData, '|')
DECLARE @QuestionsCorrectTemp Table (Id INT IDENTITY(1,1), CorrectId nvarchar(max))
INSERT INTO @QuestionsCorrectTemp
SELECT Item FROM SplitString(@QuestionsDataCorrect, '|')
DECLARE @QuestionsCountTemp Table (Id INT IDENTITY(1,1), CountId nvarchar(max))
INSERT INTO @QuestionsCountTemp
SELECT Item FROM SplitString(@QuestionsCount, '|')
THen 声明一个新的 table 并插入来自其他三个 table 的所有值,然后通过 ID 加入:
DECLARE @QuestionsAll Table (QuestionsId nvarchar(max), CorrectId nvarchar(max), CountId nvarchar(max))
INSERT INTO @QuestionsAll (QuestionsId, CorrectId, CountId)
SELECT prim.QuestionsId, corr.CorrectId, cnt.CountId
FROM @QuestionsPrimaryTable prim
INNER JOIN @QuestionsCorrectTemp corr ON prim.Id = corr.Id
INNER JOIN @QuestionsCountTemp cnt ON prim.Id = count.Id
您需要在结果集中包含项目编号,这样您就可以使用它加入您的数据。所以,你的函数也需要 return 项目位置,然后你可以做这样的事情:
SET @QuestionData = '5|7|2'
SET @QuestionsDataCorrect = '0|1|0'
SET @QuestionsCount = '1|1|1'
DECLARE @QuestionsPrimaryTable Table (QuestionsId nvarchar(max), pos int)
INSERT INTO @QuestionsPrimaryTable
SELECT Item, ItemPos FROM SplitString(@QuestionData, '|')
DECLARE @QuestionsCorrectTemp Table (CorrectId nvarchar(max), pos int)
INSERT INTO @QuestionsCorrectTemp
SELECT Item, ItemPos FROM SplitString(@QuestionsDataCorrect, '|')
DECLARE @QuestionsCountTemp Table (CountId nvarchar(max), pos int)
INSERT INTO @QuestionsCountTemp
SELECT Item, ItemPos FROM SplitString(@QuestionsCount, '|')
select * from
@QuestionsPrimaryTable P
join @QuestionsCorrectTemp C on P.pos = C.pos
join @QuestionsCountTemp CO on P.pos = CO.pos
这假设所有这 3 个列表的项目数始终相等。