当前位置: 代码迷 >> Sql Server >> 动态字符串比较解决思路
  详细解决方案

动态字符串比较解决思路

热度:69   发布时间:2016-04-27 18:04:57.0
动态字符串比较
alter procedure test
@date nvarchar(20)
as 
declare @result nvarchar(3000)
set @result=N' select a.DocNo,a.CreatedOn'
set @[email protected]+N' from SM_SO a'
set @[email protected]+N' left join SM_SOLine a1 on a1.SO=a.ID'
-- set @[email protected]+N' where 1=1'
--if @date is not null
set @[email protected]+N' where a.CreatedOn > [email protected]
exec sp_executesql @result

其中 createdOn 是日期
exec test '2011-10-10'

exec test '2011-10-10'

怎么我的where 条件没有生效的呢

------解决方案--------------------
SQL code
set @[email protected]+N' where a.CreatedOn > [email protected]-->set @[email protected]+N' where a.CreatedOn > [email protected]+''''
------解决方案--------------------
如果这样:
set @[email protected]+N' where a.CreatedOn > [email protected]
拼出来的查询语句里这一段是:
where a.CreatedOn > 2011-10-10
即日期大于1991(数值),那你表中的两个日期都满足条件.
所以,拼出来的字符串必须为这样:
where a.CreatedOn > '2011-10-10'
才对.
拼接字符中,如果所得到的拼接字符串中含有引号,那就要用两个引号两处理.所以语句要改为:
set @[email protected]+N' where a.CreatedOn > [email protected] +''''
------解决方案--------------------
探讨

set @[email protected]+N' where a.CreatedOn > [email protected] + ''''
'''
''''
不明白能说下吗

set @[email protected]+N' where a.CreatedOn = [email protected]
为什么这样子就全部出来了呢
  相关解决方案