Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server 2012 scope_identity advise

I created a stored procedure in SQL Server 2012 and I have used scope_identity to get identity column's value but in my case I do not know is this correct or not please help

CREATE PROCEDURE Add_Translation
    @english nvarchar(70),
    @kurdish nvarchar(70),
    @english_category int,
    @kurdish_category int,
    @result int out
AS
BEGIN
    SET NOCOUNT ON;

    if @english is not null and @kurdish is not null and @english_category is not null and @kurdish_category is not null
    begin
        insert into english_table values(@english, @english_category);

        declare @identityEnglish int;
        set @identityEnglish = SCOPE_IDENTITY();

        insert into kurdish_table values(@kurdish, @kurdish_category);
        declare @identityKurdish int;
        set @identityKurdish = SCOPE_IDENTITY();

        insert into transactions values(@identityEnglish, @identityKurdish);
        set @result = 1;
    end
    else
    begin
        set @result = 0;
    end
END

My question is that will the variable @identityEnglish get last identity value of english_table and variable @identityKurdish get last identity value of kurdish_table

thank you..

like image 241
danarj Avatar asked Jan 01 '26 20:01

danarj


1 Answers

If the inserts succeed then scope_identity() works correctly.

like image 96
Jimbo Avatar answered Jan 03 '26 13:01

Jimbo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!