CREATE PROCEDURE ShowDZData
@ProsureDate varchar(1),
@beginDatetime datetime,
@endDatetime datetime,
@typetxt varchar(1),
@danNo varchar(22),
@depotid varchar(8)
ASdeclare @sqltxt varchar(4000)
declare @where varchar(4000) begin
set @sqltxt = ' SELECT dbo.c_ManagerAccounts.AccountsID, dbo.c_ManagerAccountsSub.depotid, dbo.c_ManagerAccountsSub.Type, dbo.c_ManagerAccountsSub.Content, dbo.c_ManagerAccountsSub.Price, dbo.c_ManagerAccounts.PayDatetime, 
      dbo.c_ManagerAccounts.ProcDatetime, dbo.c_ManagerAccounts.chkDate, dbo.c_ManagerAccounts.chkname, dbo.c_ManagerAccounts.SurDate, dbo.c_ManagerAccounts.Surname, dbo.c_ManagerAccounts.Proname 
FROM dbo.c_ManagerAccounts RIGHT OUTER JOIN dbo.c_ManagerAccountsSub ON  dbo.c_ManagerAccounts.AccountsID = dbo.c_ManagerAccountsSub.AccountsID '
if (@danNo='0')  ------单号为空的时候不加单号条件
   begin
if (@ProsureDate='0')     ----制单时间
begin 
        set   @where = '  Where  dbo.c_ManagerAccounts.ProcDatetime between ('+ @beginDatetime+  ' ) and ( '+   @endDatetime  +') and  dbo.c_ManagerAccountsSub.depotid=' + @depotid
end
else if(@ProsureDate='1')   --审核时间
begin
        set   @where = '  Where  dbo.c_ManagerAccounts.SurDate  between ' + @beginDatetime + '  and  ' + @endDatetime + ' and dbo.c_ManagerAccountsSub.depotid=' + @depotid
end
    end 
else      --------加上单号
       begin
if (@ProsureDate='0')
begin 
        set   @where = '  Where  dbo.c_ManagerAccounts.ProcDatetime between ' + @beginDatetime + '  and  ' +  @endDatetime + ' and dbo.c_ManagerAccountsSub.depotid=' + @depotid +' and dbo.c_ManagerAccounts.AccountsID='+@danNo
end
else if(@ProsureDate='1')
begin
        set   @where ='  Where  dbo.c_ManagerAccounts.SurDate  between ' +  @beginDatetime + ' and '+ @endDatetime + '  and dbo.c_ManagerAccountsSub.depotid='+@depotid+ ' and dbo.c_ManagerAccounts.AccountsID='+@danNo
end
        end         ----------加上判断类型 if (@typetxt='1')
begin
    set @where=@where+ ' and dbo.c_ManagerAccounts.chksure=0'
end
else if(@typetxt='2')
begin
     set @where=@where+' and dbo.c_ManagerAccounts.chksure=1 and dbo.c_ManagerAccounts.sure =0'
end
else if(@typetxt='3')
begin
set @where=@where +  '  and  dbo.c_ManagerAccounts.sure =1'
end              set @sqltxt= @sqltxt + @where
end
exec sp_executesql @sqltxt
GO
注意红色部分,就是在调到between 与 and 都报错,提示:从字符串转换为 datetime 时发生语法错误。,可是我前台传的是日期格式啊, 我调了好几个小时参考网上的别人的问题,都没有解决办法,我该怎么办?求助你们了。...高人帮帮我吧...

