hi all,
In sql server 2005 i had created 2 tables,table 1 and table 2. Here is the detail of the table.
table 1:
tid--> int,identity,primary key
tname-->varchar(200)
table 2:
sid-->int,identity,primary key
tid-->fk (this tid is set as foreign key for the tid in table1)
now when i'm inserting values into tname i have to insert the value of tid from table 1 into the tid of table 2 both at the same time. any one know how this is possible? if so please send me the code..
pls help me..
thanks
swapna
Go For Stored Procedure for Insert...
SP Flow should be-
1. Start SQL Transaction
2. Insert into Table1
3. Get the Inserted INDENTITY value.
4.Insert into table 2
5. Commit Transaction Or Rollback transaction depending on the Error .
You can create a stored procedure. There use will insert the record in the table 1 first and fetch the latest generated id value in table 1 using scope_identity and store it in a variable. Then you will insert the corresponding record in table 2 using the variable value.
For help on how to call a stored procedure from code visit http://forums.asp.net/t/1165758.aspx.
Feel free to ask for more help on this issue.
Or you can create a trigger to insert the row into the second table.
It if must always happen the same way, the trigger would be a safer bet.
check out create trigger in the documentation.
sql
No comments:
Post a Comment