Thursday, March 8, 2012

Call stored procedure within SELECT statement

Can this be done? I want to call a stored procedure from inside a select statement. Since you can nest select statements, I thought it might be possible but I have no idea how to do it.

USE NORTHWIND
GO

CREATE TABLE tbA (
Item int NOT NULL,
Value int NOT NULL
) ON [PRIMARY]

GO

INSERT INTO tbA (Item, Value)
SELECT 1, 10 UNION ALL
SELECT 2, 5 UNION ALL
SELECT 3, 2
GO

CREATE PROCEDURE usp_SquareIt

@.iItem int

AS

declare @.iValue int
SELECT @.iValue = Value FROM tbA
SELECT @.iValue * @.iValue AS Result

GO

SELECT Item,
EXECUTE usp_SquareIt Item AS Squared -- can this be done
FROM tbA
GO

DROP TABLE tbA
GO

DROP PROCEDURE usp_SquareIt
GO

Any thoughts?

Mike Bhttp://www.sqlteam.com/item.asp?ItemID=2644 for relevance of the topic.

HTH

No comments:

Post a Comment