如何添加包含 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 函数来运行此查询。
我将添加上面提到的查询字符串,但这将无法正常工作。
我在 where 子句之后添加的变量被视为字符串,但我希望该字符串被视为查询。请帮忙。我也不想使用execute 或exec 函数来运行此查询。
下面的方法效果很好。但要格外小心,因为如果用户提供输入,它很容易受到 SQL 注入的影响。
create table #TempTable1 (.....)
Declare @selectQuery varchar(max)
set @selectQuery = 'SELECT * into #TempTable1 from StudentMaster '
Declare @WhereQuery varchar(max)
SET @WhereQuery='where class=''BCA'' and RollNo=10 AND ID IN (SELECT ID FROM StudentMaster WHERE MARKS > 50)'
exec (@selectQuery + @WhereQuery)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)