我正在使用SQL Server 2014.我查找了Joel Coehoorn为this question提供的解决方案,但它对我不起作用。
我有一个客户端表,我将其重命名为Aac_Client
,其中包含客户端信息和地址信息作为列。我想将地址移到一个新表Address
来组织事情,并将其余的事情保留在Ac_Client
。我正在尝试执行以下操作:
请注意,地址与客户之间的关系是一对一的。
BEGIN TRANSACTION
DECLARE @DataID int;
-- Insert Address
INSERT INTO Address ([StreetNumber],[StreetName] ,[StreetAddress2] ,[Unit] ,[City] ,[State] ,[Zip] ,[County])
SELECT [StreetNumber], [StreetName], [StreetAddress2], [Unit] ,[City] ,[State] ,[Zip] ,
NULL AS [County]
FROM Aac_Client
-- Get Address Id
SELECT @DataID = scope_identity();
-- Insert Client
INSERT INTO Ac_Client ( AddressId, Name, Phone, Contact )
SELECT @DataID AS AddressId, Name , Phone, Contact FROM Aac_Client
COMMIT
但问题是INSERT是立即执行的,我在SELECT @DataID = scope_identity();
答案 0 :(得分:2)
BEGIN TRANSACTION
IF OBJECT_ID('tempdb..#InsertedAddresses') IS NOT NULL
BEGIN
DROP TABLE #InsertedAddresses
END
CREATE TABLE #InsertedAddresses (
AddressId INT
,ClientId INT
)
DECLARE @DataID int;
-- add a unique client identifier
-- Insert Address
INSERT INTO Address ([StreetNumber],[StreetName] ,[StreetAddress2] ,[Unit] ,[City] ,[State] ,[Zip] ,[County], [ClientId])
OUTPUT INSERTED.AddressId, INSERTED.ClientId (AddressId, ClientId)
SELECT [StreetNumber], [StreetName], [StreetAddress2], [Unit] ,[City] ,[State] ,[Zip] ,
NULL AS [County]
FROM Aac_Client
-- Insert Client
INSERT INTO Ac_Client ( AddressId, Name, Phone, Contact, ClientId)
SELECT i.AddressId, Name , Phone, Contact, c.ClientId
FROM Aac_Client c
INNER JON #InsertedAddresses i
ON c.ClientId = i.ClientId
COMMIT
将第一个insert语句的output子句用于临时表。客户也可能有多个地址吗?或者在同一地址有多个客户?如果是这样,您可能需要考虑将AC_Client ID放在地址表中而不是客户端表中的address_id。此外,如果您不担心在同一地址的多个客户端的情况下独立更改地址,您可以考虑使用第3个表来关联地址和客户端,这样您就不必重复地址或客户端来创建多对多的关系。 / p>
我想扩展scope_identity是一个标量值,意味着只保留1个值,它是范围内更改的最后一行的标识。在您的情况下,您需要所有ID,因此您需要使用输出子句。
答案 1 :(得分:1)
--Add the following Columns that you can later Drop
ALTER TABLE [Address] ADD Col_GUID UNIQUEIDENTIFIER;
ALTER TABLE [Aac_Client] ADD Col_GUID UNIQUEIDENTIFIER;
GO
-- Give a Unique value to each row.
UPDATE [Aac_Client] SET Col_GUID = NEWID();
BEGIN TRANSACTION;
-- Table variable to capture newly generated Identity values
Declare @NewID TABLE (AddressId INT, Col_GUID UNIQUEIDENTIFIER)
-- Insert Address (with output clause to get the Identity values)
INSERT INTO [Address] ([StreetNumber],[StreetName] ,[StreetAddress2]
,[Unit] ,[City] ,[State] ,[Zip] ,[County], Col_GUID)
OUTPUT Inserted.AddressId , Inserted.Col_GUID INTO @NewID (AddressId , Col_GUID)
SELECT [StreetNumber], [StreetName], [StreetAddress2]
,[Unit] ,[City] ,[State] ,[Zip] , NULL AS [County] , Col_GUID
FROM Aac_Client
-- Insert Client joining with the Table variable on the guid column to
-- to get the new Identity values.
INSERT INTO Ac_Client ( AddressId, Name, Phone, Contact )
SELECT N.AddressId, a.Name , a.Phone, a.Contact
FROM Aac_Client a
INNER JOIN @NewID N ON n.Col_GUID = a.Col_GUID
COMMIT TRANSACTION;
最后,您可以删除为此目的添加的列。
ALTER TABLE [Address] DROP COLUMN Col_GUID;
ALTER TABLE [Aac_Client] DROP COLUMN Col_GUID;