传入字符串,解析后分别操作

USE [XianChangPMSNEW0302]
GO
/****** Object: StoredProcedure [dbo].[proc_GetDistinctDeptPlan] Script Date: 03/27/2015 13:53:45 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[proc_GetDistinctDeptPlan]
@strWhere NVARCHAR(max)
AS
BEGIN
/****** Object: StoredProcedure [dbo].[GetList] Script Date: 03/09/2015 21:22:02 ******/
--SET ANSI_NULLS ON
--GO
--SET QUOTED_IDENTIFIER ON
--GO

Declare @Table_NameList table ( Name UNIQUEIDENTIFIER) -- 建立表变量
DECLARE @dynamicComplete NVARCHAR(Max);
Declare @Index_Param int /*参数 记录分隔符的位置*/
Declare @NeedParse NVARCHAR(Max) /*参数 没有处理的字符串*/

SELECT @Index_Param=CHARINDEX(',',@strWhere)
---------------------建立临时表存储查询条件---
IF(@Index_Param>0)
BEGIN
SET @NeedParse=@strWhere
while(CHARINDEX(',',@NeedParse)>0)
BEGIN
insert into @Table_NameList (Name) VALUES (CAST(SubString(@NeedParse,1,CharIndex(',',@NeedParse)-1) AS UNIQUEIDENTIFIER))
set @NeedParse =SubString(@NeedParse,CharIndex(',', @NeedParse)+1,len(@NeedParse)-CharIndex(',', @NeedParse))
END
INSERT INTO @Table_NameList(NAME) VALUES(CAST(@NeedParse AS UNIQUEIDENTIFIER))
END
ELSE
BEGIN
INSERT INTO @Table_NameList(NAME)VALUES(CAST(@strWhere AS UNIQUEIDENTIFIER))
END
----------------------执行查询,返回数据表---------------
SELECT DeptPlanID
FROM dbo.uv_whUsualMaterialInStock
WHERE MISID IN (SELECT NAME FROM @Table_NameList tnl)
AND DeptPlanID IS NOT NULL
GROUP BY DeptPlanID


END

原文地址:https://www.cnblogs.com/denghuachengle/p/4371632.html