存储过程处理前端选择多值集合
在存储过程中,有时需要处理前端传入的如数组集合,方法可以参考下面实例:
代码
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[usp_FqaVisual_GetWorkPlaceReport]
(
@WorkPlaceCollections NVARCHAR(50) --用户选择的场所集合,如:"2,5,6,7"
)
AS
BEGIN
--判断临时表是否存在
IF OBJECT_ID('#TempWorkPlace') IS NOT NULL
--如果存在,首先DROP
DROP TABLE #TempWorkPlace
--创建临时表
CREATE TABLE #TempWorkPlace
(
ID INT IDENTITY(1,1), --动处产生ID
WorkPlaceId TINYINT
)
END
--宣告变量
DECLARE @N INT, @ROWS INT,@WorkPlaceId TINYINT
--判断临时表是否有记录存在
IF EXISTS (SELECT * FROM #TempWorkPlace)
--如果有记录存在,首先TRUNCATE所有记录
TRUNCATE TABLE #TempWorkPlace
SELECT @N = 1
DECLARE @sql NVARCHAR(500)
--把用户选择的场所集合插入到临时表中。
SET @sql ='INSERT INTO #TempWorkPlace([WorkPlaceId]) SELECT '''+ REPLACE(@WorkPlaceCollections,',',''' UNION SELECT''') +''''
EXECUTE(@sql)
SELECT @ROWS = @@ROWCOUNT
--下面开始循环临时表
WHILE @N <= @ROWS
BEGIN
SELECT @WorkPlaceId = [WorkPlaceId] FROM #TempWorkPlace WHERE [Id] = @N
--这里开始处理与工作场所相关报表
SET @N = @N + 1
END
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[usp_FqaVisual_GetWorkPlaceReport]
(
@WorkPlaceCollections NVARCHAR(50) --用户选择的场所集合,如:"2,5,6,7"
)
AS
BEGIN
--判断临时表是否存在
IF OBJECT_ID('#TempWorkPlace') IS NOT NULL
--如果存在,首先DROP
DROP TABLE #TempWorkPlace
--创建临时表
CREATE TABLE #TempWorkPlace
(
ID INT IDENTITY(1,1), --动处产生ID
WorkPlaceId TINYINT
)
END
--宣告变量
DECLARE @N INT, @ROWS INT,@WorkPlaceId TINYINT
--判断临时表是否有记录存在
IF EXISTS (SELECT * FROM #TempWorkPlace)
--如果有记录存在,首先TRUNCATE所有记录
TRUNCATE TABLE #TempWorkPlace
SELECT @N = 1
DECLARE @sql NVARCHAR(500)
--把用户选择的场所集合插入到临时表中。
SET @sql ='INSERT INTO #TempWorkPlace([WorkPlaceId]) SELECT '''+ REPLACE(@WorkPlaceCollections,',',''' UNION SELECT''') +''''
EXECUTE(@sql)
SELECT @ROWS = @@ROWCOUNT
--下面开始循环临时表
WHILE @N <= @ROWS
BEGIN
SELECT @WorkPlaceId = [WorkPlaceId] FROM #TempWorkPlace WHERE [Id] = @N
--这里开始处理与工作场所相关报表
SET @N = @N + 1
END