求个简单的存储过程

mythqxh 2009-11-03 05:25:37
存储过程有Q1,Q2两个参数,分别保存到table1,table2 2张表。
中间做个判断。如果在table1表中的ID字段中有和Q1相同的数据就停止 返回int 1 。如果没有找到就把Q1保存到table1.Q2保存到table2 表中,
如果保存出错 返回 int 2.如果都执行成功返回0.谢谢
...全文
83 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ws_hgo 2009-11-03
  • 打赏
  • 举报
回复
create table t1
(
ID varchar(20)
)
insert into t1 select 'aaa'
insert into t1 select 'bbb'

create table t2
(
ID varchar(20)
)
insert into t2 select 'ccc'
insert into t2 select 'ddd'

alter proc proc_insert
(
@Q1 varchar(20),
@Q2 varchar(20)
)
as
if exists(select * from t1 where ID=@Q1)
begin
return 1
end
else
begin
insert into t1 select @Q1
if @@error<>0
return 2
insert into t2 select @Q2
if @@error<>0
return 2
else
return 0
end

exec proc_insert 'aaa','cccccccc'

select * from t1

ID
--------------------
aaa
bbb
exec proc_insert 'aaabbbb','cccccccc'

select * from t1

ID
--------------------
aaa
bbb
aaabbbb

select * from t2

ID
--------------------
ccc
ddd
cccccccc
bancxc 2009-11-03
  • 打赏
  • 举报
回复
create table MyPro(@Q1 int,@Q2 int)
as
begin
if exists(select top 1 1 from table1 where ID=@Q1)
return 1

insert into table1(ID) values(@Q1)
if @@ERROR>0
return 2
insert into table2(ID) values(@Q2)
if @@ERROR>0
return 2
return 0
end
mythqxh 2009-11-03
  • 打赏
  • 举报
回复
都下班了?
mythqxh 2009-11-03
  • 打赏
  • 举报
回复
刚才逻辑不对。重新来。。。
--小F-- 2009-11-03
  • 打赏
  • 举报
回复
刚才不是问过了吗??没解决?

34,594

社区成员

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

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