I have complaint table
1. tblProfile with columns
userId | name | age | address | mobileno |
2. tblUserId with columns userId | role | status
now when user fills the form I want to insert one row in tblProfile, before inserting a new row I want to create userId by combining starting letters of name and mobile no and then insert into tblprofile with userId after this I want to insert that UserId into tblUserId table.
for this I have to use two triggers one is before insert trigger and another is after insert trigger.but I dont know how to capture user information to create userId and how to pass that Id to second trigger.
Since you’re acting upon
INSERTs, try#insertedorinsertedas the container of your inserting values.I have not worked with triggers for a long time now, but this should help you get what you need.
Please have an eye out this link: Using the inserted and deleted Tables.
However, you already have all the tools on the application-side as you have the information and the capability to work with the in-memory information data.
EDIT #1
In this particular situation, I see more suitable to process with a stored procedure than two independant triggers. We have interdependent information data here (userId). I do think the simplest way would be a stored procedure. It is adviseable to wrap these operation within a transaction scope, as if either insert fails, both won’t be applied, assuring data integrity.
However, if you do prefer to go with triggers, then the alternative would be to insert the concatenated value for userId into a temporary table. You then should have a DDL looking as follows:
Then, within the first trigger, you would have to set the value of a @userId variable to contain you concatenated userId, then use it to insert into tblProfile, then perform a second insert into @UserIdTempTable.
Then, you may select it from your second trigger.
This is abosolute hard hand-work. Processing through triggers is not adviseable here, because within the insertion of tblProfile, you do not have information data required by tblUserId, so you have to have two
DbCommandand launch twoExecuteNonQuery()in a row. That is a lot of overhead for such a tiny task. Then, it would be more aviseable to process with the stored procedure as suggested, plus it assures data integrity by the DBMS itself, instead of simulating it through a @@ROWCOUNT verification.I do hope this helps! =)