Solved

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

Posted on 2004-10-15
6
3,378 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 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Oh! can you have that. I didn't know I will check out an example.

Thanks
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 6

Author Comment

by:dharmesh_amity
Comment Utility
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 50 total points
Comment Utility
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
Comment Utility
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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now