RIAS
asked on
SQL query with a condtion
Hello,
How to get an array of result and use the array in another query in a stored procedure.
Please find the scenerio in the attachment.
Regards,
Example1.xlsx
How to get an array of result and use the array in another query in a stored procedure.
Please find the scenerio in the attachment.
Regards,
Example1.xlsx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Can you please clarify with example
Note duplicate apostrophes around the XYZ. You'll probably replace ''XYZ'' by a variable.
I am a bit confused on what to do about this.
Note duplicate apostrophes around the XYZ. You'll probably replace ''XYZ'' by a variable.
I am a bit confused on what to do about this.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If the values are in a table, leave them there. Just use the table directly. A bit confusing since "userID from tableUsers" doesn't exist (convenient here, actually, so the "UserID" column doesn't need a table alias), I'm assuming you mean "ID".
SET @SQL = @SQL + ' AND EXISTS(SELECT 1 FROM dbo.TableUsers TU WHERE TU.UserGroupName = ''XYZ'' AND TU.ID = UserID) '
SET @SQL = @SQL + ' AND EXISTS(SELECT 1 FROM dbo.TableUsers TU WHERE TU.UserGroupName = ''XYZ'' AND TU.ID = UserID) '
ASKER
Thanks Scott,
Why are you selecting only 1
(SELECT 1 FROM dbo.TableUser
I need to get an array of userID which have groupname ='XYZ'
Why are you selecting only 1
(SELECT 1 FROM dbo.TableUser
I need to get an array of userID which have groupname ='XYZ'
The difference is in EXISTS and IN operator. Scott's variant contains UserID in WHERE clause.
ASKER
Thanks
ASKER
I don't know why the Stored Procedure is not picking this if condition:
IF NOT (@IsPowerUser=1 OR @IsAdmin=1)
IF NOT (@IsPowerUser=1 OR @IsAdmin=1)
ASKER
Is the query syntax wrong?
Why do you think so?
Are you sure the variable values are not NULL?
Are you sure the variable values are not NULL?
ASKER
Yes, I have set them as false .
When I remove this IF condition it works fine .
When I remove this IF condition it works fine .
ASKER
Please refer to my SP in the example attached.
The example is rather incomplete… but you may try to add following commands below the Query:
IF @IsAdmin IS NULL
SET @IsAdmin = 0
IF @IsPowerUser IS NULL
SET @IsPowerUser = 0
IF @IsAdmin IS NULL
SET @IsAdmin = 0
IF @IsPowerUser IS NULL
SET @IsPowerUser = 0
ASKER
ALTER PROCEDURE [dbo].[RecentJobs_St] (
@SelectedDateTime DATETIME
, @AccountName NVARCHAR(200)
)
AS
BEGIN
SET NOCOUNT ON
DECLARE @SQL NVARCHAR(2000)
DECLARE @IsPowerUser BIT
DECLARE @IsAdmin BIT
DECLARE @UserID INT
-- This query gets a UserID if the user is a power user or an admin
-- If they are then they can see all records, otherwise just their own.
SELECT @UserID = ID
, @IsAdmin = IsAdmin
, @IsPowerUser = IsPowerUser
FROM Users U
WHERE U.AccountName = @AccountName
SET @SQL = 'SELECT TOP 50 *
FROM JOB_Recnt R
WHERE CONVERT(NVARCHAR(20),R.[Date Time],112) <= ' + CONVERT(NVARCHAR(20),@SelectedDateTime,112) + ' '
IF NOT (@IsPowerUser=1 OR @IsAdmin=1)
SET @SQL = @SQL + ' AND UserID IN (SELECT ID FROM USERS WHERE UserGroupName = ''XYZ'')'
SET @SQL = @SQL + ' ORDER BY CAST([Date Time] AS datetime) DESC '
ASKER
Here you go with the complete SP.
Can you suggest where can I add the IF condition please
Can you suggest where can I add the IF condition please
ASKER
It worked mate!!!
ASKER
Thanks!
ALTER PROCEDURE [dbo].[RecentJobs_St] (
@SelectedDateTime DATETIME
, @AccountName NVARCHAR(200)
)
AS
BEGIN
SET NOCOUNT ON
DECLARE @SQL NVARCHAR(2000)
DECLARE @IsPowerUser BIT
DECLARE @IsAdmin BIT
DECLARE @UserID INT
-- This query gets a UserID if the user is a power user or an admin
-- If they are then they can see all records, otherwise just their own.
SELECT @UserID = ID
, @IsAdmin = IsAdmin
, @IsPowerUser = IsPowerUser
FROM Users U
WHERE U.AccountName = @AccountName
IF @IsAdmin IS NULL
SET @IsAdmin = 0
IF @IsPowerUser IS NULL
SET @IsPowerUser = 0
SET @SQL = 'SELECT TOP 50 *
FROM JOB_Recnt R
WHERE CONVERT(NVARCHAR(20),R.[Date Time],112) <= ' + CONVERT(NVARCHAR(20),@SelectedDateTime,112) + ' '
IF NOT (@IsPowerUser=1 OR @IsAdmin=1)
SET @SQL = @SQL + ' AND UserID IN (SELECT ID FROM USERS WHERE UserGroupName = ''XYZ'')'
SET @SQL = @SQL + ' ORDER BY CAST([Date Time] AS datetime) DESC '
ASKER
Scott thanks for your help!!
You are welcome
ASKER
Hello pcelba,
Small problem
Small problem
ASKER
Now I have made the IsAdmin and IsPoweruser = True but still the query is picking up the if condition
The best you can do here is to add PRINT commands inside your SP and call the SP from SSMS.
You should print the @UserID after the first query
You should print the final SQL command
You may print the values of @IsAdmin and @IsPowerUser
SQL Server does not know True or False. Bit values are 0, 1, or possibly NULL which means undefined.
You should print the @UserID after the first query
You should print the final SQL command
You may print the values of @IsAdmin and @IsPowerUser
SQL Server does not know True or False. Bit values are 0, 1, or possibly NULL which means undefined.
ASKER