I have below table structure :
Table1
╔═════╦══════╦═════════════╦═════════════╗
║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 ║
╚════════╩═════════╝
I want to write stored procedure which will select data from Table1 and data from another table depending upon value of column tableName and columnName in Table1.
I want data in following format:
╔═════╦═════╦════════╗
║Col1 ║ Col2║ List ║
╠═════╬═════╬════════╣
║ 1 ║ abc ║A,B,C,D ║
║ 2 ║ xyz ║ ║
║ 3 ║ pqr ║1,2,3,4 ║
║ 4 ║ jbn ║ ║
╚═════╩═════╩════════╝
Try temporary table . look at here : http://www.sqlteam.com/article/temporary-tables
You will need a dynamic sql to get such a select. Check out the link http://www.mssqltips.com/sqlservertip/1160/execute-dynamic-sql-commands-in-sql-server/
EDIT:
The following code should do the trick.
I have assumed that the column Col1
in Table1
is of type int.
I have used Temp table to generate the required table. You can replace it will your table as per your convenience. Also I have used #table1
which you can replace with your Table1
.
Also this might not be very good in terms of performance but this is the best I could come up with right now.
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
Hope this helps
Answer edited for incorporating n no of rows
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.