在sql中分隔逗号分隔值

时间:2013-01-22 13:03:27

标签: sql sql-server

SP获取输入值ListType_IDs,它是列表类型的逗号分隔值。

create procedure test
@ListType_IDs varchar(255) = null
as
declare @IsGetDirectReports bit
declare @IsGetDirectReportsManagers bit
declare @IsGetAllManagers bit
declare @IsGetFullTeamUnderManager bit
--I have to initialize the above flags based on the value of list type IDs

例如,让ListType_IDs = 5,6,7

@IsGetDirectReports = 1,如果@ListType_IDs的值为5

@IsGetDirectReportsManagers = 1,如果@ListType_IDs的值为6

@IsGetFullTeamUnderManager = 1,如果@ListType_IDs的值为7

我需要使用简单的SQL代码来实现它。有人请帮忙。提前谢谢。

1 个答案:

答案 0 :(得分:2)

IF CHARINDEX(',5,' , ',' +@ListType_IDs + ',') > 0
SELECT @IsGetIderctReports = 1

IF CHARINDEX(',6,' , ',' +@ListType_IDs + ',') > 0
SELECT @IsGetDirectReportsManagers = 1

IF CHARINDEX(',7,' , ',' +@ListType_IDs + ',') > 0
SELECT @IsGetFullTeamUnderManager = 1

Is storing a delimited list in a database column really that bad?现在我们知道了。