当前位置: 代码迷 >> Sql Server >> 日期在存储过程中合成查询语句的有关问题
  详细解决方案

日期在存储过程中合成查询语句的有关问题

热度:21   发布时间:2016-04-27 18:55:02.0
日期在存储过程中合成查询语句的问题
[email protected]@EDate传入后在Set语句中应该如何写才能不出错

ALTER   proc   Get_adjscx
(
    @bt   varchar(4),
    @ddid   varchar(30),
    @zdid   varchar(30),
    @Bdate   varchar(10),
    @Edate   varchar(10)
)
as
    DECLARE   @SqlStr   varchar(200)
    Set   @SqlStr= 'select   *   from   bl_idx   where   BDate> = '[email protected]+ '   and   Bdate <= '[email protected]
    select   @SqlStr

    if   @bt <> ' '
        Set   @[email protected]+ '   and   [email protected] '
    if   @ddid <> ' '
        Set   @[email protected]+ '   and   [email protected] '
    if   @zdid <> ' '
        Set   @[email protected]+ '   and   [email protected] '
    exec(@SqlStr)
go



------解决方案--------------------
ALTER proc Get_adjscx
(
@bt varchar(4),
@ddid varchar(30),
@zdid varchar(30),
@Bdate varchar(10),
@Edate varchar(10)
)
as
DECLARE @SqlStr varchar(200)
Set @SqlStr= 'select * from bl_idx where BDate> = ' ' '[email protected]+ ' ' ' and Bdate <= ' ' '[email protected]+ ' ' ' '
select @SqlStr

if @bt <> ' '
Set @[email protected]+ ' and bt= '[email protected]
if @ddid <> ' '
Set @[email protected]+ ' and ddid= '[email protected]
if @zdid <> ' '
Set @[email protected]+ ' and zdid= '[email protected]
exec(@SqlStr)
go
------解决方案--------------------
规定:日期要用单引号括起来,当字符串中有单引号时,必须将单引号换成二个单引号.
Set @SqlStr= 'select * from bl_idx where BDate> = ' ' '[email protected]+ ' ' ' and Bdate <= ' ' '[email protected] + ' ' ' '
  相关解决方案