本文介绍了如何在存储过程sql server中插入两个不同的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 大家好 这里我面临着在商店程序中插入两张桌子的问题 my代码是: 创建 proc [dbo]。[Insert_StaffMaster_DeviceInfo_SP] ( @ UserIdN int , @ EmpCodeC nvarchar ( 15 ), @ empnamec varchar ( 50 ), @ deptcodec varchar ( 50 ), @ DesigCodeC varchar ( 50 ), @ seccodec varchar ( 50 ), @ Dojd smalldatetime , @ TerminateDateD smalldatetime , @ DOB smalldatetime , @ CatgCodeC varchar ( 50 ), @性别 nvarchar ( 1 ), @ Photo nvarchar ( 255 ), @LocationC nvarchar ( 150 ), @ShiftGroupCodeC varchar ( 10 ), @ NricWPNoC varchar ( 15 ), @ Privilege varchar ( 50 ), @ Password nvarchar ( 50 ), @ Enabled nvarchar ( 50 ), @ CardNumber nvarchar (MAX)) as begin 如果 不 存在(选择 UserIdN 来自 StaffMaster 其中 UserId N = @ UserIdN) 插入 进入 StaffMaster(UserIdN,EmpCodeC,empnamec,deptcodec,DesigCodeC) ,seccodec,Dojd,TerminateDateD,DOB,CatgCodeC,性别,照片,LocationC,ShiftGroupCodeC,NricWPNoC)值( @ UserIdN , @ EmpCodeC , @ empnamec , @ deptcodec , @ DesigCodeC , @ seccodec , @ Dojd , @ TerminateDateD , @ DOB , @CatgCodeC , @ Gender , @ Photo , @LocationC , @ ShiftGroupCodeC , @ NricWPNoC ) insert into Device_StaffFP(UserIDN,Privilege,Password,Enabled,CardNumber)值( @ UserIDN , @ Privilege , @密码, @ Enabled , @ CardNumber ) else update StaffMaster set UserIdN = @ UserIdN,EmpCodeC = @ EmpCodeC,empnamec = @ empnamec,deptcodec = @ deptcodec,DesigCodeC = @ DesigCodeC,seccodec = @ seccodec,Dojd = @ Dojd,TerminateDateD = @ TerminateDateD,DOB = @ DOB,CatgCodeC = @ CatgCodeC,性别= @性别,照片= @照片,LocationC = @ LocationC,ShiftGroupCodeC = @ ShiftGroupCodeC,NricWPNoC = @ NricWPNoC 其中 UserIdN = @ UserIdN update Device_StaffFP set UserIDN = @ UserIDN,Privilege = @ Privilege,Password = @ Password,Enabled = @ Enabled,CardNumber = @ CardNumber 其中 UserIDN = @ UserIDN end 当我尝试这个我得到错误也就是说, 关键字'else'附近的语法不正确。 还有一个我应该使用此查询进行更新吗? 谢谢!解决方案 你的if块包含多个语句,所以你必须将它们包装在BEGIN之间.... END 如果不存在(... 。) BEGIN 插入声明1 插入声明2 END ELSE BEGIN 更新声明1 更新声明2 END Hi All Here i am facing problem with insert two table in store proceduremy Code is:create proc [dbo].[Insert_StaffMaster_DeviceInfo_SP](@UserIdN int,@EmpCodeC nvarchar(15),@empnamec varchar(50),@deptcodec varchar(50),@DesigCodeC varchar(50),@seccodec varchar(50),@Dojd smalldatetime,@TerminateDateD smalldatetime,@DOB smalldatetime,@CatgCodeC varchar(50),@Gender nvarchar(1),@Photo nvarchar(255),@LocationC nvarchar(150),@ShiftGroupCodeC varchar(10),@NricWPNoC varchar(15),@Privilege varchar(50),@Password nvarchar(50),@Enabled nvarchar(50),@CardNumber nvarchar(MAX) )asbeginif not exists(select UserIdN from StaffMaster where UserIdN=@UserIdN)insert into StaffMaster (UserIdN,EmpCodeC,empnamec,deptcodec,DesigCodeC,seccodec,Dojd,TerminateDateD,DOB,CatgCodeC,Gender,Photo,LocationC,ShiftGroupCodeC,NricWPNoC) values (@UserIdN,@EmpCodeC,@empnamec,@deptcodec,@DesigCodeC,@seccodec,@Dojd,@TerminateDateD,@DOB,@CatgCodeC,@Gender,@Photo,@LocationC,@ShiftGroupCodeC,@NricWPNoC)insert into Device_StaffFP(UserIDN,Privilege,Password,Enabled,CardNumber) values (@UserIDN,@Privilege,@Password,@Enabled,@CardNumber)else update StaffMaster set UserIdN=@UserIdN,EmpCodeC=@EmpCodeC,empnamec=@empnamec,deptcodec=@deptcodec,DesigCodeC=@DesigCodeC,seccodec=@seccodec,Dojd=@Dojd,TerminateDateD=@TerminateDateD,DOB=@DOB,CatgCodeC=@CatgCodeC,Gender=@Gender,Photo=@Photo,LocationC=@LocationC,ShiftGroupCodeC=@ShiftGroupCodeC,NricWPNoC=@NricWPNoC where UserIdN=@UserIdNupdate Device_StaffFP set UserIDN=@UserIDN,Privilege=@Privilege,Password=@Password,Enabled=@Enabled,CardNumber=@CardNumber where UserIDN=@UserIDNend when i try this i got error that is,Incorrect syntax near the keyword 'else'.and one more this shall i use this query for Updating ?Thank You ! 解决方案 your if block contains more than 1 statement so you have to wrap them between BEGIN....ENDIF not exists(....)BEGIN Insert statement 1 Insert statement 2ENDELSEBEGIN update statement 1 update statement 2END 这篇关于如何在存储过程sql server中插入两个不同的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-18 12:09