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] +''''
------解决方案--------------------