I have 3 tables where the identifier can be in table 1, table 2, table 3 or in table 1 and table 3.
What I need to do is pass the identifier of the stored procedure as a parameter, and then check 3 tables and return a result set where the identifier is found in 1 of 3 tables.
In case the identifier is found both in table 1 and in table 3, I need to return the result set for table 3.
All tables DO NOT contain the same number of columns. I can't seem to build it right.
This is my stored procedure:
ALTER PROCEDURE [dbo].[Login]
@userId varchar(20)
AS
BEGIN
SET NOCOUNT ON;
IF EXISTS
(
SELECT cs.id,
first_name,
last_name,
logins.[email_address],
logins.[password],
phone,
shift,
manager,
location
FROM customer_service cs
INNER JOIN logins ON cs.id = logins.[id]
WHERE cs.id = logins.[id]
)
IF EXISTS
(
SELECT mktg.id,
first_name,
last_name,
logins.[email_address],
logins.[password],
phone,
manager,
location
FROM marketing mktg
INNER JOIN logins ON mktg.id = logins.[id]
WHERE mktg.id = logins.[id]
)
IF EXISTS
(
SELECT ru.id,
first_name,
last_name,
logins.[email_address],
logins.[password],
manager,
location
FROM remote ru
INNER JOIN logins ON ru.id = logins.[id]
WHERE ru.id = logins.[id]
)
source
share