现在使用存储过程向数据库中的表Orders插入一条数据,该数据表有三个字段,orderID(表的主键,数据类型int,是一个流水号,增长步长为1),userid(数据类型int),orderdate(数据类型日期),该存储过程的要求是,每次向表中插入一条数据后,都要将当前新插入行的orderid返回到输出变量@OrderID中保存,输出以备调用该存储过程的程序使用

解决方案 »

  1.   

    create table ta (orderID int identity(1,1),userid int,orderdate datetime)insert into ta select 3,'2006-11-26 10:50:39.623' union all select 4,'2006-11-26 10:51:22.623'select * from ta
    /*
    1  3  2006-11-26 10:50:39.623
    2  4  2006-11-26 10:51:22.623*/create proc tt
    @orderid int
    as
    begin
      update ta set userid=133 where orderid=@orderid
    endexec tt @@identity select * from ta
    /*可以看到结果更新了
    1  3  2006-11-26 10:50:39.623
    2  133  2006-11-26 10:51:22.623
    */
      

  2.   

    create table ta(orderID int identity(1,1),userid int,orderdate datetime)
    create  trigger ta_tr on ta
    for insert
    as 
    select orderID from inserted
    就这样就行了,显示插入的orderID 
    --测试
    insert into ta select 1, '2006-11-26 10:50:39.623' 
    union all select 4,'2006-11-26 10:51:22.623'
    orderID     
    ----------- 
    1
    2(所影响的行数为 2 行)