存储过程中日期参数问题,正解即结贴

tiantian1980 2013-12-15 10:22:25
有这样的一个存储过程

execute dbo.myPro 'status=1'

create procedure dbo.myPro
declare @strWhere varchar(1500),
declare @sql varchar(1500)
set @sql='select * from T_company where '+@strWhere
exec(@strSql)

@strWherep这个参数有时会带日期
比如
status=1 and ceateTime>'2013-11-01 11:09:08.107' and create<''2013-11-05 11:09:08.107''
有时这个参数就是只有
status=1
怎么处理存储过程有时带的日期参数的情况。正解即结贴
...全文
96 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
tiantian1980 2013-12-15
  • 打赏
  • 举报
回复
谢谢yupeigu解贴
LongRui888 2013-12-15
  • 打赏
  • 举报
回复

create  procedure dbo.myPro
@strWhere varchar(1500)
as

declare @sql varchar(1500)

set @sql='select * from T_company where 1 = 1 and '+@strWhere

exec(@strSql)
--print @sql
go

--只有一个条件
execute  dbo.myPro 'status=1'


--如果里面有字符或者日期的,那么单引号要写2次
execute  dbo.myPro 'status=1 and ceateTime>''2013-11-01 11:09:08.107'' and create<''2013-11-05 11:09:08.107'''

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