dirklieske
Programmer
I would like to do the following in SQL: (My goal is to be as efficient as possible in a View not a macro or procedure)
IF (Userid = 'BOB')
SELECT *
FROM table A
WHERE Condition = 1
IF (UserID = 'BILL')
SELECT *
FROM table A
WHERE condition = 2
I know I could write the above as:
SELECT *
FROM table A
WHERE (USERID = 'BILL' AND condition = 1)
OR (USERID = 'BOB' AND condition = 2)
I could also write the above as:
SELECT *
FROM table A
WHERE USERID = 'BILL AND condition = 1
UNION
SELECT *
FROM table A
WHERE USERID = 'BOB' and condition = 2;
I could also write the above as:
SELECT *
FROM table A
WHERE condition = CASE (WHEN USERID = 'BILL'
THEN 1
ELSE 2);
Again my goal is to first evaluate the USERID before firing any other SQL. I do not want the optimizer to think about anything other then the USERID. (A final approach could be to change all views depending on the user that logs on)
BILL LOGS on the following fires:
replace view EXAMPLE AS
SELECT *
FROM TABLE A
WHERE CONDITION = 1;
BOB LOGS on the following fires:
replace view EXAMPLE AS
SELECT *
FROM TABLE A
WHERE CONDITION = 2;
IF (Userid = 'BOB')
SELECT *
FROM table A
WHERE Condition = 1
IF (UserID = 'BILL')
SELECT *
FROM table A
WHERE condition = 2
I know I could write the above as:
SELECT *
FROM table A
WHERE (USERID = 'BILL' AND condition = 1)
OR (USERID = 'BOB' AND condition = 2)
I could also write the above as:
SELECT *
FROM table A
WHERE USERID = 'BILL AND condition = 1
UNION
SELECT *
FROM table A
WHERE USERID = 'BOB' and condition = 2;
I could also write the above as:
SELECT *
FROM table A
WHERE condition = CASE (WHEN USERID = 'BILL'
THEN 1
ELSE 2);
Again my goal is to first evaluate the USERID before firing any other SQL. I do not want the optimizer to think about anything other then the USERID. (A final approach could be to change all views depending on the user that logs on)
BILL LOGS on the following fires:
replace view EXAMPLE AS
SELECT *
FROM TABLE A
WHERE CONDITION = 1;
BOB LOGS on the following fires:
replace view EXAMPLE AS
SELECT *
FROM TABLE A
WHERE CONDITION = 2;