Click here to Skip to main content
14,775,019 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

i face some issue using SQL trigger. I have three sql tables, User, UserRole and
Role.

User : UserID,UserName,Comment
UserRole : UserRoleID,UserID,RoleID
Role : RoleID, RoleName, Comment

I had UserID and RoleID when row had been inserted in User table.
how do i create trigger to save automatically in UserRole table.
i am totally newbie in trigger. By the way, i am using Linq to SQL class
with MVC3.

Pls, help me !

Best Rgds,
df
Posted
Comments
walterhevedeich 18-Apr-12 22:35pm
   
Are you using stored procedures to insert to User table? If yes, unless its a requirement for you to use triggers, you might want to add the insert to UserRole table to that stored procedure.
dartfrog 19-Apr-12 0:12am
   
I don't use stored procedure. but now i try with it. Thanks
Nathan Stiles 18-Apr-12 23:19pm
   
Like was said above stored procedures are really the best way to go, only slightly more more work in the beginning but you may be thanking yourself later as it lets you basically create an API for your data and helps prevent unintended actions. You need to look up triggers, what they are and how they're intended to be useful.
dartfrog 19-Apr-12 0:13am
   
I'll try with stored procedure. Thanks

1 solution

Hey,

You should create a Trigger into the User table.

CREATE TRIGGER tr_InsertRole
   ON  [User] 
   AFTER  INSERT
AS 
BEGIN
	
	SET NOCOUNT ON;

    --WRITE SQL statement to insert Role into User Role Table
END
GO
   
v3
Comments
dartfrog 19-Apr-12 10:07am
   
Yes .. but how can i pass RoleID and UserID ?
pls enlighten me.
Arav Pradeep Gupta 19-Apr-12 10:25am
   
get the UserID after inserted using the...

delcare @id INT
select @id=UserID from INSERTED

here you got the inserted user ID now the time to get roleID as per role.

INSERT INTO UserRole (UserID,RoleID) values(@id,roleID).

as your stucture it seems all the tables are independent. so need roleId idevisual itself.


TRY..........

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900