简体   繁体   English

将数据插入3个相关表

[英]Insert data into 3 related tables

How to insert data into 3 related tables (SQL Server) 如何将数据插入3个相关表(SQL Server)

For example, I have tables Customer <-- Customer_Address --> Address After I insert data into Customer and Address, how do I insert the IDs from Customer and Address in to Customer_Address? 例如,我有表Customer <-Customer_Address-> Address将数据插入到Customer和Address之后,如何将ID从Customer和Address插入到Customer_Address? (join table) (联接表)

Thanks! 谢谢!

Use SCOPE_IDENTITY , @@IDENTITY can return a value from any scope: 使用SCOPE_IDENTITY ,@@ IDENTITY可以从任何范围返回值:

DECLARE @CustomerId INT
DECLARE @AddressId INT

BEGIN TRANSACTION

  INSERT INTO CUSTOMER (blah, blah) values (blah, blah)
  SET @CustomerId = SCOPE_IDENTITY

  INSERT INTO ADDRESS (blah, blah) values (blah, blah)
  SET @AddressId = SCOPE_IDENTITY 

  INSERT INTO CUSTOMERADDRESS (CustomerId,AddressId) values (@CustomerId,@AddressId)

COMMIT TRANSACTION

If you want to insert more than one row you can use the output clause: 如果要插入多行,可以使用output子句:

declare @insertedAddresses table (OriginalID int, AddressID int);
declare @insertedCustomers table (OriginalID int, CustomerID int);

insert into dbo.Addresses (AddressData)
output source.OriginalID, inserted.AddressID into @insertedAddresses
select AddressData from source;

insert into dbo.Customers (CustomerData)
output source.OriginalID, inserted.CustomerID into @insertedCustomers
select CustomerData from source;

insert into dbo.Customer_Address (AddressID, CustomerID)
select a.AddressID, c.CustomerID
from @insertedAddresses a inner join @insertedCustomers c on c.OriginalID=a.OriginalID;

If the IDs from the Customer and Address tables are Identity columns, you can store the new ID into a variable. 如果“客户”和“地址”表中的ID是“身份”列,则可以将新ID存储到变量中。

DECLARE @CustomerID int

SELECT @CustomerID = @@IDENTITY FROM TABLE CUSTOMER

Similar syntax could be used for the Address table. 类似的语法可以用于地址表。 Then in your INSERT statement you can do this: 然后,在您的INSERT语句中,您可以执行以下操作:

INSERT INTO Customer_Address (CustomerID, AddressID)
VALUES (@CustomerID, @AddressID)

use a transaction, and remember the identities. 使用交易,并记住身份。 in sql server - 在SQL Server中-

declare @CustomerId int
declare @AddressId int

begin tran
    insert into Customer (blah, blah) values (blah, blah)
    set @CustomerId = @@IDENTITY    --assuming there are no triggers
    insert into [Address] (blah, blah) values (blah, blah)
    set @AddressId = @@IDENTITY     --once again, no triggers to mess up the @@IDENTITY
    insert into CustomerAddress(CustomerId,AddressId) values (@CustomerId,@AddressId)
commit

Here is one way. 这是一种方法。

Declare CustomerID int, AddressID int

insert into Customer (list, of, fields) values (list, of, values) select @CustomerID=scope_Identity() insert into Address (list, of, fields) values (list, of, values) select @AddressID =scope_Identity()

insert into Customer_Address (CustomerID, AddressID) values (@CustomerID, @AddressID)

If it is for an application, why don't just use Linq, you can accomplish things like this in just a few minutes, in a more compressive way and more thigh to the business logic model. 如果是针对应用程序的,为什么不仅仅使用Linq,您可以在几分钟内以更压缩的方式和更大的业务逻辑模型完成类似的工作。

If not the I highly recommend the transaction and SCOPE_IDENTITY version, because if you are in a heavy loaded system, an other operation can do an insert and change the identity value so you will end with inconsistent data. 如果不是这样,我强烈建议您使用事务和SCOPE_IDENTITY版本,因为如果您的系统负载很重,则其他操作可以插入并更改标识值,因此您将获得不一致的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM