[英]Use result of one query in another sql query
我有下面的表結構:
表格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 ║ ║
╚═════╩═════╩════════╝
嘗試臨時表。 在這里看: http : //www.sqlteam.com/article/temporary-tables
您將需要一個動態sql來獲得這樣的選擇。 查看鏈接http://www.mssqltips.com/sqlservertip/1160/execute-dynamic-sql-commands-in-sql-server/
編輯:
以下代碼可以解決問題。
我假設Table1
中的列Col1
是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
希望這可以幫助
已編輯答案以合並n行
碼
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
結果
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.