Solved

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

Posted on 2004-10-15
6
3,403 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL, How to do aggregation on extracting field and range band field? 2 30
SQL Backup Question 2 29
TSQL Challenge... 7 35
SQL Recursion 6 16
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

840 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