Solved

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

Posted on 2004-10-15
6
3,396 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
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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 50 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

770 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