Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programmability by Adrienne Watt.

Similar presentations


Presentation on theme: "Programmability by Adrienne Watt."— Presentation transcript:

1 Programmability by Adrienne Watt

2 Objectives Working with variables Assigning values to variables
7/1/2018 Objectives 2 Working with variables Assigning values to variables

3 Variables either local or global
3 either local or global local variables are defined with the DECLARE statement global variables are defined and maintained by SQL Server Local Variables sign preceding their name cannot be defined as text or image given values with a SELECT statement Local variables are often used in a batch or procedure as counters for WHILE loops or for IF...ELSE blocks.

4 Variables 4 4 datatype datatype...] This example creates variable, places a string value into the variable, and prints the value of variable. char(20) = 'This is a test' GO

5 Local variables 5 This example creates a local variable and uses this local variable in a SELECT statement to find all author first and last names where the author resides in the state of Utah. USE pubs GO char(2) = 'UT' SELECT RTRIM(au_fname) + ' ' + RTRIM(au_lname) AS Name, state FROM authors WHERE state

6 Variables 6 This example uses a query to assign a value to a variable. USE Northwind GO int = (SELECT COUNT(*) FROM Customers) COMP Introduction to Databases

7 Variable Declaration 7 The following example sets the aNumRows variable to the record count in the employee table. USE Pubs GO int = COUNT(*) FROM employee

8 Select Assignment 8 You cannot use a select statement that assigns values to variables to also return data to the user. The first select statement in the following example assigns the maximum price to the local the second select statement is needed to display the value: MONEY = MAX(price) FROM titles

9 Control-of-Flow Keyword Description BEGIN...END
9 Keyword Description BEGIN...END Defines a statement block. GOTO label Continues processing at the statement following the label as defined by label. IF...ELSE Defines conditional, and optionally, alternate execution when a condition is false. RETURN Exits unconditionally. WHILE Repeats statements while a specific condition is true. ...BREAK Exits the innermost WHILE loop. ...CONTINUE Restarts a WHILE loop.

10 Control-of-Flow 10 GOTO Example GOTO PrintHere ‘—the following statement will be bypassed (never executed) SELECT * FROM authors PrintHere: Print ‘The SELECT statement will not be executed.’ GO

11 Control-of-Flow 11 In addition to the keywords used for conditional processing and control-of-flow for SQL statements, a variety of helpful Transact-SQL extensions and ANSI-standard additions are available (for example): CASE Expression Allows an expression to have conditional return values. Comments Inserts a comment anywhere in an SQL statement. (/* and */) OR (--) DECLARE Statement Declares local variables PRINT Statement Prints a user-defined message on the message pane.

12 WHILE Example -wont exist the first time we run this
drop table myTitles go SELECT * INTO myTitles FROM titles SELECT AVG(price) FROM myTitles WHILE (SELECT AVG(price) FROM myTitles) < 15 BEGIN UPDATE myTitles SET price = price * 2 IF (SELECT AVG(price) FROM myTitles) > 20 BREAK ELSE CONTINUE END -wont exist the first time we run this -create a table from titles

13 Control-of-Flow Example of Case SELECT StateName = CASE state
SELECT StateName = CASE state WHEN 'CA' THEN 'California' WHEN 'KS' THEN 'Kansas' WHEN 'TN' THEN 'Tennessee' WHEN 'MI' THEN 'Minnesota' END FROM Authors GO

14 Control-of-Flow 14 SELECT Category = CASE type WHEN 'popular_comp' THEN 'Popular Computing' WHEN 'mod_cook' THEN 'Modern Cooking' WHEN 'business' THEN 'Business' WHEN 'psychology' THEN 'Psychology' WHEN 'trad_cook' THEN 'Traditional Cooking' ELSE 'Not yet categorized' END, CAST(title AS varchar(25)) AS 'Shortened Title', price AS Price FROM titles WHERE price IS NOT NULL ORDER BY type, price GO

15 Control-of-Flow 15 IF...ELSE with a Statement Block IF (SELECT AVG(price) FROM titles) < 15 BEGIN UPDATE titles SET price = price * 2 SELECT title, price FROM titles WHERE price > 28 END


Download ppt "Programmability by Adrienne Watt."

Similar presentations


Ads by Google