Need Some Help Please
-
What i need to do is to Insert a record into a primary table and then insert a corresponding record into the child table.My problem is that the Primary column is an identity column so after inserting into the primary table when i need to insert into the child table i need to know the the primary columns value(which is auto generated by the database) and then use this value to inert into the child table.Any one who faced this problem earlier :^) . i am using sql2005 and c# 3.5. No Data Set Involved in it. Zubair
-
What i need to do is to Insert a record into a primary table and then insert a corresponding record into the child table.My problem is that the Primary column is an identity column so after inserting into the primary table when i need to insert into the child table i need to know the the primary columns value(which is auto generated by the database) and then use this value to inert into the child table.Any one who faced this problem earlier :^) . i am using sql2005 and c# 3.5. No Data Set Involved in it. Zubair
You could use a stored procedure to caputure the primary key of the newly inserted row and have it either return that back to the applicaction, or if you are only inserting one child row then just send the stored proc the child data too and have the stored proc insert the child.
* Developer Day Scotland 2 - Free community conference * The Blog of Colin Angus Mackay
Vogon Building and Loan advise that your planet is at risk if you do not keep up repayments on any mortgage secured upon it. Please remember that the force of gravity can go up as well as down.
-
What i need to do is to Insert a record into a primary table and then insert a corresponding record into the child table.My problem is that the Primary column is an identity column so after inserting into the primary table when i need to insert into the child table i need to know the the primary columns value(which is auto generated by the database) and then use this value to inert into the child table.Any one who faced this problem earlier :^) . i am using sql2005 and c# 3.5. No Data Set Involved in it. Zubair
Oh, and to get the last inserted identity value generated use
SELECT SCOPE_IDENTITY()
Note: It must be run in the same scope as the original SQL.
* Developer Day Scotland 2 - Free community conference * The Blog of Colin Angus Mackay
Vogon Building and Loan advise that your planet is at risk if you do not keep up repayments on any mortgage secured upon it. Please remember that the force of gravity can go up as well as down.
-
What i need to do is to Insert a record into a primary table and then insert a corresponding record into the child table.My problem is that the Primary column is an identity column so after inserting into the primary table when i need to insert into the child table i need to know the the primary columns value(which is auto generated by the database) and then use this value to inert into the child table.Any one who faced this problem earlier :^) . i am using sql2005 and c# 3.5. No Data Set Involved in it. Zubair
Hello Zubair, There are two solutions to solve your problem 1. Either you can use stored procedure or, 2. If you have programming exprience then solve your problem as follow. **First insert record in Primary table, so primary column will autoincrement its value.**Eg. suppose u r inserting student_name 'XYZ' in student table and stud_id is primary column. Now, use select query to find the value of stud_id from student table by using the name of student that u hav just inserted. eg. SELECT stud_id FROM STUDENT WHERE student_name = 'XYZ' Now, u hav obtained stud_id which was autoincremented. **Now, use this value to insert it in child table.**eg. suppose u r inserting it in table studentdetails table INSERT INTO studentdeatails (stud_id,average) VALUES (stud_id, 70) Ok... hope this will help u. Good luck
-
Hello Zubair, There are two solutions to solve your problem 1. Either you can use stored procedure or, 2. If you have programming exprience then solve your problem as follow. **First insert record in Primary table, so primary column will autoincrement its value.**Eg. suppose u r inserting student_name 'XYZ' in student table and stud_id is primary column. Now, use select query to find the value of stud_id from student table by using the name of student that u hav just inserted. eg. SELECT stud_id FROM STUDENT WHERE student_name = 'XYZ' Now, u hav obtained stud_id which was autoincremented. **Now, use this value to insert it in child table.**eg. suppose u r inserting it in table studentdetails table INSERT INTO studentdeatails (stud_id,average) VALUES (stud_id, 70) Ok... hope this will help u. Good luck
sourabhsorate wrote:
There are two solutions to solve your problem
There are many more than that.
sourabhsorate wrote:
use select query to find the value of stud_id from student table by using the name of student that u hav just inserted. eg. SELECT stud_id FROM STUDENT WHERE student_name = 'XYZ'
What happens if you have two students called "John Smith" - It's a very common name and there are several in my phone book, some of them could even attend the same university.
* Developer Day Scotland 2 - Free community conference * The Blog of Colin Angus Mackay
Vogon Building and Loan advise that your planet is at risk if you do not keep up repayments on any mortgage secured upon it. Please remember that the force of gravity can go up as well as down.