| Author |
Topic |
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2007-03-07 : 08:47:58
|
| i have a stored procedure calledspcreatebatch which returns a numberhow can I create a new batch from within another stored procedurewill set @batch=spcreatebatch return the result of spcreatebatch in @batch? |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-03-07 : 09:42:08
|
| create #temp(batchNum int)insert into #tempexec spcreatebatch select batchNum from #tempCausing trouble since 1980blog: http://weblogs.sqlteam.com/mladenp |
 |
|
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2007-03-07 : 09:48:10
|
| i need to do all this just to get the batch number (waht's returned from spcreatebatch) there's no faster way of doing? |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-03-07 : 10:00:55
|
| you can use an output parameter...but that would mean you have to change your spcreatebatch sprocCausing trouble since 1980blog: http://weblogs.sqlteam.com/mladenp |
 |
|
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2007-03-07 : 10:06:15
|
| can you explain - i can chagne thati just want to make this as simple as possible and your code above did not seem to work |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-03-07 : 10:09:18
|
If your SP returns Scalar value through return statement, you can catch it within EXECUTE statement itself.create proc testasreturn 1godeclare @a intexec @a = testselect @a Otherwise, you have no option but to change your SP to use Output parameter or insert results of SP to temp table as spirit1 suggested.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2007-03-07 : 10:16:41
|
| thanks i'll try this -- this should work as i do return the batch id |
 |
|
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2007-03-07 : 10:19:51
|
| this didn't work thoughtmy code for createbatch is (am i doing somethign wrong here -- if i run it - it returns the next batch number)CREATE PROCEDURE [dbo].[spcreatebatch] ASBEGINdeclare @id int -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for procedure here insert into batches(mydate) values(getdate()) set @id=@@IDENTITY SELECT @id AS [id]return |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-03-07 : 10:23:46
|
[code]CREATE PROCEDURE [dbo].[spcreatebatch] ( @out int output)ASBEGINSET NOCOUNT ON;-- Insert statements for procedure hereinsert into batches(mydate) values(getdate())set @out = @@IDENTITYGOdeclare @i intexec [dbo].[spcreatebatch] @i outputselect @i[/code]Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-03-07 : 10:38:04
|
| also use scope_identity() instead of @@identityCausing trouble since 1980blog: http://weblogs.sqlteam.com/mladenp |
 |
|
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2007-03-07 : 10:49:03
|
| alter PROCEDURE [dbo].[spcreatebatch] (@out int output)ASBEGINSET NOCOUNT ON;-- Insert statements for procedure hereinsert into batches(mydate) values(getdate())set @out =scope_identity() GOi get an error Msg 102, Level 15, State 1, Procedure spcreatebatch, Line 9Incorrect syntax near ')'. |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-03-07 : 11:01:05
|
quote: Originally posted by esthera alter PROCEDURE [dbo].[spcreatebatch] (@out int output)ASBEGINSET NOCOUNT ON;-- Insert statements for procedure hereinsert into batches(mydate) values(getdate())set @out =scope_identity() GOi get an error Msg 102, Level 15, State 1, Procedure spcreatebatch, Line 9Incorrect syntax near ')'.
Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
|