在SQL查询分析器 动态SQL语句中where条件为变量时该如何写? 10
2个回答
展开全部
给你个参考吧:把要执行的语句写成sql字符串变量,执行变量即可
declare @sql varchar(1000)
declare @filter varchar(400)
declare @orderby varchar(50)
set @sql = ' select * from 你的表 '
set @filter = ' where 1=1 '
set @orderby = ' order by id desc '
if ....
set @filter = @filter + ' and 条件1 '
if ...
set @filter = @filter + ' and 条件2 '
-- 执行sql
exec( @sql + @filter + @orderby )
declare @sql varchar(1000)
declare @filter varchar(400)
declare @orderby varchar(50)
set @sql = ' select * from 你的表 '
set @filter = ' where 1=1 '
set @orderby = ' order by id desc '
if ....
set @filter = @filter + ' and 条件1 '
if ...
set @filter = @filter + ' and 条件2 '
-- 执行sql
exec( @sql + @filter + @orderby )
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询