Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
sg2255551
Constraint Violating Yak Guru
274 Posts |
Posted - 2010-09-08 : 08:03:27
|
| hiI want to list out all results from tblB based on the values which assigned to variable @x from tblA. How should i go about it?In tblA field1 i have values such as car, bus, trainDeclare @x as nvarchar(200)select @x = field1 from tblASelect * from tblBwhere field2 like '%' + @x + '%'I don't seems to get all the result set? thanks |
|
|
Ancy
Starting Member
23 Posts |
Posted - 2010-09-08 : 08:27:44
|
| To get the whole resultset, do not use a variable as only the last value from the query will be stored in the variable.You will need to loop through all the values in tblA and repeat the query for each value |
 |
|
|
sg2255551
Constraint Violating Yak Guru
274 Posts |
Posted - 2010-09-08 : 09:54:25
|
| hi I am still not able to get the result based on the while loop. Any suggestions? Thanksdeclare @x nvarchar(200)declare @RowNum intset @RowNum = 0 WHILE @RowNum < (select Count(field1) from tblA) BEGIN set @RowNum = @RowNum + 1 select @x = field1 from tblA Select * from dbo.tblB where field2 like '%' + @x + '%' END |
 |
|
|
sg2255551
Constraint Violating Yak Guru
274 Posts |
Posted - 2010-09-08 : 10:42:03
|
| hiI have managed to use cross appply to solve thisSelect * from tblBcross apply(select field1 from tblAwhere field2 like '%' + field1 + '%' ) as ans |
 |
|
|
nathans
Aged Yak Warrior
938 Posts |
Posted - 2010-09-10 : 01:12:41
|
Is LIKE necessary? Or are you simply trying to join the two tables?Perhaps?Select * from tblBwhere Field2 in (select Field1 from tblA)select *from tblB bjoin tblA a on b.Field2 = a.Field1 |
 |
|
|
sg2255551
Constraint Violating Yak Guru
274 Posts |
Posted - 2010-09-10 : 02:22:23
|
| Like is necessary because of some biz logic. Anyway, the cross apply does solve the problem. Thanks |
 |
|
|
|
|
|