SlideShare a Scribd company logo
1 of 50
Implementing Views and Batches

Objectives
In this lesson, you will learn to:
 Create, alter, drop, and rename views
 Update data using views
 Declare variables
 Print messages
 Use comments
 Use conditional statements
 Use the iteration statements

                                          SQL/Lesson 8/Slide 1 of 50
©NIIT
Implementing Views and Batches

Getting Started
 Views
     A view is a virtual table, which gives access to a subset
      of columns from one or more tables
     Views ensure security of data by restricting access to:
        ® Specific   rows of a table
        ® Specific   columns of a table
        ® Specific   rows and columns of a table
        ® The   rows fetched using joins


                                                   SQL/Lesson 8/Slide 2 of 50
©NIIT
Implementing Views and Batches

Getting Started (Contd.)
        ®   The statistical summary of data in a given table
        ®   Subsets of another view or a subset of views and tables
 Views provide several advantages:
     Providing relevant data for users
     Hiding data complexity
     Organize data from heterogeneous sources
     Reducing the object size




                                                 SQL/Lesson 8/Slide 3 of 50
©NIIT
Implementing Views and Batches

8.D.1 Creating Views
 You need to view skills of various employees. Some of the
  queries to be executed are as follows:

    SELECT vFirstName, vSkill FROM Employee JOIN
    PositionSkill ON Employee.cCurrentPosition =
    PositionSkill.cPositionCode JOIN Skill ON
    PositionSkill.cSkillCode = Skill.cSkillCode
    SELECT vFirstName, vLastName,vSkill FROM
    Employee JOIN PositionSkill ON
    Employee.cCurrentPosition=PositionSkill.cPos
    itionCode


                                            SQL/Lesson 8/Slide 4 of 50
©NIIT
Implementing Views and Batches

8.D.1 Creating Views (Contd.)
    JOIN Skill ON PositionSkill.cSkillCode =
    Skill.cSkillCode
    SELECT vFirstName, vLastName, vSkill FROM
    Employee JOIN PositionSkill
    ON Employee.cCurrentPosition
    =PositionSkill.cPositionCode
    JOIN Skill ON PositionSkill.cSkillCode
    =Skill.cSkillCode
    AND vFirstName = 'Angela'


                                   SQL/Lesson 8/Slide 5 of 50
©NIIT
Implementing Views and Batches

8.D.1 Creating Views (Contd.)
    SELECT vFirstName, vLastName,
    vQualification, vSkill
    FROM Employee JOIN PositionSkill
    ON Employee.cCurrentPosition =
    PositionSkill.cPositionCode JOIN Skill
    ON PositionSkill.cSkillCode =
    Skill.cSkillCode
  Simplify the task of executing these queries.



                                             SQL/Lesson 8/Slide 6 of 50
©NIIT
Implementing Views and Batches

Task List
 Identify how to simplify queries
 Draft the query for the view
 Create the view
 Verify that the queries have been simplified by executing the
  required queries on the view




                                             SQL/Lesson 8/Slide 7 of 50
©NIIT
Implementing Views and Batches

Identify how to simplify queries
 Result:
     Simplification of the query can be done using views




                                             SQL/Lesson 8/Slide 8 of 50
©NIIT
Implementing Views and Batches

Draft the query for the view
 Action:
     The tables from where the view derives its data are
      Employee, positionSkill, and Skill
     The columns that are to be included in the view are
      vFirstName, vLastName, and vQualification from the
      Employee table and vSkill from the Skill table




                                             SQL/Lesson 8/Slide 9 of 50
©NIIT
Implementing Views and Batches

Create the view
 A view can be created using the CREATE VIEW statement
 Syntax
  CREATE VIEW view_name
  [(column_name [, column_name]...)]
  [WITH ENCRYPTION]
  AS select_statement [WITH CHECK OPTION]




                                        SQL/Lesson 8/Slide 10 of 50
©NIIT
Implementing Views and Batches

Create the view (Contd.)
 Action:
     In the Query Analyzer window, type:
      CREATE VIEW vwEmpSkill
      AS
      SELECT vFirstName, vLastName,
      vQualification, vSkill
      FROM Employee JOIN PositionSkill
      ON Employee.cCurrentPosition =
      PositionSkill.cPositionCode
      JOIN Skill
      ON PositionSkill.cSkillCode =
      Skill.cSkillCode
     Press F5 to execute the command
                                        SQL/Lesson 8/Slide 11 of 50
©NIIT
Implementing Views and Batches

Verify that the queries have been simplified by
executing the required queries on the view
 Action:
     In the Query Analyzer window, type:
        SELECT vFirstName, vSkill FROM vwEmpSkill
     Press F5 to execute the query
     In the Query Analyzer window, type:
        SELECT vFirstName, vLastName, vSkill
        FROM vwEmpSkill
     Press F5 to execute the query
                                            SQL/Lesson 8/Slide 12 of 50
©NIIT
Implementing Views and Batches

Verify that the queries have been simplified by
executing the required queries on the view (Contd.)
     In the Query Analyzer window, type:
        SELECT vFirstName,vLastName,vSkill
        FROM vwEmpSkill WHERE vFirstName =
        'Angela'
     Press F5 to execute the query
     In the Query Analyzer window, type:
        SELECT * FROM vwEmpSkill
     Press F5 to execute the query

                                            SQL/Lesson 8/Slide 13 of 50
©NIIT
Implementing Views and Batches

Just a Minute...
 Some queries to be executed are as follows:
  SELECT vFirstName, vDepartmentName
  FROM Employee JOIN Department
  ON Employee.cDepartmentCode =
  Department.cDepartmentCode


  SELECT vFirstName, cDesignation,
  vDepartmentName
  FROM Employee JOIN Department
  ON Employee.cDepartmentCode =
  Department.cDepartmentCode
                                          SQL/Lesson 8/Slide 14 of 50
