TSQL Querying with Many Results

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;

-- Customer Service User
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]
                )

-- Marketing User
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]
                )

-- Remote User
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]
                )
+4
source share
1 answer

, proc , , , union select NULL , . , , , where:

ALTER PROCEDURE [dbo].[Login]

@userId varchar(20)
AS BEGIN SET NOCOUNT ON;

-- Customer Service User
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 = @userId
    and not exists(select 1
                   from remote
                   where id = @userId)
union all
-- Marketing User
SELECT
    mktg.id,
    first_name,
    last_name,
    logins.[email_address],
    logins.[password],
    phone,
    null as shift,
    manager,
    location
FROM marketing mktg
    INNER JOIN logins ON mktg.id = logins.[id]
WHERE mktg.id = @userId
union all
-- Remote User
SELECT
    ru.id,
    first_name,
    last_name,
    logins.[email_address],
    logins.[password],   
    null as phone,
    null as shift,                                 
    manager,
    location
FROM remote ru
INNER JOIN logins ON ru.id = logins.[id]
WHERE ru.id = @userId

end
+1

Source: https://habr.com/ru/post/1662441/


All Articles