SQL插入到…(选择*,SCOPE_IDENTITY() FROM…)

时间:2021-10-03 22:23:29

I created a stored procedure that should do 2 inserts in one

我创建了一个存储过程,它应该在一个过程中进行两次插入

First step I want to create a new entry per insert into.

第一步,我想为每个插入创建一个新条目。

Second step I will catch the created Id from this entry

第二步,我将从这个条目中捕获创建的Id。

Third step I want to copie multiple entries per select from one table and insert those to the same table with the Id

第三步,我想从一个表中每个选择复制多个条目,并将它们插入到具有Id的同一个表中

Create PROCEDURE dbo.Rolle_Copie
    @Id as int,
    @newId as int = 0,
    @Name AS nvarchar(50)
AS
    INSERT INTO Rollen (RolleName) 
    VALUES (@Name)

    @newId = SCOPE_IDENTITY()

    INSERT INTO Berechtigung_Rolle (RefRolleId,RefBerechtigungId)
        SELECT   
           RefBerechtigungId, @newId 
        FROM     
           Berechtigung_Rolle 
        WHERE    
           RefRolleId = @Id

    RETURN

but I get an error

但是我得到了一个错误

Wrong syntax next to @newId

@newId旁边的语法错误

Could somebody please enlight me what's wrong?

有人能告诉我怎么回事吗?

Any advice is greatly appreciated

非常感谢您的建议

1 个解决方案

#1


7  

don't forget to use SET

不要忘记使用SET

SET @newId = SCOPE_IDENTITY()

#1


7  

don't forget to use SET

不要忘记使用SET

SET @newId = SCOPE_IDENTITY()