©NIIT
Implementing Views and Batches

Just a Minute (Contd.)
SELECT vFirstName, vAddress, cCity, cZip,
cDesignation, vDepartmentName, vDepartmentHead
FROM Employee JOIN Department
ON Employee.cDepartmentCode =
Department.cDepartmentCode
WHERE cCity = 'Columbus'
Simplify the task of executing these queries.




                                                SQL/Lesson 8/Slide 15 of 50
©NIIT
Implementing Views and Batches

Altering, Dropping, and Renaming Views
 Altering Views
     You can modify a view by using the ALTER VIEW statement
     Syntax
        ALTER VIEW view_name [(column_name)]
        [WITH ENCRYPTION]
        AS select_statement
        [WITH CHECK OPTION]




                                         SQL/Lesson 8/Slide 16 of 50
©NIIT
Implementing Views and Batches

Altering, Dropping, and Renaming Views (Contd.)
 Dropping Views
     You can drop a view from a database by using the DROP
      VIEW statement
     Syntax
        DROP VIEW view_name




                                         SQL/Lesson 8/Slide 17 of 50
©NIIT
Implementing Views and Batches

Altering, Dropping, and Renaming Views (Contd.)
 Renaming Views
     A view can be renamed using the sp_rename system stored
      procedure
     Syntax
        sp_rename old_viewname, new_viewname




                                         SQL/Lesson 8/Slide 18 of 50
©NIIT
Implementing Views and Batches

8.D.2 Modifying Data Using Views
 A view is defined as follows:
   CREATE VIEW vwEmployeeCandidate
   AS
   SELECT Employee.cCandidateCode, vFirstName,
   vLastName, cPhone, siTestScore
   FROM Employee JOIN InternalCandidate
   ON
   Employee.cCandidateCode=InternalCandidate.
   cCandidateCode




                                   SQL/Lesson 8/Slide 19 of 50
©NIIT
Implementing Views and Batches

8.D.2 Modifying Data Using Views (Contd.)
  While updating the test score and the telephone number of an
  employee, whose candidate code is ‘000018’, the following
  command gives an error:
   UPDATE vwEmployeeCandidate
   SET cPhone = '(614)324-5634', siTestScore=75
   WHERE cCandidateCode=‘000018’
  Identify the error and correct it so that data in the tables is
  modified.




                                               SQL/Lesson 8/Slide 20 of 50
©NIIT
Implementing Views and Batches

Task List
 Identify the error and a method to modify data
 Draft separate statements to update tables
 Update tables
 Verify that the tables have been updated




                                             SQL/Lesson 8/Slide 21 of 50
©NIIT
Implementing Views and Batches

Identify the error and a method to modify data
 Modifying data through views
     Data is present in the base tables, which can be modified
      by modifying data in the view
 There are certain restrictions at the time of inserting,
  updating, or deleting data through views. These are:
     You cannot modify data in a view if the modification
      affects more than one underlying table
     You cannot change a column that is the result of a
      calculation


                                               SQL/Lesson 8/Slide 22 of 50
©NIIT
Implementing Views and Batches

Identify the error and a method to modify data (Contd.)
 Result:
     You are unable to update the attributes because views
      allow only one of the underlying tables to be updated at a
      time. You need to update the attributes by giving two
      separate UPDATE commands




                                             SQL/Lesson 8/Slide 23 of 50
©NIIT
Implementing Views and Batches

Draft separate statements to update tables
 Action:
     The following statement would update the cPhone
      attribute in the Employee base table:
        UPDATE vwEmployeeCandidate SET cPhone =
        '(614)324-5634'WHERE cCandidateCode =
        '000018'
     The following statement would update siTestScore
      attribute in the InternalCandidate table:
      UPDATE vwEmployeeCandidate
      SET siTestScore=75
      WHERE cCandidateCode='000018'

                                          SQL/Lesson 8/Slide 24 of 50
©NIIT
Implementing Views and Batches

Update tables
 Action:
     In the Query Analyzer window, type:
        UPDATE vwEmployeeCandidate
        SET cPhone = '(614)324-5634'
        WHERE cCandidateCode = '000018'

        UPDATE vwEmployeeCandidate
        SET siTestScore = 75
        WHERE cCandidateCode = '000018'
     Press F5 to execute the statement


                                            SQL/Lesson 8/Slide 25 of 50
©NIIT
Implementing Views and Batches

Verify that the tables have been updated
 Action:
     In the Query Analyzer window, type:
      SELECT * FROM Employee
      WHERE cCandidateCode = '000018'

        SELECT * FROM InternalCandidate
        WHERE cCandidateCode = '000018'
     Press F5 key to execute the queries




                                            SQL/Lesson 8/Slide 26 of 50
©NIIT
Implementing Views and Batches

8.P.1 Modifying Data Using Views
 A view was defined as follows:
   CREATE VIEW vwNewspaperNewsad
   AS
   SELECT cNewspaperName,vCity,cZip,dAdStartDate
   FROM Newspaper JOIN NewsAd
   ON Newspaper.cNewspaperCode =
   NewsAd.cNewspaperCode
  The following statement, when given to update the table, did
  not update the table:
   UPDATE cNewspaperNewsAd
   SET cZip='88993-4532',dAdStartDate='01/09/99'
   WHERE cNewspaperName='Daily News'
  Modify data in the base tables.
                                          SQL/Lesson 8/Slide 27 of 50
©NIIT
Implementing Views and Batches

Programming for SQL Server
 There are several methods of programming SQL Server
  applications. The following sections describe the different
  approaches that you can use for programming in terms of:
     Batches
     Variables
     Printing Messages
     Comments
     Control-of-flow statements



                                            SQL/Lesson 8/Slide 28 of 50
