We help IT Professionals succeed at work.

Copy Alias to Output Parameter

I feel like this is a simple question but I dont have the knowledge of SQL to answer it so I figured I would let you guys take a look at it.  I have a SP that contains and output parameter named "@gas1".  I also have a select statement that does some calculations and assigns the result to an alias, "gas1".  How do I copy the result from the alias, "gas1", to the output parameter, "@gas1"???
ALTER PROCEDURE [dbo].[Urea_PressFactors_Interp] 
	-- Add the parameters for the stored procedure here
	@InputPress float = 0,
	@gas1 float output,
	@liq1 float output
	

AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
SELECT ((@Inputpress - derivedtbl_1.psi1) / (derivedtbl_2.psi2 - derivedtbl_1.psi1)) * (derivedtbl_2.gas2 - derivedtbl_1.gas1) + derivedtbl_1.gas1 AS gas1
	FROM   (SELECT     PSI AS psi1, GAS AS gas1
	FROM          Urea_SC_PressFactors_3_4Spheres
	WHERE      (PSI = (SELECT MAX(PSI) AS Expr1
	FROM          Urea_SC_PressFactors_3_4Spheres AS Urea_SC_PressFactors_3_4Spheres_3
	WHERE      (PSI <= @InputPress)))) AS derivedtbl_1 CROSS JOIN
	(SELECT     PSI AS psi2, GAS AS gas2
	FROM          Urea_SC_PressFactors_3_4Spheres AS Urea_SC_PressFactors_3_4Spheres_2
	WHERE      (PSI =(SELECT MIN(PSI) AS Expr1
	FROM          Urea_SC_PressFactors_3_4Spheres AS Urea_SC_PressFactors_3_4Spheres_1
	WHERE      (PSI >= @Inputpress)))) AS derivedtbl_2

Open in new window

Comment
Watch Question

Commented:
Change this line to
SELECT ((@Inputpress - derivedtbl_1.psi1) / (derivedtbl_2.psi2 - derivedtbl_1.psi1)) * (derivedtbl_2.gas2 - derivedtbl_1.gas1) + derivedtbl_1.gas1 AS gas1
      FROM   (SELECT     PSI AS psi1, GAS AS gas1


This:
SELECT @gas1 = ((@Inputpress - derivedtbl_1.psi1) / (derivedtbl_2.psi2 - derivedtbl_1.psi1)) * (derivedtbl_2.gas2 - derivedtbl_1.gas1) + derivedtbl_1.gas1 AS gas1
      FROM   (SELECT     PSI AS psi1, GAS AS gas1
Máté FarkasDatabase Developer and Administrator

Commented:
brad2575's solution is correct but you should warn that in this case the procedure will not return any rows. If you want to get resultset too you have to insert the original select statement again somewhere into the procedure

Author

Commented:
I am getting the following errors when I execute the query.

Msg 156, Level 15, State 1, Procedure Urea_PressFactors_Interp, Line 20
Incorrect syntax near the keyword 'AS'.
Msg 156, Level 15, State 1, Procedure Urea_PressFactors_Interp, Line 25
Incorrect syntax near the keyword 'AS'.
Msg 156, Level 15, State 1, Procedure Urea_PressFactors_Interp, Line 30
Incorrect syntax near the keyword 'AS'.

Any ideas?

Author

Commented:
I got it figured out.  I had to remove the "AS gas1" from the end of the first select command after making the changes suggested by Brad.  Thx
Raja Jegan RSQL Server DBA & Architect, EE Solution Guide
Awarded 2009
Distinguished Expert 2019

Commented:
Fixed it, slightly tuned as well
ALTER PROCEDURE [dbo].[Urea_PressFactors_Interp] 
	-- Add the parameters for the stored procedure here
	@InputPress float = 0,
	@gas1 float output,
	@liq1 float output
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
SELECT ((@Inputpress - sub1.psi1) / (sub2.psi2 - sub1.psi1)) * (sub2.gas2 - sub1.gas1) + sub1.gas1 gas1
FROM ( SELECT MAX(PSI) psi1, GAS gas1
	   FROM Urea_SC_PressFactors_3_4Spheres
	   WHERE PSI <= @InputPress
       GROUP BY GAS) sub1 
       CROSS JOIN
	 ( SELECT MIN(PSI) psi2, GAS gas2
	   FROM Urea_SC_PressFactors_3_4Spheres
	   WHERE PSI >= @Inputpress
       GROUP BY GAS) sub2
END
GO

Open in new window