我有以下表结构:
表1
╔═════╦══════╦═════════════╦═════════════╗
║Col1 ║ Col2 ║ TableName ║ ColumnName ║
╠═════╬══════╬═════════════╬═════════════╣
║ 1 ║ abc ║ Table2 ║ column2 ║
║ 2 ║ xyz ║ ║ ║
║ 3 ║ pqr ║ Table1 ║ column1 ║
║ 4 ║ jbn ║ ║ ║
╚═════╩════════════════════╩═════════════╝
Table2 :
╔════════╦═════════╗
║Column1 ║ Column2 ║
╠════════╬═════════╣
║ 1 ║ A ║
║ 2 ║ B ║
║ 3 ║ C ║
║ 4 ║ D ║
╚════════╩═════════╝
Table3
╔════════╦═════════╗
║Column1 ║ Column2 ║
╠════════╬═════════╣
║ 1 ║ X ║
║ 2 ║ Y ║
║ 3 ║ Z ║
║ 4 ║ A ║
╚════════╩═════════╝
我想编写存储过程,它将根据Table1中列tableName和columnName的值从Table1中选择数据和从另一个表中选择数据。
我想要以下格式的数据:
╔═════╦═════╦════════╗
║Col1 ║ Col2║ List ║
╠═════╬═════╬════════╣
║ 1 ║ abc ║A,B,C,D ║
║ 2 ║ xyz ║ ║
║ 3 ║ pqr ║1,2,3,4 ║
║ 4 ║ jbn ║ ║
╚═════╩═════╩════════╝
答案 0 :(得分:1)
尝试临时表。 看看这里:http://www.sqlteam.com/article/temporary-tables
答案 1 :(得分:0)
你需要一个动态的sql来获得这样的选择。查看链接http://www.mssqltips.com/sqlservertip/1160/execute-dynamic-sql-commands-in-sql-server/
修改强>
以下代码可以解决问题。
我假设Col1
中的Table1
列是int类型。
我使用Temp表生成所需的表。您可以根据自己的方便将其替换为您的桌子。我还使用了#table1
,您可以将其替换为Table1
。
此外,这在性能方面可能不是很好,但这是我现在能想到的最好的。
declare @count int, @Query VARCHAR(5000), @counter int, @tableName VARCHAR(50), @ColumnName VARCHAR(50), @Col1 INT, @Col2 VARCHAR(50)
select @count = count(0) from #table1
SET @counter = 1
CREATE TABLE #table4
(
Col1 INT,
Col2 VARCHAR(50),
List VARCHAR(50)
)
WHILE @counter <= @count
BEGIN
SELECT @tableName = TableName, @ColumnName = columnName, @Col1 = Col1, @Col2 = Col2 FROM #Table1 WHERE Col1 = @counter
SELECT @Query = 'INSERT INTO #table4 (Col1 , Col2) VALUES (' + CONVERT(varchar(50),@Col1) + ', ''' + @Col2 + ''')'
EXEC (@Query)
SELECT @Query = ''
IF ISNULL(@tableName, '') != '' AND ISNULL(@ColumnName, '') != ''
BEGIN
SELECT @Query = 'UPDATE #table4 SET LIST = STUFF((SELECT '','' + CONVERT(VARCHAR(50), ' + @ColumnName + ') FROM ' + @tableName + ' FOR XML PATH('''')),1,1,'''') WHERE Col1 = ' + CONVERT(varchar(50),@Col1)
EXEC (@Query)
END
SET @counter = @counter + 1
END
SELECT * FROM #table4
希望这有帮助
答案 2 :(得分:0)
为了合并n个行而编辑的答案
CODE
drop table #temp1
declare @tabletemp as varchar (10)
declare @columntemp as varchar (10)
Declare @sqlTemp NVARCHAR(400)
declare @counter1 as int
declare @counter2 as int
select @counter2 = 1
select name,cname into #temp1 from test1
select @counter1 = COUNT(*) from #temp1
while (@counter2<= @counter1)
begin
SET @tabletemp = (SELECT MIN(name) FROM #temp1)
select @columntemp = (select min(cname) from #temp1 where #temp1.name = @tabletemp)
set @sqlTemp='select '+@columntemp +' from '+@tabletemp
exec(@sqlTemp)
delete from #temp1 where name = @tabletemp and cname = @columntemp
select @counter1 = COUNT(*) from #temp1
end
RESULT
select * from test1
name cname
table1 column1
test2 colname
table1 test
test2 name
select column1 from table1
column1
qwer
asdff
zxcvb
qwer
asdff
zxcvb
select colname from test2
colname
testing
wer
ewrth
sfsf
testing
wer
ewrth
sfsf
select test from table1
--got error message inavlid column nae 'test' as this column does not exist
select name from test2
name
table1
table1
table3
table2
table1
table1
table3
table2