Question : store values from stored procedure in variables

Hi everyone,

I have a stored procedures that returns a recordset with multiple fields, but only one row. Btw, I am using Sql Server 2005.

I would like to call this SP from another SP and reuse its output there, by storing it in different variables. The only way I can successfully do that is to create a temporary table with the same columns as the ones returned by the original SP, then fill that temporary table with the original SP output, like this:

INSERT INTO #mytbl(field1, field2, field3)
EXECT spMyOriginalSP @par1, @par2

Then I would assign each of the temporary table fields to variables in the new SP, like this:

SELECT
   @var1 = field1,
   @var2 = field,
   @var3 = field3
FROM #mytbl

Is there a way to bypass the temporary table creation, and directly store the original SP output into variables in the new SP?

Hope I was able to explain myself there... :-)

Thanks in advance for your help!

Answer : store values from stored procedure in variables

look at this
http://www.sommarskog.se/share_data.html

Also, if you can make it a function instead of a SP, you can return a table variable.

declare @tablename sysname
declare @colname sysname

select top 1 @tablename = tablename, @colname = ColumnName FROM dbo.TestTableList()
select @tablename, @colname
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
CREATE FUNCTION TestTableList ( 
) RETURNS @TableList TABLE (
  ID INT IDENTITY(1, 1),
  TableName SYSNAME,
  ColumnName SYSNAME
) AS BEGIN  
	INSERT INTO @TableList
	SELECT TABLE_NAME, COLUMN_NAME
	FROM INFORMATION_SCHEMA.COLUMNS
	
	RETURN 
END
Random Solutions  
 
programming4us programming4us