50

Is it true that SQL Server 2000, you can not insert into a table variable using exec?

I tried this script and got an error message:

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

declare @tmp TABLE (code varchar(50), mount money)
DECLARE @q nvarchar(4000)
SET @q = 'SELECT coa_code, amount FROM T_Ledger_detail'

INSERT INTO  @tmp (code, mount)
EXEC sp_executesql (@q)

SELECT * from @tmp

If that true, what should I do?

3
  • If it's true, you should CREATE TEMP TABLE (I'm sure you can insert into a temp table from EXEC). Commented Jan 31, 2013 at 9:59
  • "Is it true?" - yes - "However, table may not be used in the following statements: INSERT INTO table_variable EXEC stored_procedure" Commented Jan 31, 2013 at 10:12
  • Ok, i got it,, it's like @Anton Kovalenko said,, i need to create temporary table , not Table variable ... Thx. :) Commented Jan 31, 2013 at 10:15

3 Answers 3

83

N.B. - this question and answer relate to the 2000 version of SQL Server. In later versions, the restriction on INSERT INTO @table_variable ... EXEC ... were lifted and so it doesn't apply for those later versions.


You'll have to switch to a temp table:

CREATE TABLE #tmp (code varchar(50), mount money)
DECLARE @q nvarchar(4000)
SET @q = 'SELECT coa_code, amount FROM T_Ledger_detail'

INSERT INTO  #tmp (code, mount)
EXEC sp_executesql (@q)

SELECT * from #tmp

From the documentation:

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.

Sign up to request clarification or add additional context in comments.

6 Comments

@Damien, in your example there is no reason to use a Temp Table, you can still use a Table variable even in your example.
Verified that you can use @tableVariables in 2008+
@ohmusama - yes, but the question is tagged 2000 and I've linked to the 2000 documentation saying it's not available.
@Damien_The_Unbeliever, I know, but since google can't figure that out, (as I got here from there), this is a comment for other people like me.
@ohmusama - okay, I've added an note at the top to (try to) make it clear that this question and answer do just relate to the 2000 version of the product.
|
14

The documentation is misleading.
I have the following code running in production

DECLARE @table TABLE (UserID varchar(100))
DECLARE @sql varchar(1000)
SET @sql = 'spSelUserIDList'
/* Will also work
   SET @sql = 'SELECT UserID FROM UserTable'
*/

INSERT INTO @table
EXEC(@sql)

SELECT * FROM @table

Comments

5
DECLARE @q nvarchar(4000)
SET @q = 'DECLARE @tmp TABLE (code VARCHAR(50), mount MONEY)
INSERT INTO @tmp
  (
    code,
    mount
  )
SELECT coa_code,
       amount
FROM   T_Ledger_detail

SELECT *
FROM   @tmp'

EXEC sp_executesql @q

If you want in dynamic query

3 Comments

yes , i want the values is from dynamic script.. and i think this is not dynamic..
So above query will help you
Yeah this is a static execution. In order for this to be dynamic you need to have concatenations within the assignment to the @q variable. Nice try but no dice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.