日期:2014-05-19  浏览次数:20526 次

高分求一存储过程
ALTER   PROCEDURE   db_owner.edittzsm  
@uid   int
AS
  declare   @sql   varchar(500)

    if   exists   (select   *   from   userlicai   where   uid=@uid   and   DATEDIFF(d,[date],DATEADD(hh,6,getdate()))=0)
          begin
                set     @sql=insert   into   userlicai(uid,[date])   values(@uid,DATEADD(hh,6,getdate()))
              if     DATEPART(ww,DATEADD(hh,6,getdate()))=7    
                    set     @sql=insert   into   userlicai(uid,[date])   values(@uid,DATEADD(hh,54,getdate()))  
              if     DATEPART(ww,DATEADD(hh,6,getdate()))=1
                      begin
                        if   not   exists(select   *   from   userlicai   where   uid=@uid   and   DATEDIFF(d,[date],DATEADD(hh,30,getdate()))=0)
                              set   @sql=insert   into   userlicai(uid,[date])   values(@uid,DATEADD(hh,30,getdate()))
                      end
            end
  exec   @sql


总是报错,请高手帮帮忙!

------解决方案--------------------
ALTER PROCEDURE db_owner.edittzsm
@uid int
AS
declare @sql varchar(500)

if exists (select * from userlicai where uid=@uid and DATEDIFF(d,[date],DATEADD(hh,6,getdate()))=0)
begin
set @sql= 'insert into userlicai(uid,[date]) values( '+rtrim(@uid)+ ',DATEADD(hh,6,getdate())) '
if DATEPART(ww,DATEADD(hh,6,getdate()))=7
set @sql= 'insert into userlicai(uid,[date]) values( '+rtrim(@uid)+ ',DATEADD(hh,54,getdate())) '
if DATEPART(ww,DATEADD(hh,6,getdate()))=1
begin
if not exists(select * from userlicai where uid=@uid and DATEDIFF(d,[date],DATEADD(hh,30,getdate()))=0)
set @sql= 'insert into userlicai(uid,[date]) values( '+rtrim(@uid)+ ',DATEADD(hh,30,getdate())) '
end
end
exec @sql
------解决方案--------------------
ALTER PROCEDURE db_owner.edittzsm
@uid int
AS
declare @sql varchar(500)

if exists (select * from userlicai where uid=@uid and DATEDIFF(d,[date],DATEADD(hh,6,getdate()))=0)
begin
set @sql= 'insert into userlicai(uid,[date]) values( '+cast(@uid as varchar)+ ', ' ' '+cast(DATEADD(hh,6,getdate()) as varchar)+ ' ' ') '
if DATEPART(ww,DATEADD(hh,6,getdate()))=7
set @sql= 'insert into userlicai(uid,[date]) values( '+cast(@uid as varchar)+ ', ' ' '+cast(DATEADD(hh,54,getdate()) as varchar)+ ' ' ') '
if DATEPART(ww,DATEADD(hh,6,getdate()))=1
begin
if not exists(select * from userlicai where uid=@uid and DATEDIFF(d,[date],DATEADD(hh,30,getdate()))=0)
set @sql= 'insert into userlicai(uid,[date]) values( '+cast(@uid as varchar)+ ', ' ' &#