StealthRT StealthRT -3 years ago 85
SQL Question

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:

enter image description here

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'

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download