简体   繁体   中英

SQL Server stored procedure looping through a comma delimited cell

I am trying to figure out how to go about getting the values of a comma separated string that's present in one of my cells.

This is the query I current am trying to figure out in my stored procedure:

SELECT 
   uT.id, 
   uT.permissions
FROM 
   usersTbl AS uT
INNER JOIN 
   usersPermissions AS uP 
   /*Need to loop here I think?*/
WHERE 
   uT.active = 'true'
AND 
   uT.email = 'bbarker@thepriceisright.com'

The usersPermissions table looks like this:

在此处输入图片说明

And so a row in the usersTbl table looks like this for permissions :

1,3

I need to find a way to loop through that cell and get each number and place the name ****, in my returned results for the usersTbl.permissions .

So instead of returning this:

Name    | id   | permissions | age |
------------------------------------
Bbarker | 5987 | 1,3         | 87  |

It needs to returns this:

Name    | id   | permissions | age |
------------------------------------
Bbarker | 5987 | Read,Upload | 87  |

Really just replacing 1,3 with Read,Upload .

Any help would be great from a SQL GURU!

Reworked query

 SELECT 
     * 
 FROM
     usersTbl AS uT 
 INNER JOIN 
     usersPermissionsTbl AS uPT 
 ON 
     uPT.userId = uT.id 
 INNER JOIN 
     usersPermissions AS uP 
 ON 
     uPT.permissionId = uP.id 
 WHERE 
     uT.active='true'
 AND 
     uT.email='bBarker@thepriceisright.com'

First, you should read Is storing a delimited list in a database column really that bad? , where you will see a lot of reasons why the answer to this question is Absolutely yes!

Second, you should add a table for user permissions since this is clearly a many to many relationship. Your tables might look something like this (pseudo code):

usersTbl
(
    Id int primary key
    -- other user related columns
)

usersPermissionsTbl
(
    UserId int, -- Foreign key to usersTbl
    PermissionId int, -- Foreign key to permissionsTbl
    Primary key (UserId, PermissionId)
)

permissionsTbl
(
    Id int primary key,
    Name varchar(20)
)

Once you have your tables correct, it's quite easy to get a list of comma separated values from the permissions table.

Adapting scsimon's sample data script to a correct many to many relationship:

declare @users table ([Name] varchar(64), id int, age int)

insert into @users values 
('Bbarker',5987,87)

declare @permissions table (id int, [type] varchar(64))

insert into @permissions values
(1,'Read'),
(2,'Write'),
(3,'Upload'),
(4,'Admin')

declare @usersPermissions as table (userId int, permissionId int)

insert into @usersPermissions values (5987, 1), (5987, 3)

Now the query looks like this:

SELECT  u.Name,
        u.Id,
        STUFF(
        (
            SELECT ','+ [type]
            FROM @permissions p
            INNER JOIN @usersPermissions up ON p.id = up.permissionId
            WHERE up.userId = u.Id
            FOR XML PATH('')
        )
            , 1, 1, '') As Permissions,
        u.Age
FROM @Users As u

And the results:

Name    Id      Permissions     Age
Bbarker 5987    Read,Upload     87

You can see a live demo on rextester.

I agree with all of the comments... but strictly trying to do what you want, here's a way with a splitter function

declare @usersTbl table ([Name] varchar(64), id int, [permissions] varchar(64), age int)
insert into @usersTbl
values

('Bbarker',5987,'1,3',87)

declare @usersTblpermissions table (id int, [type] varchar(64))
insert into @usersTblpermissions
values
(1,'Read'),
(2,'Write'),
(3,'Upload'),
(4,'Admin')

;with cte as(
    select
        u.[Name]
        ,u.id as UID
        ,p.id
        ,p.type
        ,u.age
    from @usersTbl u
    cross apply dbo.DelimitedSplit8K([permissions],',') x
    inner join @usersTblpermissions p on p.id = x.Item)

select distinct
    [Name]
    ,UID
    ,age
    ,STUFF((
          SELECT ',' + t2.type
          FROM cte t2
          WHERE t.UID = t2.UID
          FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
from cte t

Jeff Moden Splitter

CREATE FUNCTION [dbo].[DelimitedSplit8K] (@pString VARCHAR(8000), @pDelimiter CHAR(1))
--WARNING!!! DO NOT USE MAX DATA-TYPES HERE!  IT WILL KILL PERFORMANCE!

RETURNS TABLE WITH SCHEMABINDING AS
RETURN

/* "Inline" CTE Driven "Tally Table" produces values from 1 up to 10,000...
enough to cover VARCHAR(8000)*/

  WITH E1(N) AS (
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
                ),                          --10E+1 or 10 rows
       E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
       E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
 cteTally(N) AS (--==== This provides the "base" CTE and limits the number of rows right up front
                     -- for both a performance gain and prevention of accidental "overruns"
                 SELECT TOP (ISNULL(DATALENGTH(@pString),0)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
                ),
cteStart(N1) AS (--==== This returns N+1 (starting position of each "element" just once for each delimiter)
                 SELECT 1 UNION ALL
                 SELECT t.N+1 FROM cteTally t WHERE SUBSTRING(@pString,t.N,1) = @pDelimiter
                ),
cteLen(N1,L1) AS(--==== Return start and length (for use in substring)
                 SELECT s.N1,
                        ISNULL(NULLIF(CHARINDEX(@pDelimiter,@pString,s.N1),0)-s.N1,8000)
                   FROM cteStart s
                )
--===== Do the actual split. The ISNULL/NULLIF combo handles the length for the final element when no delimiter is found.
 SELECT ItemNumber = ROW_NUMBER() OVER(ORDER BY l.N1),
        Item       = SUBSTRING(@pString, l.N1, l.L1)
   FROM cteLen l
;
GO

I concur with much of the advice being presented to you in the other responses. The structure you're starting with is not going to be fun to maintain and work with. However, your situation may mean you are stuck with it so maybe some of the tools below will help you.

You can parse the delimiter with charindex() as others demonstrated here- MSSQL - How to split a string using a comma as a separator

... and even better here (several functions are provided) - Split function equivalent in T-SQL?

If you still want to do it with raw inline SQL and are committed to a loop, then pair the string manipulation with a CURSOR. Cursors have their own controversies BTW. The code below will work if your permission syntax remains consistent, which it probably doesn't.

They used charindex(',',columnName) and fed the location into the left() and right() functions along with some additional string evaluation to pull values out. You should be able to piece those together with a cursor

Your query might look like this...

--creating my temp structure
declare @userPermissions table (id int, [type] varchar(16))
insert into @userPermissions (id, [type]) values (1, 'Read')
insert into @userPermissions (id, [type]) values (2, 'Write')
insert into @userPermissions (id, [type]) values (3, 'Upload')
insert into @userPermissions (id, [type]) values (4, 'Admin')

declare @usersTbl table ([Name] varchar(16), id int, [permissions] varchar(8), age int)
insert into @usersTbl ([Name], id, [permissions], age) values ('Bbarker', 5987, '1,3', 87)
insert into @usersTbl ([Name], id, [permissions], age) values ('Mmouse', 5988, '2,4', 88)

--example query
select 
    ut.[Name]
   ,  (select [type] from @userPermissions where [id] = left(ut.[permissions], charindex(',', ut.[permissions])-1) )
    + ','
    + (select [type] from @userPermissions where [id] = right(ut.[permissions], len(ut.[permissions])-charindex(',', ut.[permissions])) )
from @usersTbl ut

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM