Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Technical Questions
Message Icon Topic: Need help with Stored Procedure quickly Post Reply Post New Topic
Author Message
rusty
Newbie
Newbie
Avatar

Joined: 07 Mar 2008
Location: United States
Online Status: Offline
Posts: 16
Quote rusty Replybullet Topic: Need help with Stored Procedure quickly
     Posted: 22 May 2008 at 10:36am
Hi, I am a XI report developer with beginner SQL skills. I need to write the stored proc that will do the following. Any who is more skilled, please help as I need this ASAP. Thanks
 
CREATE  PROCEDURE rpt_Security
( @UserName varchar(50)
)
AS
/* Below is ALL the tables and columns that I need for this Stored Procedure. Depending upon the Access LEvel, they see Differnt Companies.*/
Select
T1.UserName,
T1.UserName2,
T1.AccessLvl,
T2.UserName,
T2.Company,
T2.Division,
T2.AccessType,
T3.CompanyID,
T3.CompanyName
FROM
dbo.Users T1
JOIN dbo.UserSec T2
ON T1.UserName2 = T2.UserName
JOIN dbo.Company T3
ON T2.Company = T3.CompanyName
Where
T1.AccessLvl IN ('A', 'U')
/* If T1.AccessLvl = 'A', then it should execute the following code and return the user name that meets this criteria.
If T1.AccessLvl = 'A'
                        BEGIN
                             Select c.CompanyID
    From dbo.Company c
   Where  c.CompanyID IN (Select CompanyID from dbo.Company)
                        END*/
/*If T1.AccessLvl = 'U', then it should execute the following code and return the user name that meets this criteria.
  BEGIN
                             Select a.UserName2, a.AccessLvl, b.UserName, b.Company, b.Division, c.CompanyID, c.CompanyName
    From dbo.Users a
   JOIN dbo.UserSec b
   ON a.UserName2 = b.UserName
   JOIN dbo.Company c
   ON b.Company = c.CompanyName
    Where  c.CompanyID IN (Select CompanyID from dbo.Users U
      Join UserSec S
      ON U.UserName2 = S.UserName)
                        END*/
GO
IP IP Logged
Lugh
Senior Member
Senior Member
Avatar

Joined: 14 Nov 2007
Online Status: Offline
Posts: 377
Quote Lugh Replybullet Posted: 23 May 2008 at 5:11am
Well, your first mistake is trying to write SQL code like it's VB code.  SQL process sets, not records.  It's a whole different paradigm.  Don't try to apply conditions to each record as you step through the recordset.  Apply conditions to groups of records.

First, I need to understand your conditions.  If I'm reading this correctly, then any user with access level 'A' simply gets access to all companies, but they won't necessarily have all the companies listed in the UserSec table (which is creating an issue with the join).  If the user has access level 'U' then they will have the appropriate companies listed in the UserSec table, and need only those companies returned.

Given the complication with the join in the case of A, I think you might be better off with a UNION query, treating them as two separate cases.

Let's see if I can take a crack at this.


SELECT T1.UserName, T1.UserName2, T1.AccessLvl, T3.CompanyID, T3.CompanyName
FROM dbo.Users T1
JOIN dbo.Company T3
WHERE T1.AccessLevel = 'A'

UNION

SELECT T1.UserName, T1.UserName2, T1.AccessLvl, T3.CompanyID, T3.CompanyName
FROM dbo.Users T1
JOIN dbo.UserSec T2
ON T1.UserName2 = T2.UserName
JOIN dbo.Company T3
ON T2.Company = T3.CompanyName
WHERE T1.AccessLvl = 'U'


Let me know if that works.


IP IP Logged
rusty
Newbie
Newbie
Avatar

Joined: 07 Mar 2008
Location: United States
Online Status: Offline
Posts: 16
Quote rusty Replybullet Posted: 30 May 2008 at 11:25am
Thanks for the help. But it did not work!
IP IP Logged
CrystalNewbie
Newbie
Newbie


Joined: 30 May 2008
Online Status: Offline
Posts: 3
Quote CrystalNewbie Replybullet Posted: 30 May 2008 at 2:42pm
Are you trying to run two different queries based on what security level is passed by the stored proc?
IP IP Logged
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum