从 SQL 存储过程返回临时表

发布于 2024-12-19 12:23:01 字数 1240 浏览 3 评论 0原文

我编写了这个存储过程:

USE [FAB28]
GO
/****** Object:  StoredProcedure [dbo].[spPLCIOFilter2]    Script Date: 12/06/2011 08:00:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[spPLCIOFilter2]
(
    @filter1 VARCHAR(50) = '%',
    @filter2 VARCHAR(50) = '%',
    @slot VARCHAR(50) = '%' 
)
AS
BEGIN

    CREATE TABLE #temp
    ( 
         "PLC RIO" char(20)
         ,SIGNAL varchar(6)
         ,RACK char(2)
         ,SLOT char(2)
         ,POINT char(2)
         ,"CARD" char(20)
         ,TAG char(30)
         ,PROJ char(10)
         ,"DESCRIPTION" char(100)
         ,"ADDRESS" varchar(22)
     )

    INSERT INTO #temp
    SELECT
       [PLC] "PLC RIO"
      ,[SIGNAL] SIGNAL
      ,[RACK] RACK
      ,[SLOT] SLOT
      ,[POINT] POINT
      ,[CARD] "CARD"
      ,[TAG] TAG
      ,[PROJ] PROJ
      ,[DESCRIPTION] "DESCRIPTION"
      ,[ADDRESS] "ADDRESS"
    FROM [FAB28].[dbo].[PLC_TAGS2]
    WHERE [PLC] <> ''
    and [PLC] LIKE '%' + @filter1 + '%'
    and [PLC] LIKE '%' + @filter2 + '%'
    and [SLOT] LIKE '%' + @slot + '%'
    ORDER BY [PLC],[RACK],[SLOT],[POINT]

END

我想返回临时表#temp,但return #temp不起作用。我还希望临时表将在函数末尾被删除。

我该怎么做呢?

I wrote this stored procedure:

USE [FAB28]
GO
/****** Object:  StoredProcedure [dbo].[spPLCIOFilter2]    Script Date: 12/06/2011 08:00:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[spPLCIOFilter2]
(
    @filter1 VARCHAR(50) = '%',
    @filter2 VARCHAR(50) = '%',
    @slot VARCHAR(50) = '%' 
)
AS
BEGIN

    CREATE TABLE #temp
    ( 
         "PLC RIO" char(20)
         ,SIGNAL varchar(6)
         ,RACK char(2)
         ,SLOT char(2)
         ,POINT char(2)
         ,"CARD" char(20)
         ,TAG char(30)
         ,PROJ char(10)
         ,"DESCRIPTION" char(100)
         ,"ADDRESS" varchar(22)
     )

    INSERT INTO #temp
    SELECT
       [PLC] "PLC RIO"
      ,[SIGNAL] SIGNAL
      ,[RACK] RACK
      ,[SLOT] SLOT
      ,[POINT] POINT
      ,[CARD] "CARD"
      ,[TAG] TAG
      ,[PROJ] PROJ
      ,[DESCRIPTION] "DESCRIPTION"
      ,[ADDRESS] "ADDRESS"
    FROM [FAB28].[dbo].[PLC_TAGS2]
    WHERE [PLC] <> ''
    and [PLC] LIKE '%' + @filter1 + '%'
    and [PLC] LIKE '%' + @filter2 + '%'
    and [SLOT] LIKE '%' + @slot + '%'
    ORDER BY [PLC],[RACK],[SLOT],[POINT]

END

I want to return the temporary table #temp and return #temp doesn't work. Also I want that the temp table will be dropped at the end of the function.

How can I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拧巴小姐 2024-12-26 12:23:02

对于问题的第二部分 - 当存储过程完成时,在存储过程中创建的任何临时表都会自动删除 - 您无需采取任何其他操作。

To the secondary part of your question - any temp tables created within a stored procedure are automatically dropped when the stored procedure finishes - you don't have to take any additional actions.

高速公鹿 2024-12-26 12:23:01

添加:

select * from #temp

您可以在程序末尾

。在您的 SP 中,临时表看起来是多余的。您只需将 insert 语句替换为 select 即可立即返回数据。

You would add:

select * from #temp

at the end of the procedure.

In your SP, the temporary table looks redundant. You could just replace the insert statement with a select to return the data immediately.

简单爱 2024-12-26 12:23:01

您不会从存储过程中返回这样的临时表。您必须首先创建临时表,调用过程,然后读取临时表:所有这些都在同一连接上,因此它保持在范围内

但是,为什么不只在存储过程中进行 SELECT 并正常使用结果集呢?这里不需要临时表

ALTER Procedure [dbo].[spPLCIOFilter2]
(
    @filter1 VARCHAR(50) = '%',
    @filter2 VARCHAR(50) = '%',
    @slot VARCHAR(50) = '%' 
)
AS
BEGIN
    SELECT
       [PLC] "PLC RIO"
      ,[SIGNAL] SIGNAL
      ,[RACK] RACK
      ,[SLOT] SLOT
      ,[POINT] POINT
      ,[CARD] "CARD"
      ,[TAG] TAG
      ,[PROJ] PROJ
      ,[DESCRIPTION] "DESCRIPTION"
      ,[ADDRESS] "ADDRESS"
    FROM [FAB28].[dbo].[PLC_TAGS2]
    WHERE [PLC] <> ''
    and [PLC] LIKE '%' + @filter1 + '%'
    and [PLC] LIKE '%' + @filter2 + '%'
    and [SLOT] LIKE '%' + @slot + '%'
    ORDER BY [PLC],[RACK],[SLOT],[POINT]
END

You don't return temp tables like this from stored procedures. You'd have to create the temp table first, call the proc, read the temp table afterwards: all on the same connection so it stays in scope

However, why not just SELECT in the stored proc and consume the result set normally? You don't need a temp table here

ALTER Procedure [dbo].[spPLCIOFilter2]
(
    @filter1 VARCHAR(50) = '%',
    @filter2 VARCHAR(50) = '%',
    @slot VARCHAR(50) = '%' 
)
AS
BEGIN
    SELECT
       [PLC] "PLC RIO"
      ,[SIGNAL] SIGNAL
      ,[RACK] RACK
      ,[SLOT] SLOT
      ,[POINT] POINT
      ,[CARD] "CARD"
      ,[TAG] TAG
      ,[PROJ] PROJ
      ,[DESCRIPTION] "DESCRIPTION"
      ,[ADDRESS] "ADDRESS"
    FROM [FAB28].[dbo].[PLC_TAGS2]
    WHERE [PLC] <> ''
    and [PLC] LIKE '%' + @filter1 + '%'
    and [PLC] LIKE '%' + @filter2 + '%'
    and [SLOT] LIKE '%' + @slot + '%'
    ORDER BY [PLC],[RACK],[SLOT],[POINT]
END
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文