嗨,伙计们,在 SQL 中是这样的。
declare a as varchar(20)
set a = 'ID = 34 and ID = 22'
select * from something where ID = 1 and a
嗨,伙计们,在 SQL 中是这样的。
declare a as varchar(20)
set a = 'ID = 34 and ID = 22'
select * from something where ID = 1 and a
Yes, you can use use EXEC
declare @a as varchar(20)
set @a = 'ID = 34 and ID = 22'
EXEC ('select * from something where ID = 1 and ' + @a)
Warning: this type of concatenating SQL queries is a security risk because of SQL injection.
您可以使用该命令exec
- 将字符串连接到一个,然后运行该字符串的 SQL 查询。
因此,在您的情况下,它将是:
declare @a varchar(20)
set @a = 'ID = 34 and ID = 22'
exec ('select * from something where ID = 1 and ' + @a)
请注意 SQL 注入。
将它们放入表值(甚至是临时表)并加入它们。你甚至可以使用索引。(尽管有一些值,索引可能不会有太大帮助!)
避免了 sql 注入的所有可能的安全并发症,并且通常更好。
DECLARE @ids TABLE (ID INT PRIMARY KEY)
INSERT @ids VALUES (1),(2)
SELECT s.*
FROM
someTable AS s
JOIN @ids AS i ON i.ID = s.ID
查看用户定义的表类型以获得将列表传递给存储过程的更好方法。
另请查看此页面以获取此类问题的最终答案:http: //www.sommarskog.se/arrays-in-sql.html