Thursday, March 22, 2012
Calling nested stored proedure many times
create procedure nestedSP
@.p float
as
declare @.q float
set @.q = @.p
select @.q
go
create procedure CallingNestedSP
@.p float
as
exec nestedSP @.p
go
exec CallingNestedSP 111.11
How do I get the resultset as 111.11, 111.11 by calling the nexted twice.
Please advise. Thanks
PeterHi Peter
There is no best way of joining the two results from SPs ...
You can maybe try to create the called procedure as a UDF and
call it twice.Or if you only have to use a SP,
then insert the result into a temp table. Your calling SP will have the code
like this..
create procedure CallingNestedSP
@.p float
as
begin
create table #temp (a decimal(...))
INsert into #temp
exec nestedSP @.p
select a,a from #temp
end..
Though I would prefer a udf for a situation similar to yours.
--
-Omnibuzz (The SQL GC)
http://omnibuzz-sql.blogspot.com/
Sunday, March 11, 2012
Calling a stored procedure inside another stored procedure (or "nested stored procedures")
Hi all - I'm trying to optimized my stored procedures to be a bit easier to maintain, and am sure this is possible, not am very unclear on the syntax to doing this correctly. For example, I have a simple stored procedure that takes a string as a parameter, and returns its resolved index that corresponds to a record in my database. ie
exec dbo.DeriveStatusID 'Created'
returns an int value as 1
(performed by "SELECT statusID FROM statusList WHERE statusName= 'Created')
but I also have a second stored procedure that needs to make reference to this procedure first, in order to resolve an id - ie:
exec dbo.AddProduct_Insert 'widget1'
which currently performs:
SET @.statusID = (SELECT statusID FROM statusList WHERE statusName='Created')
INSERT INTO Products (productname, statusID) VALUES (''widget1', @.statusID)
I want to simply the insert to perform (in one sproc):
SET @.statusID = EXEC deriveStatusID ('Created')
INSERT INTO Products (productname, statusID) VALUES (''widget1', @.statusID)
This works fine if I call this stored procedure in code first, then pass it to the second stored procedure, but NOT if it is reference in the second stored procedure directly (I end up with an empty value for @.statusID in this example).
My actual "Insert" stored procedures are far more complicated, but I am working towards lightening the business logic in my application ( it shouldn't have to pre-vet the data prior to executing a valid insert).
Hopefully this makes some sense - it doesn't seem right to me that this is impossible, and am fairly sure I'm just missing some simple syntax - can anyone assist?
Two steps:
1. Create the SP to return new ID like:
CREATE PROCEDURE [dbo].[deriveStatusID] @.Created Varchar(100),@.statusIDint OUTPUT
AS
SELECT@.statusID =statusID FROM statusList WHEREstatusName=@.Created
2. Modify your second SP as:
....
Declare @.NewStatusId Int
EXECderiveStatusID 'Created', @.NewStatusId OUTPUT
INSERT INTO Products (productname, statusID) VALUES (''widget1',@.NewStatusId)
calling a nested procedure
Everytime I execute this script - I am not getting the result I want. I was hoping to take the some parameters call one stored procedufre with in the main procedure. I was hoping to assign a variable to the nested procedure and use that variable in the main procedure, but I am getting a '0' as result - however when I call the a inner procedure by itself if get the correct result. Can someone look at this and tell me where I am wrong? It seems to be a scope problem.
Thanks
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:
-- Create date: September 8,2006
-- Description: Inserts a new project
-- =============================================
ALTER PROCEDURE [echo88].[insertProject]
-- Add the parameters for the stored procedure here
@.name varchar(50),
@.desc varchar(MAX),
@.start datetime,
@.finish datetime,
@.memid int,
@.addr1 varchar(32),
@.addr2 varchar(16),
@.city varchar(32),
@.stateid int,
@.zipcode char(5)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @.siteid int;
EXEC @.siteid = insertSite @.addr1, @.addr2, @.city, @.stateid, @.zipcode;
-- Insert statements for procedure here
INSERT INTO Project(Title, Description, Start, Finish, SiteID, MemberID ) VALUES ( @.name, @.desc, @.start, @.finish, @.siteid, @.memid );
END
SQL Server currently uses return values 0 through -14 to indicate the execution status of stored procedures. Have you defined RETURN values in the 'insertSite' stored procedure? If not, by default it will return 0 for successful execution, so you'll always get 0 for @.siteid. In this case I suggest you use OUTPUT parameter in the stored procedure, for example:
ALTER PROCEDURE [echo88].[insertSite]
-- Add the parameters for the stored procedure here
@.siteid int output
--add other parameters
AS
BEGIN
--add your code
SET @.siteid=1--set the value of the output parameter
END
Then when call it a another procedure:
ALTER PROCEDURE [echo88].[insertProject]
-- Add the parameters for the stored procedure here
@.name varchar(50),
@.desc varchar(MAX),
@.start datetime,
@.finish datetime,
@.memid int,
@.addr1 varchar(32),
@.addr2 varchar(16),
@.city varchar(32),
@.stateid int,
@.zipcode char(5)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @.siteid int;
EXEC insertSite @.addr1, @.addr2, @.city, @.stateid, @.zipcode,@.siteid output;
-- Insert statements for procedure here
INSERT INTO Project(Title, Description, Start, Finish, SiteID, MemberID ) VALUES ( @.name, @.desc, @.start, @.finish, @.siteid, @.memid );
END