User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 423,192 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,559 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MS SQL advertiser: Programming Forums

Collating results of subqueries into fields

Join Date: May 2008
Posts: 3
Reputation: Paul Phillips is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Paul Phillips Paul Phillips is offline Offline
Newbie Poster

Re: Collating results of subqueries into fields

  #3  
May 19th, 2008
No offense, Friend, but your answer is waffle.

Identifying primary and foreign keys only helps to speed up the engine and enforce a level of data integrity. Neither apply to my problem.

The way I thought I could do it is like this (I know the names are different - I wanted to keep it simple for the example) :

  1. CREATE TABLE #TempPPUser
  2. (
  3. UserID VARCHAR(64),
  4. Groups VARCHAR(8000),
  5. Roles VARCHAR(8000)
  6. )
  7.  
  8. INSERT INTO #TempPPUser
  9. SELECT tps_guid UserID, '' AS Groups, '' AS Roles FROM tps_user
  10.  
  11. DECLARE @UserID varchar(64), @GroupID varchar(64), @RoleID varchar(64)
  12.  
  13. DECLARE usersgroups_cursor CURSOR FOR
  14. SELECT tps_user_id, tps_group_id
  15. FROM tps_user_group
  16.  
  17. OPEN usersgroups_cursor
  18.  
  19. FETCH NEXT FROM usersgroups_cursor
  20. INTO @UserID, @GroupID
  21.  
  22. UPDATE #TempPPUser
  23. SET Groups = COALESCE(Groups + ',', '') + (SELECT tps_group.tps_name FROM tps_group WHERE tps_guid = @GroupID)
  24. WHERE #TempPPUser.UserID = @UserID
  25.  
  26. WHILE @@FETCH_STATUS = 0
  27. BEGIN
  28. FETCH NEXT FROM usersgroups_cursor
  29. INTO @UserID, @GroupID
  30.  
  31. UPDATE #TempPPUser
  32. SET Groups = COALESCE(Groups + ',', '') + (SELECT tps_group.tps_name FROM tps_group WHERE tps_guid = @GroupID)
  33. WHERE #TempPPUser.UserID = @UserID
  34. END
  35.  
  36. CLOSE usersgroups_cursor
  37. DEALLOCATE usersgroups_cursor
  38.  
  39. DECLARE usersroles_cursor CURSOR FOR
  40. SELECT tps_user_id, tps_role_id
  41. FROM tps_user_role
  42.  
  43. OPEN usersroles_cursor
  44.  
  45. FETCH NEXT FROM usersroles_cursor
  46. INTO @UserID, @RoleID
  47.  
  48. UPDATE #TempPPUser
  49. SET Roles = COALESCE(Roles + ',', '') + (SELECT tps_role.tps_name FROM tps_role WHERE tps_guid = @RoleID)
  50. WHERE #TempPPUser.UserID = @UserID
  51.  
  52. WHILE @@FETCH_STATUS = 0
  53. BEGIN
  54. FETCH NEXT FROM usersroles_cursor
  55. INTO @UserID, @RoleID
  56.  
  57. UPDATE #TempPPUser
  58. SET Roles = COALESCE(Roles + ',', '') + (SELECT tps_role.tps_name FROM tps_role WHERE tps_guid = @RoleID)
  59. WHERE #TempPPUser.UserID = @UserID
  60. END
  61.  
  62. CLOSE usersroles_cursor
  63. DEALLOCATE usersroles_cursor
  64.  
  65. SELECT tps_user.tps_name, #TempPPUser.Groups, #TempPPUser.Roles
  66. FROM tps_user INNER JOIN #TempPPUser ON tps_user.tps_guid = #TempPPUser.UserID
  67. WHERE tps_name = 'PPhillips1'
  68.  
  69. DROP TABLE #TempPPUser
  70.  
  71. END

=======================

The problem is it takes forever to run! Anyone any better ideas?
Last edited by peter_budo : May 19th, 2008 at 12:46 pm. Reason: Keep It Organized - please use [code] tags. No offence but code tags are there for reason
Reply With Quote  
All times are GMT -4. The time now is 8:27 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC