SQL 'while' to read a table data to a variable
问题 How could I write a while loop to populate data from a table to a variable to be used in a seperate query ? EX: select ITEM from Table1 ITEM A0001 B0001 C0003 D0005 E0032 If I can get all that results here under column ITEM to @var1 and then use it in a seperate query like: select * from Table2 where @var1 = Item_table2 <-- a random column in table2 This query should match @var1 from A0001 to E0032 to the Item_table2 column in Table2 . 回答1: You don't need a loop. select * from Table2 where