sqlserver中取出剛插入的主鍵(主鍵是guid類型)


create PROCEDURE Pro_test
(
  @userName NVARCHAR(25)
  
)

AS

-- select * from [dbo].[Table_2]
--   SELECT * FROM [dbo].[Student]
BEGIN
BEGIN TRAN

 

  DECLARE  @tempTable TABLE(tempId uniqueidentifier)
  DECLARE @tempId uniqueidentifier
  
  INSERT INTO TABLE_2(UserName) OUTPUT Inserted.UserId INTO @tempTable VALUES(@userName) --向第一張表中插入數據
  SELECT @tempId=tempId FROM @tempTable --賦值給中間變量
  INSERT INTO dbo.Student( StuName, StuAge, StuSex,StuCode )VALUES  ( 'zhansan',10,'' ,@tempId )--把中間變量插入到第二張表中

COMMIT TRAN
END

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM