使用SQL更新两个表? [英] update two tables using sql?

查看:94
本文介绍了使用SQL更新两个表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我创建了2个表:

table1栏:名称varchar(50),地址varchar(50),电子邮件地址varchar(50)

table2栏:移动bigint,EmailAddress varchar(50)

在这里,我想一次通过单个更新查询来更新两个表

请回复我

Dear All,

I created 2 tables:

table1 columns: name varchar(50), Address varchar(50), emailaddress varchar(50)

table2 columns: mobile bigint, EmailAddress varchar(50)

and here i want update both tables at a time in single update query

please reply me

推荐答案

最简单的方法是创建一个存储过程,该存储过程创建一个Transaction,然后同时执行更新和Commits,如果它们都起作用,否则它会回滚到确保数据库完整性.

Google将为您找到很多例子.
Easiest way is to create a Stored Procedure which creates a Transaction, then does both updates and Commits if they both work - otherwise it does a Rollback to ensure DB integrity.

Google will find you many, many examples.


使用所需的输入类型字段创建存储过程,并将数据同时插入两个表中. 例如

Create a stored procedure with desired input type fields and insert the data into both the tables simultaneously.
e.g

CREATE PROCEDURE [dbo].[spMyInsertData]
(@name nvarchar(50), @address nvarchar(max), @emailAddress nvarchar(max), @mobile bigint )
as 
declare @insertQuery nvarchar(max)
Begin
	Begin Try
		set @insertQuery = ''INSERT INTO table1(name,address,emailaddress)
                values(''+@name+'',''+@address+'',''+@emailAddress+'')''
		set @insertQuery = @insertQuery+'' INSERT INTO table2(mobile,EmailAddress)
                values(''+@mobile+'',''+@emailAddress+'')''
		print @insertQuery;
		exec sp_executeSql @insertQuery;
	End Try
	Begin Catch
		print @@error
	End Catch
End


这篇关于使用SQL更新两个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