I want to write a stored procedure with if condition
I have two tables
<<
IF is not a search condition; it is a control flow statement. But SQL is a declarative language and has no control flow. On a scale from 1 to 10, what color is your favorite letter of the alphabet? This is how silly you are being.
Where is the DDL for these two tables? You did not post any! So we have to guess and type for you! why are you so rude? Did you read the part about Netiquette at the front of the forum?
The ASCII pictures you posted look like you never read a book on RDBMS. You do not know the ISO 11179 rules for data element names, and you seem to be using bit flag! We do not use bit flags in SQL; that was assembly language in the 1950s and 60s.
CREATE TABLE User_Passwords
(user_name VARCHAR(255) NOT NULL,
password_start_date DATE NOT NULL,
password_expiry_date DATE NOT NULL,
CHECK (password_start_date < passeword_expiry_date),
PRIMARY KEY (user_name, password_start_date),
password_txt VARCHAR(255) NOT NULL
);
INSERT INTO User_Passwords
VALUES
('Lekshmi', 'baiju ', '2015-01-01', '2016-12-31'),
('Jain', 'rkjain', '2015-01-01', '2016-12-31'),
('Ram', 'ram123', '2015-01-01', '2016-12-03');
The term "master" is not part of RDBMS. It is part of tape files and network databases. Also, both these non-tables need keys! Her are mny guesses. But where the DRI that would reference the user names?
CREATE TABLE Bus_Routes
(user_name VARCHAR(255) NOT NULL,
bus_route_name CHAR(2) NOT NULL,
PRIMARY KEY (user_name, bus_roue_name)
INSERT INTO Bus_Routes
VALUES
('Jain', 'B1'),
('Jain', 'B2'),
('Ram', 'R1');
Your requirements are for bad 1950's COBOL, not SQL. We do not do step-by-step computing in a declarative language. Do not use the “sp_” prefix on a procedure names; Google what it means in T-SQL.
CREATE PROCEDURE Something_login
(@in_user_name VARCHAR(255),
@in_password_txt VARCHAR(255))
SELECT P.user_name, B.bus_route_name
FROM User_Passwords AS P
LEFT OUTER JOIN
Bus_Routes AS B
ON @in_user_name = P.user_name
AND @in_user_name = B.user_name
AND @in_password_txt = P.password_txt
AND CAST(CURRENT_TIMESTAMP AS DATE)
BETWEEN P.password_start_date
AND P.password_expiry_date;
We hate local variables in declarative languages; we use the expressions that return values directly in the code. But, even given that, why do keep creating more and more redundant local variables? Why are 1 and 3, 0 and 2 totally different? Looks like the same non-RDBMS flags to me. next condition is IF active = 1 check that user_name exists in Bus_Routes it exists there then return 4 else 3<<
Also the PRINT statement is for debugging only. In RDBMS, we have a presentation layer; we pass a result set (set, NOT flags!) from the database layer to those presentation layers.
Instead, this will give you the active user name and his bus route (if any). No flags, but actual values. See the difference in the mindset?