从字符串转换日期和/或时间时,SQL Server存储过程转换失败

从字符串转换日期和/或时间时,SQL Server存储过程转换失败,sql,sql-server,tsql,stored-procedures,sql-server-2014,Sql,Sql Server,Tsql,Stored Procedures,Sql Server 2014,我知道这个问题已经被问了好几次,但这些建议似乎对我没有作用。我有一个83行的存储过程,我需要调用它并将开始日期和结束日期传递给它。如果我对日期进行硬编码,这个过程非常有效;但是,当我尝试将日期作为变量传递时,我收到错误: 从字符串转换日期和/或时间时转换失败 我按如下方式调用存储过程: exec dbo.CreateReport @startDate = '10/01/2013', @endDate = '12/31/2013' create procedure dbo.CreateRepor

我知道这个问题已经被问了好几次,但这些建议似乎对我没有作用。我有一个83行的存储过程,我需要调用它并将开始日期和结束日期传递给它。如果我对日期进行硬编码,这个过程非常有效;但是,当我尝试将日期作为变量传递时,我收到错误:

从字符串转换日期和/或时间时转换失败

我按如下方式调用存储过程:

exec dbo.CreateReport @startDate = '10/01/2013', @endDate = '12/31/2013'
create procedure dbo.CreateReport 
    @startDate smalldatetime = NULL, 
    @endDate smalldatetime = NULL
我正在创建存储过程,如下所示:

exec dbo.CreateReport @startDate = '10/01/2013', @endDate = '12/31/2013'
create procedure dbo.CreateReport 
    @startDate smalldatetime = NULL, 
    @endDate smalldatetime = NULL
发生错误的代码块:

declare vp_laboraccount_timesheetitem_cursor cursor for 
            select a.laborleveldsc1,a.laborleveldsc2,a.laboracctid, sum(b.durationsecsqty)/3600 as totalhours
            from vp_laboraccount a, timesheetitem b
            where a.laboracctid=b.laboracctid and b.eventdtm >=''' + @@startDate + ''' and b.eventdtm <= ''' + @@endDate + ''' and employeeid = @personid
            group by a.laborleveldsc1,a.laborleveldsc2,a.laboracctid
            open vp_laboraccount_timesheetitem_cursor
            fetch next from vp_laboraccount_timesheetitem_cursor into @laborleveldsc1,@laborleveldsc2,@laboracctid,@totalhours
            while @@fetch_status=0
            begin
            --print results
                select @message = @personid + ',' + @personcstmdatatxt + ',' + @searchfullnm + ',' + @prdohh + ',' + @prjob1 + ',' + @laborleveldsc1 + ',' + @laborleveldsc2 + ',' + @laboracctid + ',' + @totalhours + ',' + @companhiredtm
                --print @message
                insert into dbo.tabResults (messages) Values(@message)
            fetch next from vp_laboraccount_timesheetitem_cursor into @laborleveldsc1,@laborleveldsc2,@laboracctid,@totalhours
            end
            close vp_laboraccount_timesheetitem_cursor
            deallocate vp_laboraccount_timesheetitem_cursor
declare vp_laboraccount_timesheetitem_游标
选择a.laborleveldsc1、a.laborleveldsc2、a.laboracctid、总和(b.durationsecsqty)/3600作为总小时数
来自vp_实验室账户a,时间表b

其中,a.laboracctid=b.laboracctid和b.eventdtm>='+@@startDate+''和b.eventdtm='+@@startDate+''和b.eventdtmSQL Server中唯一真正安全的日期/时间文本格式,至少对于
datetime
smalldatetime
,是:
yyyyymmdd
yyyyyyy-MM-ddth:MM:ss[.nnn]/code>-

对变量使用单个
@

exec dbo.CreateReport @startDate='20131001', @endDate='20131231'
你的
where
子句应该是这样的:

where a.laboracctid=b.laboracctid
  and b.eventdtm >= @startDate
  and b.eventdtm <= @endDate
  and employeeid = @personid
其中a.laboracctid=b.laboracctid
和b.eventdtm>=@startDate

和b.eventdtm删除引号。只需使用eventdtm>=@startDate(与@endDate相同)。在这里不使用动态sql时,不需要连接。而且你只需要一个@。这是一个局部变量。您还应该使用ANSI-92样式的联接,它们已经存在25年多了。你应该使用有意义的别名来避免令人难以置信的痛苦。删除引号和连接符有效。我尝试过这方面的变化,但从未真正尝试过完全删除它们。感谢scsimon和SqlZim为您提供的建议和帮助。我也会看看其他的建议,因为我总是愿意把我的东西做得更好。@user3788019很乐意帮忙!