Sql server sql server中字符串到查询的转换

Sql server sql server中字符串到查询的转换,sql-server,Sql Server,可能重复: 如何添加包含And子句的字符串。但是,当我们应用查询该字符串的字符串时,该字符串将被视为查询并满足所有和条件 我有一个类似这样的问题:- Declare @WhereQuery varchar(max) SET @WhereQuery='class=''BCA'' and RollNo=10 AND ID IN (SELECT ID FROM StudentMaster WHERE MARKS > 50)' SELECT * into #TempTable1 from

可能重复:

如何添加包含And子句的字符串。但是,当我们应用查询该字符串的字符串时,该字符串将被视为查询并满足所有和条件

我有一个类似这样的问题:-

Declare @WhereQuery varchar(max)

SET @WhereQuery='class=''BCA'' and RollNo=10 AND ID IN (SELECT ID FROM StudentMaster WHERE MARKS > 50)'

SELECT  * into #TempTable1
from StudentMaster 
where @WhereQuery
我也不想使用execute或exec函数来运行此查询

如果要运行动态SQL,则必须使用exec

如果不想使用EXEC,请编写非动态查询:

SELECT  * into #TempTable1
from StudentMaster 
where class='BCA' and RollNo=10 AND ID IN (SELECT ID FROM StudentMaster WHERE MARKS > 50)