久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

插入后如何在不插入相關數據的情況下獲取插入

How do I get inserted row Id, along with related data back after insert without inserting the related data(插入后如何在不插入相關數據的情況下獲取插入的行 ID 以及相關數據) - IT屋-程序員軟件開發技術分享社
本文介紹了插入后如何在不插入相關數據的情況下獲取插入的行 ID 以及相關數據的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一組數據需要在表中生成一個新行.創建此行后,我需要在與此信息相關的單獨表中附加元數據.也就是說,我需要先創建我的 [Identity],從行中取回 GlobalId,然后附加 [Accounts]>[元數據]到它.

I have a set of data which need to result in a new row in a table. Once this row is created I need to attach metadata in separate tables related to this information. That is I need to create my [Identity] first, get the GlobalId back from the row, and then attach [Accounts] and [Metadata] to it.

插入數據并獲取插入行的 ID 很容易(請參閱下面的查詢).但是我很困惑如何將 personnumber、firstnamelastname 插入到這個臨時表中,以便我可以繼續插入相關數據.

Inserting data and getting the Id of the inserted row is easy enough (see query below). But I'm stumped as to how I get the personnumber, firstname, and lastname inserted into this temporary table as well so I can continue with inserting the related data.

DECLARE @temp AS TABLE(
      [GlobalId] BIGINT
     ,[Personnumber] NVARCHAR(100)
     ,[Firstname] NVARCHAR(100)
     ,[Lastname] NVARCHAR(100)
);

;WITH person AS
(
    SELECT top 1
        t.[Personnumber]
        ,t.[Firstname]
        ,t.[Lastname]
    FROM [temp].[RawRoles] t
    WHERE t.Personnumber NOT IN 
        (
            SELECT i.Account FROM [security].[Accounts] i
        )
)
INSERT INTO [security].[Identities] ([Created], [Updated])
-- how do i get real related values here and not my hard coded strings?
OUTPUT inserted.GlobalId, 'personnumber', 'firstname', 'lastname' INTO @temp 
SELECT GETUTCDATE(), GETUTCDATE()
FROM person

附言背景故事.對我來說,身份只是我們將使用的全局 ID 的持有者,而不是其他系統中的實際個人號碼(相當于社會安全號碼),這樣只有一個位置具有敏感號碼,并且可以關聯多個帳戶標識,例如社會安全編號或 AD 帳戶到相同的全局 ID.

P.S. Backstory. Identities for me is just a holder of a global Id we will be using instead of actual personal numbers (equivalent of social security numbers) in other systems, this way only one location has sensitive numbers, and can relate multiple account identifications such as social security number or AD accounts to the same global id.

P.P.S 我更愿意避免使用游標,因為查詢在第一次運行時將移動近 200 萬條記錄,每天移動數千條記錄.

P.P.S I would prefer to avoid Cursors as the query is going to be moving around almost 2 million records on first run, and several thousand on a daily basis.

推薦答案

@PeterHe 給了我一個關于如何使用 MERGE

@PeterHe gave me an idea on how to solve this with MERGE

按如下方式工作.插入所有行后,我可以查詢 @temp 以繼續其余的插入.

Got it working as follows. When all rows have been inserted I can query @temp to continue the rest of the inserts.

DECLARE @temp AS TABLE(
      [action] NVARCHAR(20)
     ,[GlobalId] BIGINT
     ,[Personnumber] NVARCHAR(100)
     ,[Firstname] NVARCHAR(100)
     ,[Lastname] NVARCHAR(100)
);

;WITH person AS
(
    SELECT top 1
        t.[Personnumber]
        ,t.[Firstname]
        ,t.[Lastname]
    FROM [temp].[RawRoles] t
    WHERE t.Personnumber NOT IN 
        (
            SELECT i.Account FROM [security].[Accounts] i
        )
)

MERGE [security].[Identities] AS tar
USING person AS src
ON 0 = 1 -- all rows from src need to be inserted, ive already filtered out using CTE Query.
WHEN NOT MATCHED THEN
   INSERT
   (
     [Created], [Updated]
   )
   VALUES
   (
        GETUTCDATE(), GETUTCDATE()
   )
OUTPUT $action, inserted.GlobalId, src.[Personnumber], src.[Firstname], src.[Lastname]  INTO @temp;


SELECT * FROM @temp

這篇關于插入后如何在不插入相關數據的情況下獲取插入的行 ID 以及相關數據的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 国内精品视频在线观看 | 欧美一区二区网站 | 亚洲天堂影院 | 7777久久 | 国产在线高清 | 一级毛片视频 | 国产aⅴ爽av久久久久久久 | 日韩有码一区 | 午夜精品| www.国产精品 | 亚洲高清在线 | 91色视频在线观看 | 粉嫩国产精品一区二区在线观看 | 欧洲一区二区视频 | 久久中文字幕一区 | 91免费入口| 免费观看成人性生生活片 | 久久精品—区二区三区 | 精品一区二区三区在线观看国产 | 97视频在线观看免费 | 日韩欧美国产成人一区二区 | 久久精品国产一区二区电影 | 中文字幕在线观看av | 亚洲网站在线播放 | 99精品免费| 精品国产乱码久久久久久丨区2区 | 久久毛片 | 欧美美女一区二区 | 国产精品一卡二卡三卡 | 成人精品鲁一区一区二区 | 国产乱一区二区三区视频 | 欧美专区在线 | 国产视频第一页 | 亚洲啊v在线 | 一区二区三区久久 | 国产www.| 久久久久中文字幕 | 97在线超碰 | 日韩a | 久久久精品综合 | 亚洲高清在线 |