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 |
|
nmarks
Yak Posting Veteran
53 Posts |
Posted - 2008-01-02 : 12:00:20
|
| I cannot see the wood for the trees. Please help!I have written a large application and my boss requires version control for every table, procedure and function.I intend to use sp_executesql, the built function, to allow me to call procedures by the name held in a variable.For example : Execute @version_of_product_pricingwhere the value of @version_of_sp_some_proc might be sp_some_proc_1_0_0 this week but change to sp_some_proc_1_0_1 next week.The problem I'm having is accessing the result variable in some of the procedures.The simplest example is this:ALTER PROCEDURE [dbo].[sp_maximum]( @A float, @B float, @max float output)ASBEGINIF @A>=@B BEGIN set @max = @A ENDELSE BEGIN set @max = @B ENDENDI'n trying to return the @max value from this procedure using the sp_executesql method using the following technique:ALTER PROCEDURE [dbo].[Test_Multiple_Params]ASBEGINDECLARE @intA float;DECLARE @intB float;DECLARE @intmax float;DECLARE @result float;DECLARE @SQLString nvarchar(500);DECLARE @ParmDefinition nvarchar(500);SET @SQLString = N'EXEC sp_maximum @A , @B, @max';SET @ParmDefinition = N'@A FLOAT, @B FLOAT, @max FLOAT OUTPUT';SET @intA=1;SET @intB=2;SET @intmax = 0;EXECUTE sp_executesql @SQLstring, @ParmDefinition, @A = @intA, @B = @intB, @max = @intmax output;SELECT @intmaxENDThis produces an output of zero, which of course it shouldn't.Can you see the error? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-02 : 12:27:11
|
quote: Originally posted by nmarks I cannot see the wood for the trees. Please help!I have written a large application and my boss requires version control for every table, procedure and function.I intend to use sp_executesql, the built function, to allow me to call procedures by the name held in a variable.For example : Execute @version_of_product_pricingwhere the value of @version_of_sp_some_proc might be sp_some_proc_1_0_0 this week but change to sp_some_proc_1_0_1 next week.The problem I'm having is accessing the result variable in some of the procedures.The simplest example is this:ALTER PROCEDURE [dbo].[sp_maximum]( @A float, @B float, @max float output)ASBEGINIF @A>=@B BEGIN set @max = @A ENDELSE BEGIN set @max = @B ENDENDI'n trying to return the @max value from this procedure using the sp_executesql method using the following technique:ALTER PROCEDURE [dbo].[Test_Multiple_Params]ASBEGINDECLARE @intA float;DECLARE @intB float;DECLARE @intmax float;DECLARE @result float;DECLARE @SQLString nvarchar(500);DECLARE @ParmDefinition nvarchar(500);SET @SQLString = N'EXEC sp_maximum @A , @B, @max';SET @ParmDefinition = N'@A FLOAT, @B FLOAT, @max FLOAT OUTPUT';SET @intA=1;SET @intB=2;SET @intmax = 0;EXECUTE sp_executesql @SQLstring, @ParmDefinition, @A = @intA, @B = @intB, @max = @intmax output;SELECT @intmaxENDThis produces an output of zero, which of course it shouldn't.Can you see the error?
Not sure if its the problem. But can you try removing the assignment statement in blue. |
 |
|
|
nmarks
Yak Posting Veteran
53 Posts |
Posted - 2008-01-02 : 12:36:57
|
| Thanks for trying.I commented out the statement, ran the procedure and got NULL (presumably because @intmax hasn't been initialised).I also tried changing 'SELECT @intmax' for 'SELECT @max' and got the error 'Must declare the scalar variable "@max"'. |
 |
|
|
jdaman
Constraint Violating Yak Guru
354 Posts |
Posted - 2008-01-02 : 17:16:43
|
quote: Originally posted by nmarks Thanks for trying.I commented out the statement, ran the procedure and got NULL (presumably because @intmax hasn't been initialised).I also tried changing 'SELECT @intmax' for 'SELECT @max' and got the error 'Must declare the scalar variable "@max"'.
sp_maximum doesnt return @max. Try altering it with this:ALTER PROCEDURE [dbo].[sp_maximum](@A float,@B float)ASBEGINDECLARE @max floatIF @A>=@B set @max = @AELSE set @max = @BSELECT @maxEND |
 |
|
|
jdaman
Constraint Violating Yak Guru
354 Posts |
Posted - 2008-01-02 : 17:23:25
|
| You will also need to alter the second sproc:ALTER PROCEDURE [dbo].[Test_Multiple_Params]ASBEGINDECLARE @intA float;DECLARE @intB float;/*DECLARE @intmax float;*/DECLARE @result float;DECLARE @SQLString nvarchar(500);DECLARE @ParmDefinition nvarchar(500);SET @SQLString = N'EXEC sp_maximum @A , @B';SET @ParmDefinition = N'@A FLOAT, @B FLOAT';SET @intA=1;SET @intB=2;/*SET @intmax = 0;*/EXECUTE sp_executesql @SQLstring, @ParmDefinition, @A = @intA, @B = @intB/*, @max = @intmax output*/;/*SELECT @intmax*/END |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-01-02 : 18:36:59
|
1. Why do you need sp_executesql to execute your sp_maximum ?You don't need that at all. A simple direct exec is sufficientALTER PROCEDURE [dbo].[Test_Multiple_Params]ASBEGIN DECLARE @intA float; DECLARE @intB float; DECLARE @intmax float; DECLARE @result float; DECLARE @SQLString nvarchar(500); DECLARE @ParmDefinition nvarchar(500); SET @SQLString = N'EXEC sp_maximum @A , @B, @MAX'; SET @ParmDefinition = N'@A float, @B float, @MAX float OUTPUT'; SET @intA=1; SET @intB=2; SET @intmax = 0; EXECUTE sp_executesql @SQLstring, @ParmDefinition, @A = @intA, @B = @intB, @MAX = @intmax OUTPUT; EXEC sp_maximum @A = @intA, @B = @intB, @MAX = @intmax OUTPUT SELECT @intmaxEND KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-01-02 : 18:38:29
|
This is what you missed out in your stored procedure
ALTER PROCEDURE [dbo].[Test_Multiple_Params]ASBEGIN DECLARE @intA float; DECLARE @intB float; DECLARE @intmax float; DECLARE @result float; DECLARE @SQLString nvarchar(500); DECLARE @ParmDefinition nvarchar(500); SET @SQLString = N'EXEC sp_maximum @A , @B, @MAX OUTPUT'; SET @ParmDefinition = N'@A float, @B float, @MAX float OUTPUT'; SET @intA=1; SET @intB=2; SET @intmax = 0; EXECUTE sp_executesql @SQLstring, @ParmDefinition, @A = @intA, @B = @intB, @MAX = @intmax OUTPUT; SELECT @intmaxEND KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|
|
|
|
|