Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

EXECUTE cannot be used as a source when inserting into a table variable.

Posted on 2004-10-15
6
Medium Priority
?
3,448 Views
Last Modified: 2011-09-20
Hi Experts,

I am getting an error in T-SQL while I am trying to get the results of one stored procedure into a table.

The code that gives error is,

DECLARE @RetTable TABLE(ReportHCEntityID int, DataElementID int, ElementValue varchar(50))

INSERT @RetTable
      EXEC dbo.spSelectReportDataForMetric @MetricID, @DataElement1, @DataElement2

Where dbo.spSelectReportDataForMetric is another stored procedure. Does this mean I cannot do something like this. Do I have to use user defined functions?

I saw an example in SQL Server Online Books. I am trying to locate that example again. But I am pretty sure example was inserting result of execute into the table.

Thanks a lot

Dharmesh
0
Comment
Question by:dharmesh_amity
  • 3
  • 2
6 Comments
 
LVL 6

Accepted Solution

by:
acampoma earned 800 total points
ID: 12320196
nope, Posted in online help
cannot insert stored proc results into a table var

see under table variable in online help under remarks:
A table variable behaves like a local variable. It has a well-defined scope, which is the function, stored procedure, or batch in which it is declared.
Within its scope, a table variable may be used like a regular table. It may be applied anywhere a table or table expression is used in SELECT, INSERT, UPDATE, and DELETE statements. However, table may not be used in the following statements:

INSERT INTO table_variable EXEC stored_procedure

SELECT select_list INTO table_variable statements
0
 
LVL 7

Expert Comment

by:SQL_Stu
ID: 12320211
Why don't you have @table as a parameter to your spSelectReportDataForMetric stored proc?  This should solve your problem.
0
 
LVL 6

Author Comment

by:dharmesh_amity
ID: 12320267
Oh! can you have that. I didn't know I will check out an example.

Thanks
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 6

Author Comment

by:dharmesh_amity
ID: 12320443
If I write this I get error

CREATE PROCEDURE dbo.spSelectReportDataForMetric
@MetricID int,
@DataElement1 int,
@DataElement2 int = -1,
@RetTable TABLE = NULL
AS



Can you show me an example where I can pass table as a parameter to Stored Procedure

Dharmesh
0
 
LVL 7

Assisted Solution

by:SQL_Stu
SQL_Stu earned 200 total points
ID: 12320469
Sorry, I was thinking that you would have already created the physical table.

ie. pass the table name as a string to the SP.

Anyway, why do you want to use variables at all for your table?  If you need to store the data why don't you use temporary tables?
0
 
LVL 6

Author Comment

by:dharmesh_amity
ID: 12320532
I think table variables are more efficient and I have many stored procedures already written with this pattern. I would not like to change this pattern, I would rather use User Defined Function. I thought I saw an example but I guess I was wrong as "acampoma" said.

I can still do it with udf. I guess I will be going that way.

Thank a lot,

Dharmesh
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question