zouzou
zouzou

Reputation: 41

Get last identity of table and insert to new table

I'm trying to get the last identity from my table 'usergold' and insert it in a new table table with insert value. I used select "IDENT_CURRENT('UserGold') As userid", new value should be in userid.

But I'm getting an error when I put it in insert, I cannot create the trigger. Please check my code.

create trigger addrole
on UserGold
after Insert
as 
Begin

select  IDENT_CURRENT('UserGold') As userid
insert into AspNetUserRoles values(userid,'2c258e8d-c648-4b38-9b01-989d4dd525fe')
end

i tried

create trigger addrole
on UserGold
after Insert
as 
Begin
declare
@userid nvarchar(50)
select @userid=userId from UserGold where userId=IDENT_CURRENT('UserGold')
insert into AspNetUserRoles(UserId,RoleId) values(@userid,'2c258e8d-c648-4b38-9b01-989d4dd525fe')
end 

igot this error "Error converting data type nvarchar to numeric " from select @userid=userId from UserGold where userId=IDENT_CURRENT('UserGold')

Upvotes: 0

Views: 53

Answers (1)

critical_error
critical_error

Reputation: 6716

Your INSERT statement is not written correctly.

Try

INSERT INTO AspNetUserRoles ( [col_1_name], [col_2_name] )
VALUES (
    ( SELECT IDENT_CURRENT ( 'UserGold' ) ),
    '2c258e8d-c648-4b38-9b01-989d4dd525fe'
);

Note: Replace the column holder names ( col_1_name, col_2_name ) with the actual column names being inserted to.

Upvotes: 0

Related Questions