无法将临时表从一个存储过程传递到另一个存储过程,并且出现错误'必须声明标量变量'

时间:2017-02-23 05:47:34

标签: sql tsql stored-procedures

我有一个临时表,其中填充了一堆整数。我需要将这个填充的临时表传递给另一个存储过程 - 但SQL不允许您将临时表传递给另一个存储过程(不管怎么说都不好)。

我根据https://msdn.microsoft.com/en-us/library/bb510489.aspx创建了一个TYPE来解决这个问题,但是intellisense抱怨使用这个用户定义类型的表不是标量变量。

CREATE TYPE TableType AS TABLE
(
    TheIds INT
);

DECLARE @IDsThatNeedToPass AS TableType


INSERT INTO @IDsThatNeedToPass 
SELECT . . .
. . .

EXEC OtherStoredProcedure @IDsThatNeedToPass 

For some reason the EXEC command says that I 'Must declare the scalar variable "@IDsThatNeedToPass"'

In addition to this, my OtherStoredProcedure cannot find my user-defined type.

CREATE PROCEDURE [dbo].[OtherStoredProcedure]
(
    @TheIds TableType READONLY -- Complains that Parameter or variable '@TheIds' has an invalid data type
)
...

任何可能导致这种情况的想法?我正在使用Microsoft SQL Server Management Studio 2014。

1 个答案:

答案 0 :(得分:0)

Refer below one format of temp tables 

**Table variables (DECLARE @t TABLE)** are visible only to the connection 
that creates it, and are deleted when the batch or stored procedure ends.

**Local temporary tables (CREATE TABLE #t)** are visible only to the 
connection that creates it, and are deleted when the connection is closed.

**Global temporary tables (CREATE TABLE ##t)** are visible to everyone, and 
are deleted when all connections that have referenced them have closed.

**Tempdb permanent tables (USE tempdb CREATE TABLE t)** are visible to 
everyone,and are deleted when the server is restarted.