10 | Programming with Transact-SQL

Slides:



Advertisements
Similar presentations
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Advertisements

Μαθαίνοντας Python [Κ4] ‘Guess the Number’
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
NAVY Research Group Department of Computer Science Faculty of Electrical Engineering and Computer Science VŠB-TUO 17. listopadu Ostrava-Poruba.
Lecture-5 Though SQL is the natural language of the DBA, it suffers from various inherent disadvantages, when used as a conventional programming language.
True BASIC Ch. 6 Practice Questions. What is the output? PRINT X LET X = -1 PRINT X FOR X = 4 TO 5 STEP 2 PRINT X NEXT X PRINT X END.
Module 10: Implementing User-defined Functions. Overview What Is a User-defined Function? Defining Examples.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Module 2: Using Transact-SQL Querying Tools. Overview SQL Query Analyzer Using the Object Browser Tool in SQL Query Analyzer Using Templates in SQL Query.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
Programming With Alice. Alice Free Java based, 3D programming tool Enables the manipulation and interaction of 3D objects Can also.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Dexterity | CONFIDENTIAL 2009 MRO | Analytics | Insights 1 Stored Procedures.
Module 1: Introduction to Transact-SQL
T-SQL Transact-SQL is microsoft implementation of SQL. It contains additional programming constracts T-SQL enables you to write programs that contain SQL.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Graeme Malcolm | Senior Content Developer, Microsoft Geoff Allix | Principal Technologist, Content Master.
04 | Grouping and Aggregating Data Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
Deanne Larson.  Variables allow you to store data values temporarily for later use in the same batch where they were declared. I describe batches later.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
10 | Programming with Transact-SQL Graeme Malcolm | Senior Content Developer, Microsoft Geoff Allix | Principal Technologist, Content Master.
Module 3: Performing Connected Database Operations.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
JAVA PROGRAMMING Control Flow. Jeroo: Finish Activities 1-7 Finish InputTest program w/changes.
Presentation By :- Nikhil R. Anande ( ) Electronic & Communication Engineering. 3 nd Year / 5 th Semester FACULTY GUIDE : RAHIUL PATEL SIR MICROCONTROLLER.
In this session, you will learn to: Create and manage views Implement a full-text search Implement batches Objectives.
Module 9: Implementing Functions. Overview Creating and Using Functions Working with Functions Controlling Execution Context.
Module 9: Implementing User-Defined Functions. Overview Introducing User-Defined Functions Implementing User-Defined Functions.
T-SQL Scripts Advanced Database Dr. AlaaEddin Almabhouh.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Querying with Transact-SQL
Information and Computer Sciences University of Hawaii, Manoa
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
02 | Advanced SELECT Statements
11 | Error Handling and Transactions
Programmability by Adrienne Watt.
PROCEDURES, CONDITIONAL LOGIC, EXCEPTION HANDLING, TRIGGERS
09 | Modifying Data Graeme Malcolm | Senior Content Developer, Microsoft Geoff Allix | Principal Technologist, Content Master.
05 | Using Functions and Aggregating Data
03 | Querying Multiple Tables with Joins
Using Encoders to go Straight
Introduction to T-SQL Querying
06 | Using Subqueries and APPLY
11/10/2018.
04 | Using Set Operators Graeme Malcolm | Senior Content Developer, Microsoft Geoff Allix | Principal Technologist, Content Master.
09 | Modifying Data Graeme Malcolm | Senior Content Developer, Microsoft Geoff Allix | Principal Technologist, Content Master.
02 | Querying Tables with SELECT
CS122B: Projects in Databases and Web Applications Spring 2017
CS122B: Projects in Databases and Web Applications Winter 2017
07 | Using Table Expressions
Exploring Microsoft Excel
An Introduction to Java – Part I, language basics
Module 2: Understanding C# Language Fundamentals
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Information Management
Python While Loops.
02 | Querying Tables with SELECT
Introduction to T-SQL Querying
Presentation transcript:

10 | Programming with Transact-SQL Graeme Malcolm | Senior Content Developer, Microsoft Geoff Allix | Principal Technologist, Content Master

Module Overview Batches Comments Variables Conditional Branching Loops Stored Procedures

Batches Batches are sets of commands sent to SQL Server as a unit Batches determine variable scope, name resolution To separate statements into batches, use a separator: SQL Server tools use the GO keyword GO is not a T-SQL command! GO [count] executes the batch the specified number of times SELECT * FROM Production.Product; SELECT * FROM Sales.Customer; GO

Comments Marks T-SQL code as a comment: For a block, enclose it between /* and */ characters For inline text, precede the comments with -- T-SQL Editors typically color-code comments, as shown above /* This is a block of commented code */ -- This line of text will be ignored

Variables Variables are objects that allow storage of a value for use later in the same batch Variables are defined with the DECLARE keyword Variables can be declared and initialized in the same statement Variables are always local to the batch in which they're declared and go out of scope when the batch ends --Declare and initialize variables DECLARE @color nvarchar(15)='Black', @size nvarchar(5)='L'; --Use variables in statements SELECT * FROM Production.Product WHERE Color=@color and Size=@size; GO

Using Variables

Conditional Branching IF…ELSE uses a predicate to determine the flow of the code The code in the IF block is executed if the predicate evaluates to TRUE The code in the ELSE block is executed if the predicate evaluates to FALSE or UNKNOWN IF @color IS NULL SELECT * FROM Production.Product; ELSE SELECT * FROM Production.Product WHERE Color = @color;

Using IF…ELSE

Looping WHILE enables code to execute in a loop Statements in the WHILE block repeat as the predicate evaluates to TRUE The loop ends when the predicate evaluates to FALSE or UNKNOWN Execution can be altered by BREAK or CONTINUE DECLARE @custid AS INT = 1, @lname AS NVARCHAR(20); WHILE @custid <=5 BEGIN SELECT @lname = lastname FROM Sales.Customer WHERE customerid = @custid; PRINT @lname; SET @custid += 1; END;

Demo: Using WHILE

Stored Procedures Database objects that encapsulate Transact-SQL code Can be parameterized Input parameters Output parameters Executed with the EXECUTE command CREATE PROCEDURE SalesLT.GetProductsByCategory (@CategoryID INT = NULL) AS IF @CategoryID IS NULL SELECT ProductID, Name, Color, Size, ListPrice FROM SalesLT.Product ELSE WHERE ProductCategoryID = @CategoryID; EXECUTE SalesLT.GetProductsByCategory 6;

Creating a Stored Procedure

Programming with Transact-SQL Batches Comments Variables Conditional Branching Loops Stored Procedure Lab: Programming with Transact-SQL