存储过程有Q1,Q2两个参数,分别保存到table1,table2 2张表。
中间做个判断。如果在table1表中的ID字段中有和Q1相同的数据就停止 返回int 1 。如果没有找到就把Q1保存到table1.Q2保存到table2 表中,
如果保存出错 返回 int 2.如果都执行成功返回0.谢谢
中间做个判断。如果在table1表中的ID字段中有和Q1相同的数据就停止 返回int 1 。如果没有找到就把Q1保存到table1.Q2保存到table2 表中,
如果保存出错 返回 int 2.如果都执行成功返回0.谢谢
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
(
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
endexec proc_insert 'aaa','cccccccc'select * from t1ID
--------------------
aaa
bbb
exec proc_insert 'aaabbbb','cccccccc'select * from t1ID
--------------------
aaa
bbb
aaabbbbselect * from t2ID
--------------------
ccc
ddd
cccccccc