©NIIT
Implementing Views and Batches

Programming for SQL Server (Contd.)
 Batches
     Batches are groups of SQL statements submitted together
      to SQL Server for execution
     SQL server processes a batch interactively or from a file
 Variables
     You can use a variable to store a temporary value
     Syntax
         DECLARE @variable_name data_type
     Example
         DECLARE @Charge int
                                              SQL/Lesson 8/Slide 29 of 50
 ©NIIT
Implementing Views and Batches

Programming for SQL Server (Contd.)
 In Transact SQL, there are two kinds of variables, local and
  global
 The variable @Charge is declared within a batch and is lost
  when the execution of the batch is over. Such variables are
  called local variables and, since we defined them, they are
  called the user-defined variables
 Global variables are those that are declared by the server
  and typically assigned values by the server




                                            SQL/Lesson 8/Slide 30 of 50
©NIIT
Implementing Views and Batches

Programming for SQL Server (Contd.)
 Printing Messages
     The PRINT statement is used to display a user-defined
      message or the content of a variable on the screen
     Example
        DECLARE @MyName char (50)
        SELECT @MyName = 'Coomar Chris'
        PRINT @MyName




                                           SQL/Lesson 8/Slide 31 of 50
©NIIT
Implementing Views and Batches

Programming for SQL Server (Contd.)
 Comment Entry
     A comment entry is used in batches to write a description
      of the code
     It can be written in two ways:
        ®   Multiple line comment entries enclosed within /* and */
        ®   Single line comment entry starting with a -- (double
            hyphens)




                                               SQL/Lesson 8/Slide 32 of 50
©NIIT
Implementing Views and Batches

Programming for SQL Server (Contd.)
 Control-of-Flow Language
     The control-of-flow language controls the flow of execution
      of SQL statements in batches, stored procedures, triggers
      and transactions
     The control-of-flow statements provided by SQL Server for
      programming are:
        ® The IF…ELSE statement
        ® The CASE statement
        ®The WHILE statement


                                             SQL/Lesson 8/Slide 33 of 50
©NIIT
Implementing Views and Batches

Just a Minute...
 Fill in the blanks:
    a. A group of SQL statements submitted together to SQL
       server for execution is called a ________.
    b. _________ declared within a batch and is lost when the
       execution of the batch is over.




                                            SQL/Lesson 8/Slide 34 of 50
©NIIT
Implementing Views and Batches

8.D.3 Using the IF Statement
 The minimum test score for an internal candidate to be
  called for an interview is 80. Write a batch that would display
  “Called for interview” along with the test score, if the test
  score is more than 80, and display “Rejected - Not called for
  interview” if it is less than 80, for an employee whose
  employee code is 000008.




                                              SQL/Lesson 8/Slide 35 of 50
©NIIT
Implementing Views and Batches

Task List
 Identify how to display the required messages
 Draft the batch on paper
 Execute the batch
 Verify the batch




                                           SQL/Lesson 8/Slide 36 of 50
©NIIT
Implementing Views and Batches

Identify how to display the required messages
 The IF…ELSE Statement
     This statement can be used for conditional execution of
      SQL statements
     Syntax
        IF boolean_expression
            {sql_statement | statement_block}
         [ELSE boolean_expression
            {sql_statement | statement_block}]




                                            SQL/Lesson 8/Slide 37 of 50
©NIIT
Implementing Views and Batches

Identify how to display the required messages (Contd.)
 BEGIN…END Statement
     If there are multiple T-SQL statements, then these must
      be enclosed within the BEGIN and END keywords
     Syntax
          BEGIN
             {sql_statement | statement_ block}
          END




                                           SQL/Lesson 8/Slide 38 of 50
©NIIT
Implementing Views and Batches

Identify how to display the required messages (Contd.)
 Result:
     The messages can be displayed using the PRINT and
      IF…ELSE statements in a batch




                                        SQL/Lesson 8/Slide 39 of 50
©NIIT
Implementing Views and Batches

Draft the batch on paper
 Result:
     The batch statements are shown below.
       DECLARE @iTest int
       SELECT @iTest = siTestScore FROM
       InternalCandidate
       WHERE cEmployeeCode = '000008'
       IF @iTest  80
       PRINT 'Rejected - Not called for interview'
       ELSE
       BEGIN
           PRINT 'Called for interview'
           PRINT 'your test score ='
           PRINT @iTest
       END
                                  SQL/Lesson 8/Slide 40 of 50
©NIIT
Implementing Views and Batches

Execute the batch
 Action:
     In the Query Analyzer window, type the statement given
      below.
       DECLARE @iTest int
       SELECT @iTest = siTestScore
       FROM InternalCandidate
       WHERE cEmployeeCode = '000008'
       IF @iTest  80
           PRINT 'Rejected - Not called for
           interview'

                                           SQL/Lesson 8/Slide 41 of 50
©NIIT
Implementing Views and Batches

Execute the batch (Contd.)
        ELSE
        BEGIN
            PRINT 'Called for interview'
            PRINT 'your test score ='
            PRINT @iTest
        END
     Press F5 to execute the batch




                                      SQL/Lesson 8/Slide 42 of 50
©NIIT
Implementing Views and Batches

Verify the batch
 Action:
    Verify your answer by executing the following query:
        SELECT siTestScore
        FROM InternalCandidate
        WHERE cEmployeeCode = '000008'




                                           SQL/Lesson 8/Slide 43 of 50
©NIIT
Implementing Views and Batches