解决方案 »

  1.   

    你传入参数然后print  @sqltxt
    输出看看sql
      

  2.   

    '  Where  dbo.c_ManagerAccounts.ProcDatetime between ('+ convert(varchar(10),@beginDatetime,120)+  ' ) and ( '+  convert(varchar(10),@endDatetime,120)  +') and
      

  3.   

    htl258,你那条语句,我试过,也用不了啊,,555
      

  4.   

    dbo.c_ManagerAccounts.ProcDatetime 
    是什么类型?
      

  5.   

    用你的过程需要参数convert(varchar(10),@beginDatetime,120)这样形式,就提示另一个 '@statement' 为 'ntext/nchar/nvarchar' 类型   错误啊~~~~~~~~~~~~`,更本没@statement这个.....
      

  6.   

    dbo.c_ManagerAccounts.ProcDatetime 
    是什么类型?    当然是日期类型啊.
      

  7.   

    拿掉也没用啊,,,我都试过拉,,,到底该如何组织between间的参数啊,,崩溃,,,,,大家帮我。..
      

  8.   

    理论上没问题,你print一下最后拼接的sql语句,运行一下看看有没问题
      

  9.   

    关键是没办法拼接啊,,,,,因为调试的时候跳到第一个连接日期那里就出错,不在能继续了。比如到这里就不动了。
    set  @where = '  Where  dbo.c_ManagerAccounts.ProcDatetime between ('+ @beginDatetime+  ' ) and ( '+  @endDatetime  +')
      

  10.   

    create table tb(id int,dt datetime)
    insert into tb select 1,'2009-06-25' union all select 2,'2009-06-27' union all select 3,'2009-06-29' union all select 4,'2009-06-30' union all select 5,'2009-07-01'
    go
    declare @dt1 datetime,@dt2 datetime,@sqlstr varchar(800)
    set @dt1='2009-06-27'
    set @dt2='2009-06-30'
    set @sqlstr='select * from tb where dt between '''+convert(varchar,@dt1,120)+''' and ''' +convert(varchar,@dt2,120)+''''
    execute(@sqlstr)
    go
    --or
    declare @dt1 varchar(10),@dt2 varchar(10),@sqlstr varchar(800)
    set @dt1='2009-06-27'
    set @dt2='2009-06-30'
    set @sqlstr='select * from tb where dt between '''+@dt1+''' and ''' +@dt2+''''
    execute(@sqlstr)
    go
    drop table tb
    /*
    id          dt
    ----------- -----------------------
    2           2009-06-27 00:00:00.000
    3           2009-06-29 00:00:00.000
    4           2009-06-30 00:00:00.000(3 行受影响)id          dt
    ----------- -----------------------
    2           2009-06-27 00:00:00.000
    3           2009-06-29 00:00:00.000
    4           2009-06-30 00:00:00.000(3 行受影响)*/
      

  11.   


    对对,问题就出在这,参数是日期形式,在拼接的是报错
    ,看下面的测试,所以把参数改成字符串形式
    declare @t datetime
    set @t=getdate()
    select '现在是'+@t
    /*
    服务器: 消息 241,级别 16,状态 1,行 3
    从字符串转换为 datetime 时发生语法错误。*/
      

  12.   


    declare @strSQL nvarchar(1024)
    declare @beginDatetime datetime 
    declare @endDatetime datetimeset @beginDateTime = '2008-11-11'
    set @endDatetime = '2010-01-01'set @strSQL = 'select * from dbo.sellA where 日期 between '''+
    Convert(varchar,@beginDateTime,120)+
    ''' and '''+ Convert(varchar,@endDateTime,120)+
    ''''print @strSQLexec sp_executesql @strsql
      

  13.   

    可是我按照 13 楼的做法,传参的时候又出现这个.....   过程需要参数 '@statement' 为 'ntext/nchar/nvarchar' 类型
      

  14.   

    谢谢各位老大,终于搞定了。..谢谢你们,我爱死你们了。嘿嘿..明天加分给你们我最后问题的解决办法是定义declare @sqltxt navachar(4000) 
      

  15.   


    set  @where = '  Where  (dbo.c_ManagerAccounts.ProcDatetime between '''+
    Convert(varchar,@beginDateTime,120)+
    ''' and '''+ Convert(varchar,@endDateTime,120)+
    ''') and  dbo.c_ManagerAccountsSub.depotid= ' + @depotid--  Where  (dbo.c_ManagerAccounts.ProcDatetime between '2008-11-11 00:00:00' and '2010-01-01 00:00:00') and  dbo.c_ManagerAccountsSub.depotid= 1