我如何将此语句更改为动态SQL?

时间:2021-08-08 12:03:48

I trying to change this to be dynamic but I stuck at the set of data..
Example, the statement

我试图将其更改为动态,但我坚持使用数据集。例如,声明

SELECT * FROM A
WHERE id IN (1,2)

SELECT * FROM A WHERE id IN(1,2)

and also 1,2 come from
SELECT id FROM B
WHERE type='%xxx%'

还有1,2来自SELECT id FROM B WHERE type ='%xxx%'

Statement above can return many number
I try to declare @id but I have no idea

上面的声明可以返回很多数字我尝试声明@id但我不知道

So, Have any idea?
Thank you for suggestion :)

那么,有什么想法吗?谢谢你的建议:)

2 个解决方案

#1


0  

SELECT * FROM A
WHERE id IN (
     SELECT id FROM B
     WHERE type='%xxx%')

This is called a subquery.

这称为子查询。

EDIT: When using a subquery is not an option and you want to use a variable, you can declare a temporary table and join table A with that table.

编辑:当使用子查询不是一个选项并且您想要使用变量时,您可以声明一个临时表并将表A与该表连接。

DECLARE @C table (id int)

INSERT @C (id) 
SELECT id FROM B
WHERE type='%xxx%'

SELECT A.* 
FROM A INNER JOIN @C c ON A.id = c.id

#2


0  

SELECT A.* FROM A 
INNER JOIN B
WHERE B.type='%xxx%'
AND A.ID = B.ID

Perhaps you can just use inner join, it should return you the result set that you looking at

也许你可以只使用内连接,它应该返回你看到的结果集

#1


0  

SELECT * FROM A
WHERE id IN (
     SELECT id FROM B
     WHERE type='%xxx%')

This is called a subquery.

这称为子查询。

EDIT: When using a subquery is not an option and you want to use a variable, you can declare a temporary table and join table A with that table.

编辑:当使用子查询不是一个选项并且您想要使用变量时,您可以声明一个临时表并将表A与该表连接。

DECLARE @C table (id int)

INSERT @C (id) 
SELECT id FROM B
WHERE type='%xxx%'

SELECT A.* 
FROM A INNER JOIN @C c ON A.id = c.id

#2


0  

SELECT A.* FROM A 
INNER JOIN B
WHERE B.type='%xxx%'
AND A.ID = B.ID

Perhaps you can just use inner join, it should return you the result set that you looking at

也许你可以只使用内连接,它应该返回你看到的结果集