当前位置: 代码迷 >> Sql Server >> 帮帮小弟我,看看这两个存储过程,如何只能插入一条
  详细解决方案

帮帮小弟我,看看这两个存储过程,如何只能插入一条

热度:141   发布时间:2016-04-27 19:34:50.0
帮帮我,看看这两个存储过程,怎么只能插入一条?
我的原意是:把表tel_ychongzhi_1 和tel_ychongzhi 里面的部分字段的数据导入到im_ask表中 把表tel_usermoney 和tel_mingxi_table 表中的部分字段数据导入到表im_user表中!
规则:如果存在则更新,不存在则插入! 运行完结果我发现,im_user表中只有一条,im_ask表中两条
各位大大给看看吧,我新进这里,分不多,回头再补!!
如果,实在不行,给改成只插入数据也行(要清空一下表im_user 和 im_ask)!!
-----------
CREATE PROCEDURE add_im_ask_data
AS
begin transaction
DECLARE @a nvarchar(20),@b nvarchar(20),@c nvarchar(20),@d nvarchar(20),@e nvarchar(20),@f nvarchar(20)
if exists(select 1 from tel_ychongzhi)  
begin
  set nocount on  
  select @a=khuser,@b=khcardNum,@c=khquhao,@d=sqriqi,@e=khcardType,@f=dbo.fn_getcode(khuser) from tel_ychongzhi
  if not exists(select * from im_ask where [email protected] and [email protected])
  insert into im_ask(u_name_code,u_code,a_card,a_date,a_flag,a_zone,a_cardType,a_updata,a_updata_time)values(@a,@f,@b,@d,'1',@c,@e,'1',getdate())
  else
  update im_ask set [email protected],a_flag='1',[email protected],[email protected],a_updata='1',a_updata_time=getdate() where [email protected] and [email protected]
  set nocount off
end
if exists(select 1 from tel_ychongzhi_1)  
 begin
  set nocount on  
  select @a=khuser,@b=khcardNum,@c=khquhao,@d=sqriqi,@e=khcardType,@f=dbo.fn_getcode(khuser) from tel_ychongzhi_1
  if not exists(select * from im_ask where [email protected] and [email protected])
  insert into im_ask(u_name_code,u_code,a_card,a_zone,a_flag,a_date,a_cast_date,a_cardType,a_updata,a_updata_time)values(@a,@f,@b,@c,'2',@d,getdate(),@e,'1',getdate())
  else
  update im_ask set [email protected],[email protected],a_flag='2',a_cast_date=getdate(),[email protected],a_updata='1',a_updata_time=getdate() where [email protected] and [email protected]
  set nocount off
  end
commit
go  
if exists (select * from sysobjects where name='add_im_user_data' and type='P')
  drop procedure add_im_user_data
go
create procedure add_im_user_data
as
begin transaction
 declare @temu_name_code nvarchar(50),@temu_code nvarchar(50),@temu_money nvarchar(50),@temu_zone nvarchar(50),@temu_area nvarchar(50),@temu_vioce_money nvarchar(50),@temu_tel_money nvarchar(50)

  select @temu_name_code=tel_usermoney.userCode,@temu_code=dbo.fn_getcode(tel_usermoney.userCode),@temu_money=tel_usermoney.userTimes,@temu_zone=substring(tel_usermoney.memo,0,patindex('%|%',tel_usermoney.memo)),@temu_area=tel_usermoney.useraddress,@temu_vioce_money=tel_mingxi_table.times,@temu_tel_money=tel_mingxi_table.MtTimes from tel_usermoney,tel_mingxi_table where tel_userMoney.usercode=tel_mingxi_table.username
  if not exists (select * from im_user where [email protected]_name_code)
  begin
  insert into im_user(u_name_code,u_code,u_money,u_zone,u_area,u_vioce_money,u_tel_money,u_updata,u_del_flag)values(@temu_name_code,@temu_code,@temu_money,@temu_zone,@temu_area,@temu_vioce_money,@temu_tel_money,'1','0')
  end
  else
  begin
  update im_user set [email protected]_money,[email protected]_zone,[email protected]_area,[email protected]_vioce_money,[email protected]_tel_money,u_updata='1' where [email protected]_name_code
  end
commit
go

------解决方案--------------------
不是很熟,帮顶``
  相关解决方案