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?
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.
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
问题的第二部分-存储过程完成后,将自动删除在存储过程中创建的任何临时表-您无需执行任何其他操作。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.