Module 02_Overview of Programming SQR Server

Embed Size (px)

Citation preview

  • 8/9/2019 Module 02_Overview of Programming SQR Server

    1/30

  • 8/9/2019 Module 02_Overview of Programming SQR Server

    2/30

    SQL Server Programming Tools

    SQL Query Analyzer

    Color-codes syntax elements automatically

    Multiple query windows

    Customizable views of result sets

    Graphical execution plans

    Execute portions of scripts

    osql Utility

    Command-line utility

  • 8/9/2019 Module 02_Overview of Programming SQR Server

    3/30

    The Transact-SQL Programming Language

    SQL Server Implementation of Entry-Level ANSI ISOStandard

    Can Be Run on Any Entry-Level Compliant Product Contains Additional Unique Functionality

  • 8/9/2019 Module 02_Overview of Programming SQR Server

    4/30

    Elements of Transact-SQL

    Data Control Language Statements

    Data Definition Language Statements

    Data Manipulation Language Statements

  • 8/9/2019 Module 02_Overview of Programming SQR Server

    5/30

    Data Control Language Statements

    Set or Change Permissions

    GRANT

    DENY

    REVOKE

    By Default, Only sysadmin, dbcreator, db_owner, anddb_securityadmin Roles Can Execute

  • 8/9/2019 Module 02_Overview of Programming SQR Server

    6/30

    Data Definition Language Statements

    Define the Database Objects

    CREATE object_type object_name

    ALTER object_type object_name

    DROP object_type object_name

  • 8/9/2019 Module 02_Overview of Programming SQR Server

    7/30

  • 8/9/2019 Module 02_Overview of Programming SQR Server

    8/30

    Naming Guidelines

    Use Meaningful Names Where Possible

    Keep Names Short

    Use a Clear and Simple Naming Convention

    Chose an Identifier That Distinguishes Types of Objects

    Views

    Stored procedures

    Keep Object Names and User Names Unique

  • 8/9/2019 Module 02_Overview of Programming SQR Server

    9/30

    Standard Identifiers

    Standard identifiers can contain from one to 128 characters, includingletters, symbols (_, @, or #), and numbers. No embedded spaces are

    allowed in standard identifiers.

    Rules for using identifiers include:

    . The first character must be an alphabetic character of a-z or A-Z.

    . After the first character, identifiers can include letters, numerals, or

    the @, $, #, or _.

    Identifier names starting with a symbol have special uses:

    An identifier beginning with the @ symbol denotes a local variable

    or parameter. An identifier beginning with a pound sign (#) denotes a temporary

    table or procedure. An identifier beginning with a double pound sign(##) denotes a global temporary object.

  • 8/9/2019 Module 02_Overview of Programming SQR Server

    10/30

    Delimited Identifiers

    Delimited identifiers can be used in the following situations:

    . When names contain embedded spaces

    . When reserved words are used for object names or portions of object

    names

    Delimited identifiers must be enclosed in brackets or double quotationmarks when they are used in Transact-SQL statements.

    . Bracketed identifiers are delimited by square brackets ([ ]):

    Quoted identifiers are delimited by double quotation marks (""):

  • 8/9/2019 Module 02_Overview of Programming SQR Server

    11/30

    Additional Language Elements

    Local Variables

    Operators

    Functions

    Function Examples

    Control of Flow Language Elements

    Comments

  • 8/9/2019 Module 02_Overview of Programming SQR Server

    12/30

    Local Variables

    User-defined with DECLARE Statement

    Assigned Values with SET or Select Statement

    DECLARE @vLastName char(20),@vFirstName varchar(11)

    SET @vLastName = 'Dodsworth'

    SELECT @vFirstName = FirstNameFROM Northwind..EmployeesWHERE LastName = @vLastName

    PRINT @vFirstName + ' ' + @vLastName

    DECLARE @vLastName char(20),@vFirstName varchar(11)

    SET @vLastName = 'Dodsworth'

    SELECT @vFirstName = FirstNameFROM Northwind..EmployeesWHERE LastName = @vLastName

    PRINT @vFirstName + ' ' + @vLastName

  • 8/9/2019 Module 02_Overview of Programming SQR Server

    13/30

    Operators

    Types of Operators

    Arithmetic

    Comparison

    String concatenation

    Logical

    Operator Precedence Levels

  • 8/9/2019 Module 02_Overview of Programming SQR Server

    14/30

    Comments

    In-Line Comments

    Block Comments

    SELECT ProductName,(UnitsInStock + UnitsOnOrder) AS Max -- Calculates inventory, SupplierIDFROM Products

    SELECT ProductName,(UnitsInStock + UnitsOnOrder) AS Max -- Calculates inventory, SupplierIDFROM Products

    /*** This code retrieves all rows of the products table** and displays the unit price, the unit price increased

    ** by 10 percent, and the name of the product.*/SELECT UnitPrice, (UnitPrice * 1.1), ProductNameFROM Products

    /*** This code retrieves all rows of the products table** and displays the unit price, the unit price increased

    ** by 10 percent, and the name of the product.*/SELECT UnitPrice, (UnitPrice * 1.1), ProductNameFROM Products

  • 8/9/2019 Module 02_Overview of Programming SQR Server

    15/30

    Ways to Execute Transact-SQL Statements

    Dynamically Constructing Statements

    Using Batches

    Using Scripts

    Using Transactions

    Using XML

  • 8/9/2019 Module 02_Overview of Programming SQR Server

    16/30

  • 8/9/2019 Module 02_Overview of Programming SQR Server

    17/30

    Using Batches

    One or More Transact-SQL StatementsSubmitted Together

    Define a Batch by Using the GO Statement

    How SQL Server Processes Batches

    You Cannot Combine Some Statements in a Batch

    CREATE PROCEDURE

    CREATE VIEW

    CREATE TRIGGER

    CREATE RULE

    CREATE DEFAULT

  • 8/9/2019 Module 02_Overview of Programming SQR Server

    18/30

    Using Scripts

    Contain Saved Statements

    Can Be Written in Any Text Editor

    Save by using .sql file name extension

    Execute in SQL Query Analyzer or osql Utility

    Use to Recreate Database Objects or to ExecuteStatements Repeatedly

  • 8/9/2019 Module 02_Overview of Programming SQR Server

    19/30

    Using Transactions

    Processed Like a Batch

    Data Integrity Is Guaranteed

    Changes to the Database Are Either Applied Together orRolled Back

    BEGIN TRANSACTIONUPDATE savings SET amount = (amount - 100)

    WHERE custid = 78910 UPDATE checking SET amount = (amount + 100)

    WHERE custid = 78910

    COMMIT TRANSACTION

    BEGIN TRANSACTIONUPDATE savings SET amount = (amount - 100)

    WHERE custid = 78910

    UPDATE checking SET amount = (amount + 100)WHERE custid = 78910

    COMMIT TRANSACTION

  • 8/9/2019 Module 02_Overview of Programming SQR Server

    20/30

    Check Your Understanding.

  • 8/9/2019 Module 02_Overview of Programming SQR Server

    21/30

    Q.1. What are the features of SQL Server QueryAnalyzer.?

  • 8/9/2019 Module 02_Overview of Programming SQR Server

    22/30

    Q.2. What are the elements of T-SQL .?

  • 8/9/2019 Module 02_Overview of Programming SQR Server

    23/30

    Q.3. Which SQL Commands are meant for DataControl language.?

  • 8/9/2019 Module 02_Overview of Programming SQR Server

    24/30

    Q.4. Which SQL Commands meant for Data DefinitionLanguage.?

  • 8/9/2019 Module 02_Overview of Programming SQR Server

    25/30

    Q.5. Which SQL Commands meant for DataManipulation Language.?

  • 8/9/2019 Module 02_Overview of Programming SQR Server

    26/30

    Q.6. What is delimited identifier.?

  • 8/9/2019 Module 02_Overview of Programming SQR Server

    27/30

    Q.7. What are the types of Comments available inSQL Server.?

  • 8/9/2019 Module 02_Overview of Programming SQR Server

    28/30

    Q.8. What is the Similarity between a Batch and aScript.?

  • 8/9/2019 Module 02_Overview of Programming SQR Server

    29/30

    Q.9. What is the Difference between a Batch and aScript.?

  • 8/9/2019 Module 02_Overview of Programming SQR Server

    30/30

    Thank You.