More Constructs
 The CASE Statement
     In situations where several conditions need to be
      evaluated, SQL Server provides a programming construct
      called the CASE statement
     Syntax
        CASE
          WHEN boolean_expression THEN expression
          [[WHEN boolean_expression THEN
          expression] [...]]
          [ELSE expression]
        END
                                          SQL/Lesson 8/Slide 44 of 50
©NIIT
Implementing Views and Batches

More Constructs (Contd.)
 The WHILE Statement
     You can use the WHILE construct in a batch, a stored
      procedure, a trigger, or a cursor to allow a set of T-SQL
      statements to execute repeatedly as long as the given
      condition holds true
     Syntax
        WHILE boolean_expression
        {sql_statement | statement_block}
        [BREAK]
        {sql_statement | statement_block}
        [CONTINUE]
                                              SQL/Lesson 8/Slide 45 of 50
©NIIT
Implementing Views and Batches

More Constructs (Contd.)
 The BREAK and CONTINUE Statements
     You can use the BREAK and CONTINUE statements to
      control the execution of the statements inside a WHILE
      loop
     The BREAK statement causes an exit from the WHILE
      loop
     The CONTINUE statement causes the WHILE loop to
      restart, skipping any statements after CONTINUE inside
      the loop



                                           SQL/Lesson 8/Slide 46 of 50
©NIIT
Implementing Views and Batches

Summary
In this lesson, you learned that:
 A view is a virtual table, which consists of a subset of columns
  from one or more tables
 A view derives its data from one or more tables known as base
  or underlying tables
 Views serve as security mechanisms, thereby protecting data
  in the base tables
 A view can restrict access to data in specific columns, specific
  rows, specific rows and columns, rows fetched by using joins,
  statistical summary of data in a given table, subsets of another
  view or a subset of views and tables
                                             SQL/Lesson 8/Slide 47 of 50
©NIIT
Implementing Views and Batches

Summary (Contd.)
 A view can be created with the CREATE VIEW statement
 SQL Server allows data to be modified only in one of the
  underlying tables when using views, even if the view is derived
  from multiple underlying tables
 A view can be modified with the ALTER VIEW statement
 A view can be dropped with the DROP VIEW statement
 A view can be renamed with the sp_rename stored procedure
 A batch is a set of SQL statements submitted together to the
  server for execution
 You can use a variable to store a temporary value
                                            SQL/Lesson 8/Slide 48 of 50
©NIIT
Implementing Views and Batches

Summary (Contd.)
 You can use the PRINT statement to display a user-defined
  message or the content of a variable on the screen
 You can use the comment entries in batches to write a
  description of the code
 You can use the IF…ELSE statement for conditional execution
  of SQL statements
 The CASE statement evaluates a list of conditions and returns
  one of the various possible results
 You can use the WHILE statement in a batch to allow a set of
  T-SQL statements to execute repeatedly as long as the given
  condition holds true
                                           SQL/Lesson 8/Slide 49 of 50
©NIIT
Implementing Views and Batches

Summary (Contd.)
 The BREAK statement causes an exit from the WHILE loop
 The CONTINUE statement causes the WHILE loop to restart,
  skipping any statements after CONTINUE inside the loop




                                        SQL/Lesson 8/Slide 50 of 50
©NIIT

More Related Content

Similar to Sql xp 08

Lesson10
Lesson10Lesson10
Lesson10renguzi
 
1 Database Security Lab 2 – Virtual Private Database.docx
1 Database Security Lab 2 – Virtual Private Database.docx1 Database Security Lab 2 – Virtual Private Database.docx
1 Database Security Lab 2 – Virtual Private Database.docxjeremylockett77
 
Creating Views - oracle database
Creating Views - oracle databaseCreating Views - oracle database
Creating Views - oracle databaseSalman Memon
 
e computer notes - Creating views
e computer notes - Creating viewse computer notes - Creating views
e computer notes - Creating viewsecomputernotes
 
Getting Started with MySQL II
Getting Started with MySQL IIGetting Started with MySQL II
Getting Started with MySQL IISankhya_Analytics
 
Labs And Walkthroughs
Labs And WalkthroughsLabs And Walkthroughs
Labs And WalkthroughsBryan Tuttle
 
09 qmds2005 session13
09 qmds2005 session1309 qmds2005 session13
09 qmds2005 session13Niit Care
 
Use Oracle 9i Summary Advisor To Better Manage Your Data Warehouse
Use Oracle 9i Summary Advisor To Better Manage Your Data WarehouseUse Oracle 9i Summary Advisor To Better Manage Your Data Warehouse
Use Oracle 9i Summary Advisor To Better Manage Your Data Warehouseinfo_sunrise24
 
CASE STUDY InternetExcel Exercises, page 434, textRecord your.docx
CASE STUDY InternetExcel Exercises, page 434, textRecord your.docxCASE STUDY InternetExcel Exercises, page 434, textRecord your.docx
CASE STUDY InternetExcel Exercises, page 434, textRecord your.docxketurahhazelhurst
 
Dynamics ax 2012 workflow development
Dynamics ax 2012 workflow development Dynamics ax 2012 workflow development
Dynamics ax 2012 workflow development Ahmed Farag
 
Understanding performance bottlenecks using performance dashboard
Understanding performance bottlenecks using performance dashboardUnderstanding performance bottlenecks using performance dashboard
Understanding performance bottlenecks using performance dashboardAmit Banerjee
 
