【问题标题】:Multiple table Set-based insert with identity column in table1 and foreign key in table2 [duplicate]多表基于集合的插入,表1中的标识列和表2中的外键[重复]
【发布时间】:2014-10-28 12:36:23
【问题描述】:

我正在编写一个从多个外部系统导入配置数据的应用程序。为了将用户数据与导入的数据分离,您可以使用中间表。为便于论证,下表进行了简化。

  • 表[connection](不包括其他表引用的通用连接信息)
  • table [importConfig](来自外部系统之一的连接配置)
  • 表[connectionImportConfig](中间表,链接[connection]和[importConfig]

对于 [importConfig] 中的每个连接,我想在 [connection] 中创建一个带有标识列的行。然后我想在 [connectionImportConfig] 中插入来自 [connection] 的新 ID 和来自 [importConfig] 的标识符。

约束:我不想使用光标。必须是基于集合的解决方案。

我在 stackoverflow 上找到的示例不是基于集合的,或者我发现它们由于其他原因不适用。

我已经尝试了很多,但我不得不将新 ID 插入 connectionImportConfig

-- Test tables&data (stripped down version)
CREATE TABLE connection            (ConnectionID int identity(1,1) NOT NULL, comment nvarchar(max) null)
CREATE TABLE connectionImportConfig(connectionID int NOT NULL, ConfigCode nvarchar(50) NOT NULL)
CREATE TABLE importConfig          (ConfigCode nvarchar(50) NOT NULL)
DECLARE @MyConnection table        (ConnectionID int not null);

insert into importConfig values ('a')
insert into importConfig values ('b')
insert into importConfig values ('c')

-- Insert into PK-table creating the IDs
INSERT  INTO connection (comment) 
        OUTPUT INSERTED.ConnectionID  INTO @MyConnection
SELECT * from importConfig

-- How do I insert the new IDs together with the ConfigCode? 1 has to be replaced with the ID. 
-- JOIN doesn't seem to work because there is no join condition to use
INSERT INTO connectionImportConfig (connectionID, ConfigCode) 
SELECT 1, ConfigCode FROM ImportConfig 

select * from @MyConnection
select * from connectionImportConfig

-- Cleanup
DROP TABLE importConfig;
DROP TABLE connection;
DROP table connectionImportConfig;

【问题讨论】:

  • 您是否阅读过this answer 关于使用MERGE 命令的内容? OUTPUT 功能可以将数据加载到一个表中,然后将该表的标识添加到现有数据中并将其插入到另一个表中。我已经收藏了一段时间,并在几张桥牌桌上愉快地使用它。
  • 是的,成功了!非常感谢。我以前从未使用过 MERGE 命令...

标签: sql sql-server tsql


【解决方案1】:

看看这个。我相信您可以根据自己的需要对其进行松散的修改。我通常不喜欢直接从输出中插入,但这确实取决于您的数据。希望这个例子有所帮助。

IF OBJECT_ID('tempdb..#ImportConfig') IS NOT NULL DROP TABLE #ImportConfig
IF OBJECT_ID('tempdb..#Config') IS NOT NULL DROP TABLE #Config
IF OBJECT_ID('tempdb..#Connection') IS NOT NULL DROP TABLE #Connection
GO

CREATE TABLE #ImportConfig (ImportConfigID INT PRIMARY KEY IDENTITY(1000,1), ImportConfigMeta VARCHAR(25))
CREATE TABLE #Config (ConfigID INT PRIMARY KEY IDENTITY(2000,1), ImportConfigID INT, ConfigMeta VARCHAR(25))
CREATE TABLE #Connection (ConnectionID INT PRIMARY KEY IDENTITY(3000,1), ConfigID INT, ConnectionString VARCHAR(50))

INSERT INTO #ImportConfig (ImportConfigMeta) VALUES
('IMPORT_ConfigMeta1'),('IMPORT_ConfigMeta2')

;MERGE
INTO    #Config AS T
USING   #ImportConfig AS S
ON      T.ConfigID = S.ImportConfigID
WHEN    NOT MATCHED THEN
        INSERT (ImportConfigID, ConfigMeta) VALUES (
                                                    S.ImportConfigID, 
                                                    REPLACE(S.ImportConfigMeta,'IMPORT_','')
                                                    )
OUTPUT  INSERTED.ConfigID, 'CONNECTION_STRING: ' + INSERTED.ConfigMeta INTO #Connection;

SELECT 'IMPORT CONFIG' AS TableName, * FROM #ImportConfig
SELECT 'CONFIG' AS TableName, * FROM #Config
SELECT 'CONNECTION' AS TableName, * FROM #Connection

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-15
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 2021-11-12
    • 1970-01-01
    • 2014-04-15
    相关资源
    最近更新 更多