views:

348

answers:

2

SQL Newbie here, and I'm having a hell of a time finding what should be a simple code example to answer what I think is a simple question.

I need to write a stored procedure that does three things in order: 1) Select rows from one table 2) Update rows in another table, using values from the results table in #1 3) Return the results table from #1.

What I can't find is any example about how to return a value like this from a stored procedure. Also, how to retrieve that returned table from the caller (which is another T-SQL script).

A: 

See here - a near exact copy of this question.

i found it by searching SO for "How to return table from T-SQL Stored Procedure"

Paul Sasik
@psasik +1 simply cos someone downvoted what seems like the answer
dove
Thanks Dove. It is a link to an answer and it's also meant to coach a noob to not assume that he/she is the first to ask a particular question. Search SO first... or Google.
Paul Sasik
I saw that post too, but it doesn't work for me. MS SQL Server 2005 tells me that I cannot use an "Insert" or a statement with sideeffects inside a table-valued function. If I change the function to a stored proc I no longer get the errors, but it also doesn't understand the "RETURNS" keyword and I don't know what all the syntax is to change it.
Whiteknight
+4  A: 

Have a look at this.

DECLARE @Table1 TABLE(
     ID INT,
     VAL int
)

INSERT INTO @Table1 (ID,VAL) SELECT 1, 1
INSERT INTO @Table1 (ID,VAL) SELECT 2, 2
INSERT INTO @Table1 (ID,VAL) SELECT 3, 3

DECLARE @Table2 TABLE(
     ID INT,
     VAL VARCHAR(MAX)
)

INSERT INTO @Table2 (ID,VAL) SELECT 1, 1
INSERT INTO @Table2 (ID,VAL) SELECT 2, 2
INSERT INTO @Table2 (ID,VAL) SELECT 3, 3

--Lets say this is the 2 tables


--now this will go into the sp
UPDATE  @Table1
SET  Val = t1.Val + t2.Val
FROM    @Table1 t1 INNER JOIN
     @Table2 t2 ON t1.ID = t2.ID

SELECT  t1.* 
FROM    @Table1 t1 INNER JOIN
     @Table2 t2 ON t1.ID = t2.ID


--and you can insert into a var table in the tsql script that calls the sp

DECLARE @Table1TSQL TABLE(
     ID INT,
     VAL int
)

INSERT INTO @Table1TSQL (ID,VAL) EXEC YourSP
astander
Okay, so what's the syntax inside YourSP that allows me to return a value?
Whiteknight
the rows matching the last select will be returned
CSharpAtl
That's exactly the information I was looking for. Thanks CSharpAtl
Whiteknight
+1, But one small correction: You can't use a table variable to receive the output from an INSERT EXEC statement. It has to be a standard (normal static table) or temp table (#table).
foriamstu
We have done so in sql server 2005, have you tried it? It does not allow insert into ### select , but execs are fine.
astander
I've tried it in SQL server 2005 previously, perhaps there's a version or setting difference between our setups? I'll double-check tomorrow anyway.
foriamstu
K, will recheck and let know
astander
I double checked, and a insert into @table exec sp.I doublke checked the documentation for INSERT INTO from sql server 2005. it states "A table variable, within its scope, can be used as a table source in an INSERT statement."
astander
Ace, thanks for checking. :)
foriamstu
+1, works for me with @table variable on sql server 2005
KM