我有一个临时表,其中填充了一堆整数。我需要将这个填充的临时表传递给另一个存储过程 - 但是 SQL 不允许您将临时表传递给另一个存储过程(无论如何都不好)。
我已经创建了一个类型来解决这个问题,按照https://msdn.microsoft.com/en-us/library/bb510489.aspx但智能感知提示使用此用户定义类型的表不是标量变量。
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。
最佳答案
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.
关于sql - 无法将临时表从一个存储过程传递到另一个存储过程,出现错误 'Must declare scalar variable',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42408027/