[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (1/3)
[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (1/3)[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (1/3)
[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (1/3)Carles Farré
 

Similar to Sql xp 08 (20)

Les11
Les11Les11
Les11
 
Lesson10
Lesson10Lesson10
Lesson10
 
1 Database Security Lab 2 – Virtual Private Database.docx
1 Database Security Lab 2 – Virtual Private Database.docx1 Database Security Lab 2 – Virtual Private Database.docx
1 Database Security Lab 2 – Virtual Private Database.docx
 
Sql xp 10
Sql xp 10Sql xp 10
Sql xp 10
 
Creating Views - oracle database
Creating Views - oracle databaseCreating Views - oracle database
Creating Views - oracle database
 
e computer notes - Creating views
e computer notes - Creating viewse computer notes - Creating views
e computer notes - Creating views
 
Sql ch 13 - sql-views
Sql ch 13 - sql-viewsSql ch 13 - sql-views
Sql ch 13 - sql-views
 
Les11.ppt
Les11.pptLes11.ppt
Les11.ppt
 
Sql views
Sql viewsSql views
Sql views
 
chap 9 dbms.ppt
chap 9 dbms.pptchap 9 dbms.ppt
chap 9 dbms.ppt
 
Getting Started with MySQL II
Getting Started with MySQL IIGetting Started with MySQL II
Getting Started with MySQL II
 
Sql xp 09
Sql xp 09Sql xp 09
Sql xp 09
 
Sql xp 11
Sql xp 11Sql xp 11
Sql xp 11
 
Labs And Walkthroughs
Labs And WalkthroughsLabs And Walkthroughs
Labs And Walkthroughs
 
09 qmds2005 session13
09 qmds2005 session1309 qmds2005 session13
09 qmds2005 session13
 
Use Oracle 9i Summary Advisor To Better Manage Your Data Warehouse
Use Oracle 9i Summary Advisor To Better Manage Your Data WarehouseUse Oracle 9i Summary Advisor To Better Manage Your Data Warehouse
Use Oracle 9i Summary Advisor To Better Manage Your Data Warehouse
 
CASE STUDY InternetExcel Exercises, page 434, textRecord your.docx
CASE STUDY InternetExcel Exercises, page 434, textRecord your.docxCASE STUDY InternetExcel Exercises, page 434, textRecord your.docx
CASE STUDY InternetExcel Exercises, page 434, textRecord your.docx
 
Dynamics ax 2012 workflow development
Dynamics ax 2012 workflow development Dynamics ax 2012 workflow development
Dynamics ax 2012 workflow development
 
Understanding performance bottlenecks using performance dashboard
Understanding performance bottlenecks using performance dashboardUnderstanding performance bottlenecks using performance dashboard
Understanding performance bottlenecks using performance dashboard
 
[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (1/3)
[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (1/3)[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (1/3)
[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (1/3)
 

More from Niit Care (20)

Ajs 1 b
Ajs 1 bAjs 1 b
Ajs 1 b
 
Ajs 4 b
Ajs 4 bAjs 4 b
Ajs 4 b
 
Ajs 4 a
Ajs 4 aAjs 4 a
Ajs 4 a
 
Ajs 4 c
Ajs 4 cAjs 4 c
Ajs 4 c
 
Ajs 3 b
Ajs 3 bAjs 3 b
Ajs 3 b
 
Ajs 3 a
Ajs 3 aAjs 3 a
Ajs 3 a
 
Ajs 3 c
Ajs 3 cAjs 3 c
Ajs 3 c
 
Ajs 2 b
Ajs 2 bAjs 2 b
Ajs 2 b
 
Ajs 2 a
Ajs 2 aAjs 2 a
Ajs 2 a
 
Ajs 2 c
Ajs 2 cAjs 2 c
Ajs 2 c
 
Ajs 1 a
Ajs 1 aAjs 1 a
Ajs 1 a
 
Ajs 1 c
Ajs 1 cAjs 1 c
Ajs 1 c
 
Dacj 4 2-c
Dacj 4 2-cDacj 4 2-c
Dacj 4 2-c
 
Dacj 4 2-b
Dacj 4 2-bDacj 4 2-b
Dacj 4 2-b
 
Dacj 4 2-a
Dacj 4 2-aDacj 4 2-a
Dacj 4 2-a
 
Dacj 4 1-c
Dacj 4 1-cDacj 4 1-c
Dacj 4 1-c
 
Dacj 4 1-b
Dacj 4 1-bDacj 4 1-b
Dacj 4 1-b
 
Dacj 4 1-a
Dacj 4 1-aDacj 4 1-a
Dacj 4 1-a
 
Dacj 1-2 b
Dacj 1-2 bDacj 1-2 b
Dacj 1-2 b
 
Dacj 1-3 c
Dacj 1-3 cDacj 1-3 c
Dacj 1-3 c
 

Recently uploaded

Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 

Recently uploaded (20)

Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 

Sql xp 08

  • 1. Implementing Views and Batches Objectives In this lesson, you will learn to: Create, alter, drop, and rename views Update data using views Declare variables Print messages Use comments Use conditional statements Use the iteration statements SQL/Lesson 8/Slide 1 of 50 ©NIIT
  • 2. Implementing Views and Batches Getting Started Views A view is a virtual table, which gives access to a subset of columns from one or more tables Views ensure security of data by restricting access to: ® Specific rows of a table ® Specific columns of a table ® Specific rows and columns of a table ® The rows fetched using joins SQL/Lesson 8/Slide 2 of 50 ©NIIT
  • 3. Implementing Views and Batches Getting Started (Contd.) ® The statistical summary of data in a given table ® Subsets of another view or a subset of views and tables Views provide several advantages: Providing relevant data for users Hiding data complexity Organize data from heterogeneous sources Reducing the object size SQL/Lesson 8/Slide 3 of 50 ©NIIT
  • 4. Implementing Views and Batches 8.D.1 Creating Views You need to view skills of various employees. Some of the queries to be executed are as follows: SELECT vFirstName, vSkill FROM Employee JOIN PositionSkill ON Employee.cCurrentPosition = PositionSkill.cPositionCode JOIN Skill ON PositionSkill.cSkillCode = Skill.cSkillCode SELECT vFirstName, vLastName,vSkill FROM Employee JOIN PositionSkill ON Employee.cCurrentPosition=PositionSkill.cPos itionCode SQL/Lesson 8/Slide 4 of 50 ©NIIT
  • 5. Implementing Views and Batches 8.D.1 Creating Views (Contd.) JOIN Skill ON PositionSkill.cSkillCode = Skill.cSkillCode SELECT vFirstName, vLastName, vSkill FROM Employee JOIN PositionSkill ON Employee.cCurrentPosition =PositionSkill.cPositionCode JOIN Skill ON PositionSkill.cSkillCode =Skill.cSkillCode AND vFirstName = 'Angela' SQL/Lesson 8/Slide 5 of 50 ©NIIT
  • 6. Implementing Views and Batches 8.D.1 Creating Views (Contd.) SELECT vFirstName, vLastName, vQualification, vSkill FROM Employee JOIN PositionSkill ON Employee.cCurrentPosition = PositionSkill.cPositionCode JOIN Skill ON PositionSkill.cSkillCode = Skill.cSkillCode Simplify the task of executing these queries. SQL/Lesson 8/Slide 6 of 50 ©NIIT
  • 7. Implementing Views and Batches Task List Identify how to simplify queries Draft the query for the view Create the view Verify that the queries have been simplified by executing the required queries on the view SQL/Lesson 8/Slide 7 of 50 ©NIIT
  • 8. Implementing Views and Batches Identify how to simplify queries Result: Simplification of the query can be done using views SQL/Lesson 8/Slide 8 of 50 ©NIIT
  • 9. Implementing Views and Batches Draft the query for the view Action: The tables from where the view derives its data are Employee, positionSkill, and Skill The columns that are to be included in the view are vFirstName, vLastName, and vQualification from the Employee table and vSkill from the Skill table SQL/Lesson 8/Slide 9 of 50 ©NIIT
  • 10. Implementing Views and Batches Create the view A view can be created using the CREATE VIEW statement Syntax CREATE VIEW view_name [(column_name [, column_name]...)] [WITH ENCRYPTION] AS select_statement [WITH CHECK OPTION] SQL/Lesson 8/Slide 10 of 50 ©NIIT
  • 11. Implementing Views and Batches Create the view (Contd.) Action: In the Query Analyzer window, type: CREATE VIEW vwEmpSkill AS SELECT vFirstName, vLastName, vQualification, vSkill FROM Employee JOIN PositionSkill ON Employee.cCurrentPosition = PositionSkill.cPositionCode JOIN Skill ON PositionSkill.cSkillCode = Skill.cSkillCode Press F5 to execute the command SQL/Lesson 8/Slide 11 of 50 ©NIIT
  • 12. Implementing Views and Batches Verify that the queries have been simplified by executing the required queries on the view Action: In the Query Analyzer window, type: SELECT vFirstName, vSkill FROM vwEmpSkill Press F5 to execute the query In the Query Analyzer window, type: SELECT vFirstName, vLastName, vSkill FROM vwEmpSkill Press F5 to execute the query SQL/Lesson 8/Slide 12 of 50 ©NIIT
  • 13. Implementing Views and Batches Verify that the queries have been simplified by executing the required queries on the view (Contd.) In the Query Analyzer window, type: SELECT vFirstName,vLastName,vSkill FROM vwEmpSkill WHERE vFirstName = 'Angela' Press F5 to execute the query In the Query Analyzer window, type: SELECT * FROM vwEmpSkill Press F5 to execute the query SQL/Lesson 8/Slide 13 of 50 ©NIIT
  • 14. Implementing Views and Batches Just a Minute... Some queries to be executed are as follows: SELECT vFirstName, vDepartmentName FROM Employee JOIN Department ON Employee.cDepartmentCode = Department.cDepartmentCode SELECT vFirstName, cDesignation, vDepartmentName FROM Employee JOIN Department ON Employee.cDepartmentCode = Department.cDepartmentCode SQL/Lesson 8/Slide 14 of 50 ©NIIT
  • 15. Implementing Views and Batches Just a Minute (Contd.) SELECT vFirstName, vAddress, cCity, cZip, cDesignation, vDepartmentName, vDepartmentHead FROM Employee JOIN Department ON Employee.cDepartmentCode = Department.cDepartmentCode WHERE cCity = 'Columbus' Simplify the task of executing these queries. SQL/Lesson 8/Slide 15 of 50 ©NIIT
  • 16. Implementing Views and Batches Altering, Dropping, and Renaming Views Altering Views You can modify a view by using the ALTER VIEW statement Syntax ALTER VIEW view_name [(column_name)] [WITH ENCRYPTION] AS select_statement [WITH CHECK OPTION] SQL/Lesson 8/Slide 16 of 50 ©NIIT
  • 17. Implementing Views and Batches Altering, Dropping, and Renaming Views (Contd.) Dropping Views You can drop a view from a database by using the DROP VIEW statement Syntax DROP VIEW view_name SQL/Lesson 8/Slide 17 of 50 ©NIIT
  • 18. Implementing Views and Batches Altering, Dropping, and Renaming Views (Contd.) Renaming Views A view can be renamed using the sp_rename system stored procedure Syntax sp_rename old_viewname, new_viewname SQL/Lesson 8/Slide 18 of 50 ©NIIT
  • 19. Implementing Views and Batches 8.D.2 Modifying Data Using Views A view is defined as follows: CREATE VIEW vwEmployeeCandidate AS SELECT Employee.cCandidateCode, vFirstName, vLastName, cPhone, siTestScore FROM Employee JOIN InternalCandidate ON Employee.cCandidateCode=InternalCandidate. cCandidateCode SQL/Lesson 8/Slide 19 of 50 ©NIIT
  • 20. Implementing Views and Batches 8.D.2 Modifying Data Using Views (Contd.) While updating the test score and the telephone number of an employee, whose candidate code is ‘000018’, the following command gives an error: UPDATE vwEmployeeCandidate SET cPhone = '(614)324-5634', siTestScore=75 WHERE cCandidateCode=‘000018’ Identify the error and correct it so that data in the tables is modified. SQL/Lesson 8/Slide 20 of 50 ©NIIT
  • 21. Implementing Views and Batches Task List Identify the error and a method to modify data Draft separate statements to update tables Update tables Verify that the tables have been updated SQL/Lesson 8/Slide 21 of 50 ©NIIT
  • 22. Implementing Views and Batches Identify the error and a method to modify data Modifying data through views Data is present in the base tables, which can be modified by modifying data in the view There are certain restrictions at the time of inserting, updating, or deleting data through views. These are: You cannot modify data in a view if the modification affects more than one underlying table You cannot change a column that is the result of a calculation SQL/Lesson 8/Slide 22 of 50 ©NIIT
  • 23. Implementing Views and Batches Identify the error and a method to modify data (Contd.) Result: You are unable to update the attributes because views allow only one of the underlying tables to be updated at a time. You need to update the attributes by giving two separate UPDATE commands SQL/Lesson 8/Slide 23 of 50 ©NIIT
  • 24. Implementing Views and Batches Draft separate statements to update tables Action: The following statement would update the cPhone attribute in the Employee base table: UPDATE vwEmployeeCandidate SET cPhone = '(614)324-5634'WHERE cCandidateCode = '000018' The following statement would update siTestScore attribute in the InternalCandidate table: UPDATE vwEmployeeCandidate SET siTestScore=75 WHERE cCandidateCode='000018' SQL/Lesson 8/Slide 24 of 50 ©NIIT
  • 25. Implementing Views and Batches Update tables Action: In the Query Analyzer window, type: UPDATE vwEmployeeCandidate SET cPhone = '(614)324-5634' WHERE cCandidateCode = '000018' UPDATE vwEmployeeCandidate SET siTestScore = 75 WHERE cCandidateCode = '000018' Press F5 to execute the statement SQL/Lesson 8/Slide 25 of 50 ©NIIT
  • 26. Implementing Views and Batches Verify that the tables have been updated Action: In the Query Analyzer window, type: SELECT * FROM Employee WHERE cCandidateCode = '000018' SELECT * FROM InternalCandidate WHERE cCandidateCode = '000018' Press F5 key to execute the queries SQL/Lesson 8/Slide 26 of 50 ©NIIT
  • 27. Implementing Views and Batches 8.P.1 Modifying Data Using Views A view was defined as follows: CREATE VIEW vwNewspaperNewsad AS SELECT cNewspaperName,vCity,cZip,dAdStartDate FROM Newspaper JOIN NewsAd ON Newspaper.cNewspaperCode = NewsAd.cNewspaperCode The following statement, when given to update the table, did not update the table: UPDATE cNewspaperNewsAd SET cZip='88993-4532',dAdStartDate='01/09/99' WHERE cNewspaperName='Daily News' Modify data in the base tables. SQL/Lesson 8/Slide 27 of 50 ©NIIT
  • 28. Implementing Views and Batches Programming for SQL Server There are several methods of programming SQL Server applications. The following sections describe the different approaches that you can use for programming in terms of: Batches Variables Printing Messages Comments Control-of-flow statements SQL/Lesson 8/Slide 28 of 50 ©NIIT
  • 29. Implementing Views and Batches Programming for SQL Server (Contd.) Batches Batches are groups of SQL statements submitted together to SQL Server for execution SQL server processes a batch interactively or from a file Variables You can use a variable to store a temporary value Syntax DECLARE @variable_name data_type Example DECLARE @Charge int SQL/Lesson 8/Slide 29 of 50 ©NIIT
  • 30. Implementing Views and Batches Programming for SQL Server (Contd.) In Transact SQL, there are two kinds of variables, local and global The variable @Charge is declared within a batch and is lost when the execution of the batch is over. Such variables are called local variables and, since we defined them, they are called the user-defined variables Global variables are those that are declared by the server and typically assigned values by the server SQL/Lesson 8/Slide 30 of 50 ©NIIT
  • 31. Implementing Views and Batches Programming for SQL Server (Contd.) Printing Messages The PRINT statement is used to display a user-defined message or the content of a variable on the screen Example DECLARE @MyName char (50) SELECT @MyName = 'Coomar Chris' PRINT @MyName SQL/Lesson 8/Slide 31 of 50 ©NIIT
  • 32. Implementing Views and Batches Programming for SQL Server (Contd.) Comment Entry A comment entry is used in batches to write a description of the code It can be written in two ways: ® Multiple line comment entries enclosed within /* and */ ® Single line comment entry starting with a -- (double hyphens) SQL/Lesson 8/Slide 32 of 50 ©NIIT
  • 33. Implementing Views and Batches Programming for SQL Server (Contd.) Control-of-Flow Language The control-of-flow language controls the flow of execution of SQL statements in batches, stored procedures, triggers and transactions The control-of-flow statements provided by SQL Server for programming are: ® The IF…ELSE statement ® The CASE statement ®The WHILE statement SQL/Lesson 8/Slide 33 of 50 ©NIIT
  • 34. Implementing Views and Batches Just a Minute... Fill in the blanks: a. A group of SQL statements submitted together to SQL server for execution is called a ________. b. _________ declared within a batch and is lost when the execution of the batch is over. SQL/Lesson 8/Slide 34 of 50 ©NIIT
  • 35. Implementing Views and Batches 8.D.3 Using the IF Statement The minimum test score for an internal candidate to be called for an interview is 80. Write a batch that would display “Called for interview” along with the test score, if the test score is more than 80, and display “Rejected - Not called for interview” if it is less than 80, for an employee whose employee code is 000008. SQL/Lesson 8/Slide 35 of 50 ©NIIT
  • 36. Implementing Views and Batches Task List Identify how to display the required messages Draft the batch on paper Execute the batch Verify the batch SQL/Lesson 8/Slide 36 of 50 ©NIIT
  • 37. Implementing Views and Batches Identify how to display the required messages The IF…ELSE Statement This statement can be used for conditional execution of SQL statements Syntax IF boolean_expression {sql_statement | statement_block} [ELSE boolean_expression {sql_statement | statement_block}] SQL/Lesson 8/Slide 37 of 50 ©NIIT
  • 38. Implementing Views and Batches Identify how to display the required messages (Contd.) BEGIN…END Statement If there are multiple T-SQL statements, then these must be enclosed within the BEGIN and END keywords Syntax BEGIN {sql_statement | statement_ block} END SQL/Lesson 8/Slide 38 of 50 ©NIIT
  • 39. Implementing Views and Batches Identify how to display the required messages (Contd.) Result: The messages can be displayed using the PRINT and IF…ELSE statements in a batch SQL/Lesson 8/Slide 39 of 50 ©NIIT
  • 40. Implementing Views and Batches Draft the batch on paper Result: The batch statements are shown below. DECLARE @iTest int SELECT @iTest = siTestScore FROM InternalCandidate WHERE cEmployeeCode = '000008' IF @iTest 80 PRINT 'Rejected - Not called for interview' ELSE BEGIN PRINT 'Called for interview' PRINT 'your test score =' PRINT @iTest END SQL/Lesson 8/Slide 40 of 50 ©NIIT
  • 41. Implementing Views and Batches Execute the batch Action: In the Query Analyzer window, type the statement given below. DECLARE @iTest int SELECT @iTest = siTestScore FROM InternalCandidate WHERE cEmployeeCode = '000008' IF @iTest 80 PRINT 'Rejected - Not called for interview' SQL/Lesson 8/Slide 41 of 50 ©NIIT
  • 42. Implementing Views and Batches Execute the batch (Contd.) ELSE BEGIN PRINT 'Called for interview' PRINT 'your test score =' PRINT @iTest END Press F5 to execute the batch SQL/Lesson 8/Slide 42 of 50 ©NIIT
  • 43. Implementing Views and Batches Verify the batch Action: Verify your answer by executing the following query: SELECT siTestScore FROM InternalCandidate WHERE cEmployeeCode = '000008' SQL/Lesson 8/Slide 43 of 50 ©NIIT
  • 44. Implementing Views and Batches More Constructs The CASE Statement In situations where several conditions need to be evaluated, SQL Server provides a programming construct called the CASE statement Syntax CASE WHEN boolean_expression THEN expression [[WHEN boolean_expression THEN expression] [...]] [ELSE expression] END SQL/Lesson 8/Slide 44 of 50 ©NIIT
  • 45. Implementing Views and Batches More Constructs (Contd.) The WHILE Statement You can use the WHILE construct in a batch, a stored procedure, a trigger, or a cursor to allow a set of T-SQL statements to execute repeatedly as long as the given condition holds true Syntax WHILE boolean_expression {sql_statement | statement_block} [BREAK] {sql_statement | statement_block} [CONTINUE] SQL/Lesson 8/Slide 45 of 50 ©NIIT
  • 46. Implementing Views and Batches More Constructs (Contd.) The BREAK and CONTINUE Statements You can use the BREAK and CONTINUE statements to control the execution of the statements inside a WHILE loop The BREAK statement causes an exit from the WHILE loop The CONTINUE statement causes the WHILE loop to restart, skipping any statements after CONTINUE inside the loop SQL/Lesson 8/Slide 46 of 50 ©NIIT
  • 47. Implementing Views and Batches Summary In this lesson, you learned that: A view is a virtual table, which consists of a subset of columns from one or more tables A view derives its data from one or more tables known as base or underlying tables Views serve as security mechanisms, thereby protecting data in the base tables A view can restrict access to data in specific columns, specific rows, specific rows and columns, rows fetched by using joins, statistical summary of data in a given table, subsets of another view or a subset of views and tables SQL/Lesson 8/Slide 47 of 50 ©NIIT
  • 48. Implementing Views and Batches Summary (Contd.) A view can be created with the CREATE VIEW statement SQL Server allows data to be modified only in one of the underlying tables when using views, even if the view is derived from multiple underlying tables A view can be modified with the ALTER VIEW statement A view can be dropped with the DROP VIEW statement A view can be renamed with the sp_rename stored procedure A batch is a set of SQL statements submitted together to the server for execution You can use a variable to store a temporary value SQL/Lesson 8/Slide 48 of 50 ©NIIT
  • 49. Implementing Views and Batches Summary (Contd.) You can use the PRINT statement to display a user-defined message or the content of a variable on the screen You can use the comment entries in batches to write a description of the code You can use the IF…ELSE statement for conditional execution of SQL statements The CASE statement evaluates a list of conditions and returns one of the various possible results You can use the WHILE statement in a batch to allow a set of T-SQL statements to execute repeatedly as long as the given condition holds true SQL/Lesson 8/Slide 49 of 50 ©NIIT
  • 50. Implementing Views and Batches Summary (Contd.) The BREAK statement causes an exit from the WHILE loop The CONTINUE statement causes the WHILE loop to restart, skipping any statements after CONTINUE inside the loop SQL/Lesson 8/Slide 50 of 50 ©NIIT