I have table users
x
Userid flag
145 1
142 0
a second table Sales
salesId date amount
252 01/01/2021 125
23 01/02/2021 300
I need to ckeck each time for user parameter the flag to display or not the data fOr the user 145 I’ll display everything in the table sales because flag=1 but the user 142 I’ll disply nothing beacuse flag =0
CREATE PROCEDURE Sales (@userID nvarchar(30))
AS
SELECT * FROM sales
cross join users
WHERE UserID= @userID and flag =1
GO;
The result is incorrect, how to correct it
Advertisement
Answer
Your version would work with select sales.*
. However, I would use:
select s.*
from sales s
where exists (select 1
from users u
where u.UserId = @UserId and u.flag = 1
);
This makes it clear that users
is just being used for filtering and that there really isn’t a relationship between the two tables (other than the permissioning, of course).