CREATE PROCEDURE [PostVote]
@PID nvarchar(100),
@UserID nvarchar(100),
@IP nvarchar(15),
@return int =0 output
AS
BEGIN
declare @time datetime
select @time=[Time] from [Vote] Where [UserID]=@UserID
select @time=isnull(@time,dateadd(hh,-2,getdate())
if datediff(hh,getdate(),@time)>1 --这个地方提示语法错误,请问该怎么写?
begin
Update [Photos] Set [Vote]=[Vote]+1 Where ID=@PID
Insert into [Vote](PhotoID,UserID,IP) Values(@PID,@UserID,@IP)
select @return=1
end
END
GO

解决方案 »

  1.   

    --确保@time是Datetime型,试试这个if datediff(hh,convert(datetime,getdate()),convert(datetime,@time))>1
      

  2.   

    CREATE PROCEDURE [PostVote]
        @PID nvarchar(100),
        @UserID nvarchar(100),
        @IP nvarchar(15),
        @return int =0 output
    AS
    BEGIN
        declare @time datetime
        select @time=[Time] from [Vote] Where [UserID]=@UserID
        select @time=isnull(@time,dateadd(hh,-2,getdate()))
        if datediff(hh,getdate(),@time)>1 --这个地方提示语法错误,请问该怎么写?
        begin
            Update [Photos] Set [Vote]=[Vote]+1 Where ID=@PID
            Insert into [Vote](PhotoID,UserID,IP) Values(@PID,@UserID,@IP)
            select @return=1
        end
    END
    GO
      

  3.   

    select @time=isnull(@time,dateadd(hh,-2,getdate()))
    少了一边括号
      

  4.   

    CREATE PROCEDURE [PostVote]
        @PID nvarchar(100),
        @UserID nvarchar(100),
        @IP nvarchar(15),
        @return int =0 output
    AS
    BEGIN
        declare @time datetime
        select @time=[Time] from [Vote] Where [UserID]=@UserID
        set @time=isnull(@time,dateadd(hh,-2,getdate()))--
    if (datediff(hh,getdate(),@time)>1) --
        begin
            Update [Photos] Set [Vote]=[Vote]+1 Where ID=@PID
            Insert into [Vote](PhotoID,UserID,IP) Values(@PID,@UserID,@IP)
            select @return=1
        end
    END
    GO
      

  5.   

    select @time=isnull(@time,dateadd(hh,-2,getdate())是这一句的问题要这样才行
    select @time=isnull(@time,cast(dateadd(hh,-2,getdate()) as datetime))