Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
sqlnovice123
Constraint Violating Yak Guru
262 Posts |
Posted - 2011-02-14 : 09:44:19
|
Hi,I am using sql 2005.I have the requirement that I need to get the data for ids for only those users that have access to the ids when a Group is specified in teh front end.I have the below parameter passed to the stored procedure@UserID smallint@CustText1 nvarchar(100) @CustBool1 bit = 0 The below are local variablesDeclare @Group nvarchar(100)Declare @UseLatestPeriod bitSet @Group = CASE WHEN LEN(RTRIM(IsNull(@CustText1,''))) < 1 THEN NULL ELSE RTRIM(@CustText1)ENDSet @UseLatestPeriod = @CustBool1DECLARE @SelectedFunds TABLE(pshipid nvarchar(100))IF @Group IS NULL Begininsert into @SelectedFundsSELECT DISTINCT pshipid from SelectedCapitalAccounts Where SessionID = @SessionID AND ReportID = @ReportID AND Selected = 1 endELSEBegininsert into @SelectedFundsSELECT DISTINCT pshipid FROM GroupPshipAccess GPA INNER JOIN UserGroup UG ON GPA.UserGroupID = UG.UserGroupIDINNER JOIN UserGroupMember UGM ON GPA.UserGroupID = UGM.UserGroupIDWHERE UserGroupName = @Groupgroup by pshipidI have three tables UserGroup, UserGroupMember and GrouPshipAccess.If a Group is specified, include ids that are in the group requested. Table GroupPshipAccess identifies id in a group.If a Group is specified, only include ids that a user has access to. Table UserGroupMember contains user's group access.I am tryign to account for th parameter @UserID passed in the stored procedure and I am not sure how to fit in the UserID with the above joins in the insert to the table.I tried something like the below but I get too many rows(270K)DECLARE @UserID smallintDECLARE @SelectedFunds TABLE(pshipid nvarchar(100), UserID smallint)insert into @SelectedFundsSELECT DISTINCT GPA.pshipid, UGM.UserID FROM GroupPshipAccess GPA INNER JOIN UserGroup UG ON GPA.UserGroupID = UG.UserGroupIDINNER JOIN UserGroupMember UGM ON UG.UserGroupID = UGM.UserGroupID The query below gives me 1804 rows which is correct. How can I account for user's group access and set the paramter @UserID?SELECT DISTINCT pshipid FROM GroupPshipAccess GPA INNER JOIN UserGroup UG ON GPA.UserGroupID = UG.UserGroupIDINNER JOIN UserGroupMember UGM ON GPA.UserGroupID = UGM.UserGroupIDWHERE UserGroupName = @Groupgroup by pshipidThanks for your help.Regards,sqlnovice123 |
|
sqlnovice123
Constraint Violating Yak Guru
262 Posts |
Posted - 2011-02-14 : 10:00:32
|
I tried the below:DECLARE @SelectedFunds TABLE(pshipid nvarchar(100), UserGroupID smallint, UserID smallint)insert into @SelectedFundsSELECT DISTINCT GPA.pshipid, UserGroupID, UserID FROM GroupPshipAccess GPA INNER JOIN UserGroup UG ON GPA.UserGroupID = UG.UserGroupIDINNER JOIN UserGroupMember UGM ON UG.UserGroupID = UGM.UserGroupID WHERE UserGroupName = 'BLUECREST'group by GPA.pshipid, GPA.UserGroupID, UGM.UserIDselect * from @SelectedFundsI get 1128 rows. examplepshipid UserGroupID UserID42 12 6142 164 6142 179 6142 321 6142 336 6142 403 6142 687 6142 691 6142 692 6142 733 6142 734 6142 742 6142 502 6142 509 6142 586 6142 596 6142 623 6142 624 6142 415 6142 425 6142 459 6142 476 6142 490 6142 493 61660 12 61660 164 61660 179 61660 321 61660 336 61660 403 61660 687 61660 691 61660 692 61660 733 61660 734 61660 742 61660 502 61660 509 61660 586 61660 596 61660 623 61660 624 61660 415 61660 425 61660 459 61660 476 61660 490 61660 493 61661 12 61661 164 61661 179 61661 321 61661 336 61661 403 61661 687 61661 691 61661 692 61661 733 61661 734 61661 742 61661 502 61661 509 61661 586 61661 596 61661 623 61661 624 61661 415 61661 425 61661 459 61661 476 61661 490 61661 493 61743 12 61743 164 61743 179 61743 321 61743 336 61743 403 61743 687 61743 691 61743 692 61743 733 61743 734 61743 742 61743 502 61743 509 61743 586 61743 596 61743 623 61743 624 61743 415 61743 425 61743 459 61743 476 61743 490 61743 493 61744 12 61744 164 61744 179 61744 321 61744 336 61744 403 61744 687 61744 691 61744 692 61744 733 61744 734 61744 742 61744 502 61744 509 61744 586 61744 596 61744 623 61744 624 61744 415 61744 425 61744 459 61744 476 61744 490 61744 493 61750 12 61750 164 61750 179 61750 321 61750 336 61750 403 61750 687 61750 691 61750 692 61750 733 61750 734 61750 742 61750 502 61750 509 61750 586 61750 596 61750 623 61750 624 61750 415 61750 425 61750 459 61750 476 61750 490 61750 493 61751 12 61751 164 61751 179 61751 321 61751 336 61751 403 61751 687 61751 691 61751 692 61751 733 61751 734 61751 742 61751 502 61751 509 61751 586 61751 596 61751 623 61751 624 61751 415 61751 425 61751 459 61751 476 61751 490 61751 493 61752 12 61752 164 61752 179 61752 321 61752 336 61752 403 61752 687 61752 691 61752 692 61752 733 61752 734 61752 742 61752 502 61752 509 61752 586 61752 596 61752 623 61752 624 61752 415 61752 425 61752 459 61752 476 61752 490 61752 493 61753 12 61753 164 61753 179 61753 321 61753 336 61753 403 61753 687 61753 691 61753 692 61753 733 61753 734 61753 742 61753 502 61753 509 61753 586 61753 596 61753 623 61753 624 61753 415 61753 425 61753 459 61753 476 61753 490 61753 493 61760 12 61760 164 61760 179 61760 321 61760 336 61760 403 61760 687 61760 691 61760 692 61760 733 61760 734 61760 742 61760 502 61760 509 61760 586 61760 596 61760 623 61760 624 61760 415 61760 425 61760 459 61760 476 61760 490 61760 493 61775 12 61775 164 61775 179 61775 321 61775 336 61775 403 61775 687 61775 691 61775 692 61775 733 61775 734 61775 742 61775 502 61775 509 61775 586 61775 596 61775 623 61775 624 61775 415 61775 425 61775 459 61775 476 61775 490 61775 493 61776 12 61776 164 61776 179 61776 321 61776 336 61776 403 61776 687 61776 691 61776 692 61776 733 61776 734 61776 742 61776 502 61776 509 61776 586 61776 596 61776 623 61776 624 61776 415 61776 425 61776 459 61776 476 61776 490 61776 493 61777 12 61777 164 61777 179 61777 321 61777 336 61777 403 61777 687 61777 691 61777 692 61777 733 61777 734 61777 742 61777 502 61777 509 61777 586 61777 596 61777 623 61777 624 61777 415 61777 425 61777 459 61777 476 61777 490 61777 493 61781 12 61781 164 61781 179 61781 321 61781 336 61781 403 61781 687 61781 691 61781 692 61781 733 61781 734 61781 742 61781 502 61781 509 61781 586 61781 596 61781 623 61781 624 61781 415 61781 425 61781 459 61781 476 61781 490 61781 493 61788 12 61788 164 61788 179 61788 321 61788 336 61788 403 61788 687 61788 691 61788 692 61788 733 61788 734 61788 742 61788 502 61788 509 61788 586 61788 596 61788 623 61788 624 61788 415 61788 425 61788 459 61788 476 61788 490 61788 493 61793 12 61793 164 61793 179 61793 321 61793 336 61793 403 61793 687 61793 691 61793 692 61793 733 61793 734 61793 742 61793 502 61793 509 61793 586 61793 596 61793 623 61793 624 61793 415 61793 425 61793 459 61793 476 61793 490 61793 493 61794 12 61794 164 61794 179 61794 321 61794 336 61794 403 61794 687 61794 691 61794 692 61794 733 61794 734 61794 742 61794 502 61794 509 61794 586 61794 596 61794 623 61794 624 61794 415 61794 425 61794 459 61794 476 61794 490 61794 493 61797 12 61797 164 61797 179 61797 321 61797 336 61797 403 61797 687 61797 691 61797 692 61797 733 61797 734 61797 742 61797 502 61797 509 61797 586 61797 596 61797 623 61797 624 61797 415 61797 425 61797 459 61797 476 61797 490 61797 493 61798 12 61798 164 61798 179 61798 321 61798 336 61798 403 61798 687 61798 691 61798 692 61798 733 61798 734 61798 742 61798 502 61798 509 61798 586 61798 596 61798 623 61798 624 61798 415 61798 425 61798 459 61798 476 61798 490 61798 493 61799 12 61799 164 61799 179 61799 321 61799 336 61799 403 61799 687 61799 691 61799 692 61799 733 61799 734 61799 742 61799 502 61799 509 61799 586 61799 596 61799 623 61799 624 61799 415 61799 425 61799 459 61799 476 61799 490 61799 493 61807 12 61807 164 61807 179 61807 321 61807 336 61807 403 61807 687 61807 691 61807 692 61807 733 61807 734 61807 742 61807 502 61807 509 61807 586 61807 596 61807 623 61807 624 61807 415 61807 425 61807 459 61807 476 61807 490 61807 493 61808 12 61808 164 61808 179 61808 321 61808 336 61808 403 61808 687 61808 691 61808 692 61808 733 61808 734 61808 742 61808 502 61808 509 61808 586 61808 596 61808 623 61808 624 61808 415 61808 425 61808 459 61808 476 61808 490 61808 493 61813 12 61813 164 61813 179 61813 321 61813 336 61813 403 61813 687 61813 691 61813 692 61813 733 61813 734 61813 742 61813 502 61813 509 61813 586 61813 596 61813 623 61813 624 61813 415 61813 425 61813 459 61813 476 61813 490 61813 493 61814 12 61814 164 61814 179 61814 321 61814 336 61814 403 61814 687 61814 691 61814 692 61814 733 61814 734 61814 742 61814 502 61814 509 61814 586 61814 596 61814 623 61814 624 61814 415 61814 425 61814 459 61814 476 61814 490 61814 493 61822 12 61822 164 61822 179 61822 321 61822 336 61822 403 61822 687 61822 691 61822 692 61822 733 61822 734 61822 742 61822 502 61822 509 61822 586 61822 596 61822 623 61822 624 61822 415 61822 425 61822 459 61822 476 61822 490 61822 493 61823 12 61823 164 61823 179 61823 321 61823 336 61823 403 61823 687 61823 691 61823 692 61823 733 61823 734 61823 742 61823 502 61823 509 61823 586 61823 596 61823 623 61823 624 61823 415 61823 425 61823 459 61823 476 61823 490 61823 493 61824 12 61824 164 61824 179 61824 321 61824 336 61824 403 61824 687 61824 691 61824 692 61824 733 61824 734 61824 742 61824 502 61824 509 61824 586 61824 596 61824 623 61824 624 61824 415 61824 425 61824 459 61824 476 61824 490 61824 493 61831 12 61831 164 61831 179 61831 321 61831 336 61831 403 61831 687 61831 691 61831 692 61831 733 61831 734 61831 742 61831 502 61831 509 61831 586 61831 596 61831 623 61831 624 61831 415 61831 425 61831 459 61831 476 61831 490 61831 493 61832 12 61832 164 61832 179 61832 321 61832 336 61832 403 61832 687 61832 691 61832 692 61832 733 61832 734 61832 742 61832 502 61832 509 61832 586 61832 596 61832 623 61832 624 61832 415 61832 425 61832 459 61832 476 61832 490 61832 493 61833 12 61833 164 61833 179 61833 321 61833 336 61833 403 61833 687 61833 691 61833 692 61833 733 61833 734 61833 742 61833 502 61833 509 61833 586 61833 596 61833 623 61833 624 61833 415 61833 425 61833 459 61833 476 61833 490 61833 493 61834 12 61834 164 61834 179 61834 321 61834 336 61834 403 61834 687 61834 691 61834 692 61834 733 61834 734 61834 742 61834 502 61834 509 61834 586 61834 596 61834 623 61834 624 61834 415 61834 425 61834 459 61834 476 61834 490 61834 493 61835 12 61835 164 61835 179 61835 321 61835 336 61835 403 61835 687 61835 691 61835 692 61835 733 61835 734 61835 742 61835 502 61835 509 61835 586 61835 596 61835 623 61835 624 61835 415 61835 425 61835 459 61835 476 61835 490 61835 493 61854 12 61854 164 61854 179 61854 321 61854 336 61854 403 61854 687 61854 691 61854 692 61854 733 61854 734 61854 742 61854 502 61854 509 61854 586 61854 596 61854 623 61854 624 61854 415 61854 425 61854 459 61854 476 61854 490 61854 493 61855 12 61855 164 61855 179 61855 321 61855 336 61855 403 61855 687 61855 691 61855 692 61855 733 61855 734 61855 742 61855 502 61855 509 61855 586 61855 596 61855 623 61855 624 61855 415 61855 425 61855 459 61855 476 61855 490 61855 493 61856 12 61856 164 61856 179 61856 321 61856 336 61856 403 61856 687 61856 691 61856 692 61856 733 61856 734 61856 742 61856 502 61856 509 61856 586 61856 596 61856 623 61856 624 61856 415 61856 425 61856 459 61856 476 61856 490 61856 493 61857 12 61857 164 61857 179 61857 321 61857 336 61857 403 61857 687 61857 691 61857 692 61857 733 61857 734 61857 742 61857 502 61857 509 61857 586 61857 596 61857 623 61857 624 61857 415 61857 425 61857 459 61857 476 61857 490 61857 493 61903 12 61903 164 61903 179 61903 321 61903 336 61903 403 61903 687 61903 691 61903 692 61903 733 61903 734 61903 742 61903 502 61903 509 61903 586 61903 596 61903 623 61903 624 61903 415 61903 425 61903 459 61903 476 61903 490 61903 493 61963 12 61963 164 61963 179 61963 321 61963 336 61963 403 61963 687 61963 691 61963 692 61963 733 61963 734 61963 742 61963 502 61963 509 61963 586 61963 596 61963 623 61963 624 61963 415 61963 425 61963 459 61963 476 61963 490 61963 493 61964 12 61964 164 61964 179 61964 321 61964 336 61964 403 61964 687 61964 691 61964 692 61964 733 61964 734 61964 742 61964 502 61964 509 61964 586 61964 596 61964 623 61964 624 61964 415 61964 425 61964 459 61964 476 61964 490 61964 493 611045 12 611045 164 611045 179 611045 321 611045 336 611045 403 611045 687 611045 691 611045 692 611045 733 611045 734 611045 742 611045 502 611045 509 611045 586 611045 596 611045 623 611045 624 611045 415 611045 425 611045 459 611045 476 611045 490 611045 493 611200 12 611200 164 611200 179 611200 321 611200 336 611200 403 611200 687 611200 691 611200 692 611200 733 611200 734 611200 742 611200 502 611200 509 611200 586 611200 596 611200 623 611200 624 611200 415 611200 425 611200 459 611200 476 611200 490 611200 493 611201 12 611201 164 611201 179 611201 321 611201 336 611201 403 611201 687 611201 691 611201 692 611201 733 611201 734 611201 742 611201 502 611201 509 611201 586 611201 596 611201 623 611201 624 611201 415 611201 425 611201 459 611201 476 611201 490 611201 493 611202 12 611202 164 611202 179 611202 321 611202 336 611202 403 611202 687 611202 691 611202 692 611202 733 611202 734 611202 742 611202 502 611202 509 611202 586 611202 596 611202 623 611202 624 611202 415 611202 425 611202 459 611202 476 611202 490 611202 493 611757 12 611757 164 611757 179 611757 321 611757 336 611757 403 611757 687 611757 691 611757 692 611757 733 611757 734 611757 742 611757 502 611757 509 611757 586 611757 596 611757 623 611757 624 611757 415 611757 425 611757 459 611757 476 611757 490 611757 493 611895 12 611895 164 611895 179 611895 321 611895 336 611895 403 611895 687 611895 691 611895 692 611895 733 611895 734 611895 742 611895 502 611895 509 611895 586 611895 596 611895 623 611895 624 611895 415 611895 425 611895 459 611895 476 611895 490 611895 493 611896 12 611896 164 611896 179 611896 321 611896 336 611896 403 611896 687 611896 691 611896 692 611896 733 611896 734 611896 742 611896 502 611896 509 611896 586 611896 596 611896 623 611896 624 611896 415 611896 425 611896 459 611896 476 611896 490 611896 493 611897 12 611897 164 611897 179 611897 321 611897 336 611897 403 611897 687 611897 691 611897 692 611897 733 611897 734 611897 742 611897 502 611897 509 611897 586 611897 596 611897 623 611897 624 611897 415 611897 425 611897 459 611897 476 611897 490 611897 493 61But my original query correctly gives 47 rows.DECLARE @SelectedFunds TABLE(pshipid nvarchar(100))insert into @SelectedFundsSELECT DISTINCT pshipid FROM GroupPshipAccess GPA INNER JOIN UserGroup UG ON GPA.UserGroupID = UG.UserGroupIDINNER JOIN UserGroupMember UGM ON GPA.UserGroupID = UGM.UserGroupIDWHERE UserGroupName = 'BLUECREST'group by pshipidselect * from @SelectedFundsexample4266066174374475075175275376077577677778178879379479779879980780881381482282382483183283383483585485585685790396396410451200120112021757189518961897Thanks for your help.Regards,sqlnovice123 |
 |
|
sqlnovice123
Constraint Violating Yak Guru
262 Posts |
Posted - 2011-02-14 : 11:41:42
|
I think this is the way I can set teh parameter passed from to the stored proc and get valid results.SELECT DISTINCT GPA.pshipid, GPA.UserGroupID, UserID FROM GroupPshipAccess GPA INNER JOIN UserGroup UG ON GPA.UserGroupID = UG.UserGroupIDINNER JOIN UserGroupMember UGM ON UG.UserGroupID = UGM.UserGroupID WHERE UserGroupName = @Groupgroup by GPA.pshipid, GPA.UserGroupID, UGM.UserIDendSELECT @UserID = UserID from @SelectedFundsBy setting the @UserID, I am ensuring that only users that have access to the ids(etermined by the UserID in the UserMemberGroup table). The table @SelectedFunds has rows inserted from the UserGroupMember table. |
 |
|
sqlnovice123
Constraint Violating Yak Guru
262 Posts |
Posted - 2011-02-15 : 13:39:37
|
I was able to get this to work. Thanks.Sqlnovice123 |
 |
|
|
|
|
|
|