i have data @P = (000000000111,000000000222) from table_B
this code can exec complete
select * from table_A where id in (000000000111,000000000222)
but this code can not exec complete
declare @P nvarchar(100)
set @P = (select id from table_B)
select @P
select * from merchant where id in (@P)
how to this code can exec complete
in
is a set operation, it works with sets, not with strings are you are assuming.
There are multiple ways to select rows from one table where the id exists in another, for example
select * from merchant m
where exists (select * from table_B b where b.id=m.id)
or in line with your thinking
select * from merchant
where id in (select id from table_B)
Here, the in
operates on a set not a string.
First, you don't need a parameter:
select *
from merchant
where id in (select id from table_b);
I would suggest exists
instead, but that is a nuance.
If you did want to store the values, you would use a table:
declare @p table (id nvarchar(100));
insert into @p (id)
select id
from table_b;
select *
from merchant m
where exists (select 1 from @p p where p.id = m.id);
Of course, this is just a more verbose version of the first query.
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.