同时设置变量显示存储过程中的错误

时间:2013-08-25 18:16:07

标签: sql-server

我有这样的存储过程:

create PROCEDURE [dbo].[Test1]  
   @startdate datetime(100),  
   @enddate datetime(100)
   set  date1 = CONVERT(datetime, @startdate + ' 00:01:00', 120)
   set   date2 = CONVERT(datetime, @enddate + ' 00:23:59', 120)  
as  
begin  
  select t.PlateNo 
  from Transaction_tbl t  
  WHERE dtime between @date1 + ' 00.01.00.00' 
    AND @date2 + ' 23:59.59.00'  
end

执行此操作时会收到两条错误消息

  

关键字' set'附近的语法不正确。

     

必须声明标量变量@date1

2 个答案:

答案 0 :(得分:0)

试试这个: -

create PROCEDURE [dbo].[Test1]  
   @startdate varchar(100),  
   @enddate varchar(100)
as 
declare @date1 datetime
declare @date2 datetime
   set  @date1 = CONVERT(datetime, @startdate + ' 00:01:00', 120)
   set   @date2 = CONVERT(datetime, @enddate + ' 00:23:59', 120)  

begin  
  select t.PlateNo 
  from Transaction_tbl t  
  WHERE dtime between @date1 + ' 00.01.00.00' 
    AND @date2 + ' 23:59.59.00'  
end

答案 1 :(得分:0)

我认为你的意思是:

create PROCEDURE [dbo].[Test1]  
   @startdate varchar(100),  
   @enddate varchar(100)
as  
begin  
   declare @date1 datetime = CONVERT(datetime, @startdate + ' 00:01:00', 120);
   declare @date2 datetime = CONVERT(datetime, @enddate + ' 00:23:59', 120);

   select t.PlateNo 
   from Transaction_tbl t  
   WHERE dtime between @date1 AND @date2; 
end;

但是,我认为您最好按照我在上一个问题的答案中的建议并将参数传递为date