Presentation is loading. Please wait.

Presentation is loading. Please wait.

Case Statements and Functions

Similar presentations


Presentation on theme: "Case Statements and Functions"— Presentation transcript:

1 Case Statements and Functions

2 CASE Statement CASE WHEN grade < 2.0 THEN SET status = 'Failing'; WHEN grade <= 3.5 THEN SET status = 'Passing'; ELSE SET status = 'Rocking!'; END CASE; CASE statement are used for conditional expression. There are two forms, the first (shown above) tests until a WHEN clause is true, the THEN clause is executed.

3 CASE Statement (Alternate form)
CASE status WHEN 'Failing' THEN SET come_talk = 1; WHEN 'Passing' THEN SET come_talk = 0; ELSE SET come_talk = 0; END CASE; This is the second form of a CASE STATEMENT. The expression between CASE and WHEN is evaluated, and each WHEN clause is tests for equality. If equal, the THEN clause is executed.

4 CASE Expression You can also use CASE as an expression.
UPDATE students SET come_talk = CASE WHEN grade < 2.0 THEN 1; WHEN grade > 4.0 THEN 2; ELSE 0; END CASE; ;

5 CASE Expression (Alternate Form)
UPDATE students SET come_talk = CASE status WHEN 'Passing' THEN 1; WHEN 'Rocking!' THEN 2; ELSE 0; END CASE; ;

6 CASE statements and expressions
You can only use CASE statements in stored procedures. But you can use CASE expressions anywhere you can use a value. So, SQLite only supports CASE expressions, not CASE statements.

7 Where can you use variables and CASE statements?
In any legal SQL statement Only in stored procedures Only in stored procedures and triggers I can use them anywhere, they just might not work

8 Functions We've seen functions in SQL before (e.g. "date"), they take 0 or more arguments and return one result. Here are some more: abs(x) - returns the absolute value of its argument lower(x) - returns a copy of a string in lower case upper(x) - guess what this does random(x) - returns an random, signed 64-bit integer typeof(x) - returns the type ("null", "integer", "real", "text", "blob") of x round(x, y) - returns a floating-point value x rounds to y digits after the decimal point trim(x, y) - returns a copy of string x with y characters removed from each end

9 User Defined Functions
What if we wanted to add a custom function to SQLite? title(x) - returns the string x in "Title Case" (the first letter of each word is capitalized). We first write a function (in Python) that does what we want: def make_title(x): return x.title() Then we need to tell our connection to the database that this function exists: conn.create_function("title", 1, make_title) Now we can use it in SQL queries: conn.execute("SELECT title(name) FROM students;")

10 create_function The create_function method on the connection object takes three parameters: name - a string giving the name the function will be called by in SQL num_params - the number of parameters the function requires You can overload functions with different number of arguments func - the function (in Python) to be called The function can return any of the SQLite supported types (bytes, str, int, float, and None)

11 Functions versus Stored Procedures
Functions must return a value, stored procedures can "return" 0 or more values. Functions only have "in" arguments, whereas stored procedures can have "in", "out" and "inout" arguments. Stored procedures can manipulate the database directly (SELECT, UPDATE, INSERT, DELETE), where functions can only return a value.


Download ppt "Case Statements and Functions"

Similar presentations


Ads by Google