Tuesday, 9 June 2020

case sql

how to give details
CASE
creeate another column and write some description if one row is this print this statemnt in new column
    WHEN condition1 THEN result1
    WHEN condition2 THEN result2
    WHEN conditionN THEN resultN
    ELSE result
END;
SELECT OrderID, Quantity,
select two columns , add one column , write some statements, I f quantity is this reaspective row writes this, if column is this , write this in new column
CASE
    WHEN Quantity > 30 THEN 'The quantity is greater than 30'
    WHEN Quantity = 30 THEN 'The quantity is 30'
    ELSE 'The quantity is under 30'
END AS QuantityText
FROM OrderDetails;
table name
SELECT CustomerName, City, Country
FROM Customers
ORDER BY
(CASE
    WHEN City IS NULL THEN Country
    ELSE City
END);
to not write every time write procedure
CREATE PROCEDURE procedure_name
AS
sql_statement
GO;
EXEC procedure_name;
execute the programm
CREATE PROCEDURE SelectAllCustomers
AS
SELECT * FROM Customers
GO;
execute the procedure and u can give some more inputs at execution time
CREATE PROCEDURE SelectAllCustomers @City nvarchar(30)
AS
SELECT * FROM Customers WHERE City = @City
GO;
EXEC SelectAllCustomers @City = 'London';
whenever it matches this conditon that rows will execute
with two parameters
CREATE PROCEDURE SelectAllCustomers @City nvarchar(30), @PostalCode nvarchar(10)
AS
SELECT * FROM Customers WHERE City = @City AND PostalCode = @PostalCode
GO;
EXEC SelectAllCustomers @City = 'London', @PostalCode = 'WA1 1DP';
comments
--Select all:
SELECT * FROM Customers;
SELECT * FROM Customers -- WHERE City='Berlin';
/*Select all the columns
of all the records
in the Customers table:*/
SELECT * FROM Customers;



No comments:

Post a Comment