Sql server 在存储过程外部访问临时表

Sql server 在存储过程外部访问临时表,sql-server,stored-procedures,Sql Server,Stored Procedures,我有一个存储过程,它以temp表的形式给出输出 Create Proc Hello (@id int,@name nvarchar(30)) as begin If (OBJECT_ID('tempdb..#Welcome') Is Not Null) Drop Table #Welcome select * into #Welcome from hello where id=@id If (OBJECT_ID('tempdb..#Welcomes') Is Not Null) Drop

我有一个存储过程,它以temp表的形式给出输出

Create Proc Hello (@id int,@name nvarchar(30))
as
begin 
If (OBJECT_ID('tempdb..#Welcome')  Is Not Null) Drop Table #Welcome
select * into #Welcome from hello where id=@id
If (OBJECT_ID('tempdb..#Welcomes')  Is Not Null) Drop Table #Welcomes
select * into #Welcomes from hello where name=@name

end
现在我得到了2个临时表作为结果,我将在数据集中使用它

现在我需要在另一个存储过程中访问this#welcome..我是说

    Create Proc HelloThere(@ids int,@name nvarchar(10)) 
    as
    begin 
      exec hello @id = @ids ,@name =@name

      //select * from #Welcome(Here i need to access the #Welcome so i can perform inner join something like below//

   select * from #welcome inner join Atable on #welcome.id=Atable.id

   end

存储过程完成时,在存储过程中创建的临时表将自动删除,因此临时表对调用存储过程不可用

保留临时表的一种方法是在调用过程中显式创建表(使用
createtable
selectinto
),然后将其加载到被调用过程中:

CREATE PROC Hello @id int,@name nvarchar(30)
AS
INSERT INTO #Welcome SELECT * FROM hello where id=@id;
GO

CREATE PROC HelloThere(@ids int,@name nvarchar(10)) 
AS
If OBJECT_ID(N'tempdb..#Welcome', 'U') IS NOT NULL DROP TABLE #Welcome;

SELECT * INTO #welcome FROM hello WHERE 0 = 1;

EXEC hello @id = @ids ,@name =@name;

SELECT * FROM #welcome INNER JOIN Atable ON #welcome.id=Atable.idl

GO

正如@jeroenmoster在注释中提到的,您可以仔细阅读其他技术。

存储过程完成时,在存储过程中创建的临时表将自动删除,因此临时表将不可用于调用存储过程

保留临时表的一种方法是在调用过程中显式创建表(使用
createtable
selectinto
),然后将其加载到被调用过程中:

CREATE PROC Hello @id int,@name nvarchar(30)
AS
INSERT INTO #Welcome SELECT * FROM hello where id=@id;
GO

CREATE PROC HelloThere(@ids int,@name nvarchar(10)) 
AS
If OBJECT_ID(N'tempdb..#Welcome', 'U') IS NOT NULL DROP TABLE #Welcome;

SELECT * INTO #welcome FROM hello WHERE 0 = 1;

EXEC hello @id = @ids ,@name =@name;

SELECT * FROM #welcome INNER JOIN Atable ON #welcome.id=Atable.idl

GO

正如@jeroenmoster在评论中提到的,您可以仔细阅读其他技术。

临时表是在过程之间共享数据的一种方法,但不是唯一的方法,也不总是最好的方法。有关如何使用临时表的完整概述,请参阅。感谢Jereon的帮助,informativeTemporary表是在过程之间共享数据的一种方法,但不是唯一的方法,也不总是最好的方法。请参阅以获取完整概述,包括如何使用临时表。非常感谢Jereon提供的帮助和信息