Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
ujjwal
Starting Member
4 Posts |
Posted - 2006-09-21 : 04:08:43
|
| Hi all,I have 2 tables like thatCREATE TABLE tab1 ( id int IDENTITY (1, 1) NOT NULL , master varchar (50) NULL ) ON PRIMARYGOCREATE TABLE tab2 ( id int IDENTITY (1, 1) NOT NULL , student_name varchar (50) NULL , master_id int NULL )Now i want to create a Spwhich iscreate proc inner_sp(@s1 varchar(50))asinsert into tab1(master) values (@s1)and i want to call it from another spcreate proc outer_sp2(@s2 varchar(50),@s1 varchar(50))asdeclare @r intexec inner_sp @s1set @r=Scope_Identity()insert into tab2(student_name,master_id) values(@s2,@r)exec outer_sp2 'Vivek1','Test'But in table tab2 master_id column contain NULL value....Please fix it...Ujjwal |
|
|
ujjwal
Starting Member
4 Posts |
Posted - 2006-09-21 : 04:11:04
|
| problem is that in table tab2 contain master_id NULL .It should contain the last inserted id(integer value) of Table tab1. |
 |
|
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2006-09-21 : 04:15:54
|
Welcome to SQLTeam,i would do it like thus;create proc inner_sp(@s1 varchar(50))asinsert into tab1(master) values (@s1)set @r=Scope_Identity()insert into tab2(student_name,master_id) values(@s2,@r)exec outer_sp2 'Vivek1','Test' you dont need the second SPAfrika |
 |
|
|
ujjwal
Starting Member
4 Posts |
Posted - 2006-09-21 : 04:17:58
|
| No sir I did That but master_id column of tab2 table insert 'NULL' value. |
 |
|
|
ujjwal
Starting Member
4 Posts |
Posted - 2006-09-21 : 04:28:47
|
| Sorry africa,My primary job 2 call store procedure from another store procedure.and in this case my inserted value in tab2 table is 'NULL' in the master_id column .Which should insert the auto increment id valueof tab1 table. |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2006-09-21 : 07:35:53
|
get the last inserted identity value with scope_identity() in the first sproc and pass that as a paramter to the second sprocGo with the flow & have fun! Else fight the flow blog thingie: http://weblogs.sqlteam.com/mladenp |
 |
|
|
|
|
|
|
|