Stored procedures 声明没有列定义的表变量?

Stored procedures 声明没有列定义的表变量?,stored-procedures,sql-server-2000,Stored Procedures,Sql Server 2000,在SQL Server中,有没有一种方法可以在不知道表定义的情况下声明表变量 免费范例: DECLARE @Results TABLE INSERT INTO @Results EXEC MyProc @param1 = @myValue 或 或 或 或 或 或 (你明白了)不可能。引用自“在线图书”: ============== 语法 注意:使用DECLARE@local_variable来声明table类型的变量 table_type_definition ::= TABLE (

在SQL Server中,有没有一种方法可以在不知道表定义的情况下声明表变量

免费范例:

DECLARE @Results TABLE
INSERT INTO @Results EXEC MyProc @param1 = @myValue

(你明白了)

不可能。引用自“在线图书”:

==============

语法 注意:使用DECLARE@local_variable来声明table类型的变量

table_type_definition ::= 
  TABLE ( { column_definition | table_constraint } [ ,...n ] ) 
==============

语法上需要“(”,至少一个列定义和“)”


PS:AFAIK从“exec”结果插入任何新表是根本不可能的。仅适用于具有预定义结构的表。

不能使用表变量,但可以使用临时表

-- Drop the table, if it exists
IF OBJECT_ID(N'tempdb.dbo.#tmpMyTable',N'U') IS NOT NULL
DROP TABLE #tmpMyTable


SELECT
  ColumnA,
  ColumnB

INTO #tmpMyTable

FROM MyTable

-- Then clean up after yourself
DROP TABLE #tmpMyTable
DECLARE @Results TABLE
EXEC INTO @Results MyProc @param1 = @myValue
DECLARE @Results TABLE
SELECT * FROM EXEC MyProc @param1 = @myValue INTO @Results
DECLARE @Results TABLE
SELECT * INTO @Results FROM EXEC MyProc @param1 = @myValue
DECLARE @Results TABLE
SELECT * INTO @Results EXEC MyProc @param1 = @myValue
table_type_definition ::= 
  TABLE ( { column_definition | table_constraint } [ ,...n ] ) 
-- Drop the table, if it exists
IF OBJECT_ID(N'tempdb.dbo.#tmpMyTable',N'U') IS NOT NULL
DROP TABLE #tmpMyTable


SELECT
  ColumnA,
  ColumnB

INTO #tmpMyTable

FROM MyTable

-- Then clean up after yourself
DROP TABLE #tmpMyTable