这样的存储过程如何创建

wth668899 2006-11-26 10:01:34
现在使用存储过程向数据库中的表Orders插入一条数据,该数据表有三个字段,orderID(表的主键,数据类型int,是一个流水号,增长步长为1),userid(数据类型int),orderdate(数据类型日期),该存储过程的要求是,每次向表中插入一条数据后,都要将当前新插入行的orderid返回到输出变量@OrderID中保存,输出以备调用该存储过程的程序使用
...全文
177 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
rfq 2006-11-27
  • 打赏
  • 举报
回复
select @@identity
中国风 2006-11-26
  • 打赏
  • 举报
回复
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 行)
sgucxc0 2006-11-26
  • 打赏
  • 举报
回复
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
end

exec tt @@identity

select * from ta
/*可以看到结果更新了
1 3 2006-11-26 10:50:39.623
2 133 2006-11-26 10:51:22.623
*/
wth668899 2006-11-26
  • 打赏
  • 举报
回复
在线等待,感谢!·!!
中国风 2006-11-26
  • 打赏
  • 举报
回复
用触发器,不是用存储过程

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