create PROCEDURE tr_jifen_pro
(
    @cd char(8) ,
    @xph char(12) ,
    @kh decimal(15,2)
--设置变参
)
 AS
if not exists   --判断下面的select是否返回值,如果不返回值,执行下面的语句,如果返回跳到else
(select 1 from tr_customer where cardno =@cd)   --直接判断卡号是否在tr_customer表中
begin
insert into tr_jifen
(cardno,xulh,riqi,shishje,kouhj)
select '非本店卡',xulh,riqi,shishje,@kh
from retjels
where xulh=@xph and yishj='否'  --当选定的小票号在retjels中的yishj字段为否,查询纪录插入tr_jifen表end
else
begininsert into tr_jifen
(cardno,xulh,riqi,shishje,kouhj)
select @cd,xulh,riqi,shishje,@kh
from retjels
where xulh=@xph and yishj='否'  ----当选定的小票号在retjels中的yishj字段为否,查询纪录插入tr_jifen表
update retjels set yishj='是' where xulh=@xph  --当选定的小票号在retjels中的yishj字段为是endselect *
from tr_jifen
where xulh=@xphGO
这个存储过程作用是凭信用卡的小票号,往表中写入会员消费明细
各位高手能不能帮我加入一个时间判断,判断小票号的日期和本地的机器日起是否一致
如果一致,将retjels中的数据写入到tr_jifen表中,如果不一致,
只提示此票已过期,请使用当天的信誉卡。
谢谢各位

解决方案 »

  1.   

    create PROCEDURE tr_jifen_pro
    (
        @cd char(8) ,
        @xph char(12) ,
        @kh decimal(15,2)
    --设置变参
    )
     AS
    if left(@xph,8) = convert(char(8),getdate(),112)--假设前8位是日期
        if not exists   --判断下面的select是否返回值,如果不返回值,执行下面的语句,如果返回跳到else
       (select 1 from tr_customer where cardno =@cd)   --直接判断卡号是否在tr_customer表中
        begin
          insert into tr_jifen
         (cardno,xulh,riqi,shishje,kouhj)
         select '非本店卡',xulh,riqi,shishje,@kh
         from retjels
        where xulh=@xph and yishj='否'  --当选定的小票号在retjels中的yishj字段为否,查询纪录 插入tr_jifen表    end
       else
        begin    insert into tr_jifen
        (cardno,xulh,riqi,shishje,kouhj)
        select @cd,xulh,riqi,shishje,@kh
        from retjels
        where xulh=@xph and yishj='否'  ----当选定的小票号在retjels中的yishj字段为否,查询纪录插入tr_jifen表
        update retjels set yishj='是' where xulh=@xph  --当选定的小票号在retjels中的yishj字段为是   end
    else
      begin 
        raiserror ..
        return 
    endselect *
    from tr_jifen
    where xulh=@xph
    GO
      

  2.   

    刚才没说清楚,是这样
    上面的存储过程,用到3个表,分别是retjels,tr_customer,tr_jifen
    retjels中有xulh(小票号)字段,riqi(小票号上的日期)
    tr_customer上面是会员的资料
    tr_jifen上面是会员消费的明细
    现在我要判断如果小票号上面的日期与本地机器日期不一致的话,就提示‘已过期’
    如果一致就正常执行插入,并最后查询纪录
    而且判断日期需要在判断完卡号后也就是else后面加入
      

  3.   

    create PROCEDURE tr_jifen_pro
    (
        @cd char(8) ,
        @xph char(12) ,
        @kh decimal(15,2)
    --设置变参
    )
     AS
        if not exists   --判断下面的select是否返回值,如果不返回值,执行下面的语句,如果返回跳到else
       (select 1 from tr_customer where cardno =@cd)   --直接判断卡号是否在tr_customer表中
        begin
          insert into tr_jifen
         (cardno,xulh,riqi,shishje,kouhj)
         select '非本店卡',xulh,riqi,shishje,@kh
         from retjels
        where xulh=@xph and yishj='否'  --当选定的小票号在retjels中的yishj字段为否,查询纪录 插入tr_jifen表    end
       else
    if left(@xph,8) = convert(char(8),getdate(),112)--假设前8位是日期    begin    insert into tr_jifen
        (cardno,xulh,riqi,shishje,kouhj)
        select @cd,xulh,riqi,shishje,@kh
        from retjels
        where xulh=@xph and yishj='否'  ----当选定的小票号在retjels中的yishj字段为否,查询纪录插入tr_jifen表
        update retjels set yishj='是' where xulh=@xph  --当选定的小票号在retjels中的yishj字段为是   end
    else
      begin 
        raiserror ..
        return 
      end
    endselect *
    from tr_jifen
    where xulh=@xph
    GO
      

  4.   

    create PROCEDURE tr_jifen_pro
    (
        @cd char(8) ,
        @xph char(12) ,
        @kh decimal(15,2)
    --设置变参
    )
     AS
    if not exists   --判断下面的select是否返回值,如果不返回值,执行下面的语句,如果返回跳到else
    (select 1 from tr_customer where cardno =@cd)   --直接判断卡号是否在tr_customer表中
    begin
    insert into tr_jifen
    (cardno,xulh,riqi,shishje,kouhj)
    select '非本店卡',xulh,riqi,shishje,@kh
    from retjels
    where xulh=@xph and yishj='否'  --当选定的小票号在retjels中的yishj字段为否,查询纪录插入tr_jifen表endelsebeginif not exists
    (select 1 from retjels where riqi=convert(char(10),getdate(),120) and xulh=@xph)beginselect '此票已过期,请使用当天的信誉卡' as 提示end elsebegininsert into tr_jifen
    (cardno,xulh,riqi,shishje,kouhj,jiqrq)
    select @cd,xulh,riqi,shishje,@kh,convert(char(10),getdate(),120)
    from retjels
    where xulh=@xph and yishj='否'  ----当选定的小票号在retjels中的yishj字段为否,查询纪录插入tr_jifen表
    update retjels set yishj='是' where xulh=@xph  --当选定的小票号在retjels中的yishj字段为是select *
    from tr_jifen
    where xulh=@xphend
    endGO我自己写的,有错误
    主要判断的不是@xph而是retjels中的riqi字段
      

  5.   

    create table tr_customer --创建新tr_customer表
    (
        xh int identity(1,1), 
        cardno char(8) not null,
        certif_no char(18) not null,
        certif char(8) not null,
        uname char(8) not null,
        sex char(2) null,
        phone char(12) null,
        kriqi char(10) null
        --primary key (certif_no)设置主键不运行
    )
    -----------------------------------------------create table tr_jifen  --创建新tr_jifen表
    (
        cardno char(8) not null,
        xulh char(12) not null,
        riqi char(10) null,
        shishje decimal(15,2) null default 0,
        kouhj decimal (15,2) null default 0,
        jiqrq char(10) null,
        shoukyh char(6) null,
        jfsky char(6) null
    )retjels中有xulh,riqi,shishje,shoukyh字段