2017-02-23 12 views
1

我有一個臨時表,其中填充了一堆整數。我需要將這個填充的臨時表傳遞到另一個存儲過程 - 但是,SQL不允許您將臨時表傳遞給另一個存儲過程(反正不太好)。無法將臨時表從一個存儲過程傳遞到另一個存在錯誤'必須聲明標量變量'

我已經創建了一個類型來解決這個問題,根據https://msdn.microsoft.com/en-us/library/bb510489.aspx,但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.

回答

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. 
相關問題