i have a small doubt, this is my query
declare @table table(id int, name varchar(100))
insert into @table values (1,'a')
insert into @table values (2,'b')
insert into @table values (3,'c')
select * from @table where id in (1,2,5)
as you can see, in the above query i hard coded 1,2,5 in the select statement. but now i want to read them from another table, so i tried as:
declare @table2 table(id int)
insert into @table2 values (1)
insert into @table2 values (2)
insert into @table2 values (5)
select t1.* from @table t1 join @table2 t2 on t1.id=t2.id
i used join here. for both the queries im getting the same result. is this the proper way. i mean instead of hard coding, im taking the values from a table.
idcolumn in@table2was unique. Otherwise, the join operation would generate duplicate rows and you would want to use one of the alternatives suggested in Guffa's answer. – Cheran Shunmugavel Oct 21 '12 at 7:14