Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL stored procedures use condition from one table

For this stored procedure, how to add a condition for value Location?

If tblEmployees.Office = USA and tblEmployees.state = West then Location = CA, else Location = others:

CREATE PROCEDURE spEmployee
AS
BEGIN
    SELECT 
        EmployeeId, Name, Gender, DepartmentName, Location
    FROM 
        tblEmployees
    INNER JOIN 
        tblDepartments ON tblEmployees.EmployeeDepartmentId = tblDepartments.DepartmentId
END

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

That’s a simple enough CASE expression – something like this:

CREATE PROCEDURE spEmployee
AS
BEGIN
    SELECT 
        EmployeeId, Name, Gender, DepartmentName, 
        Location = CASE
                       WHEN Office = 'USA' AND State = 'West' THEN 'CA'
                       ELSE 'others'
                   END
    FROM 
        tblEmployees e
    INNER JOIN 
        tblDepartments d ON e.EmployeeDepartmentId = d.DepartmentId
END
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading