sql - 无法将临时表从一个存储过程传递到另一个存储过程,出现错误 'Must declare scalar variable'

标签 sql t-sql stored-procedures

我有一个临时表,其中填充了一堆整数。我需要将这个填充的临时表传递给另一个存储过程 - 但是 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/

相关文章:

sql - 当列的值与上一行相比发生变化时选择行

mysql - 如何使用 MySQL 获取每天的数字总和?

sql-server - 如何获取每个季度的运行总计,而不是将重复值设为 0。使用 SQL Server 2012

c# - Entity Framework 6 和具有架构名称的存储过程

sql-server - SQL - 如何使用 ISO 周获取年-周?

t-sql - SQL Join 语句问题

c# - 显示 SQL Server 存储过程的单个返回值的函数

sql - SQL中用IF语句替换CASE语句

sql - 使用 Sql 2005 CTE 可以吗?

mysql - 在第 4 列中添加 3 列(具有 avg() 值)