SlideShare a Scribd company logo
1 of 31
Querying and Managing Data Using SQL Server 2005
Objectives


                In this session, you will learn to:
                   Create and manage views
                   Implement a full-text search
                   Implement batches




     Ver. 1.0                        Session 11       Slide 1 of 31
Querying and Managing Data Using SQL Server 2005
Creating Views


                A View is:
                   Used to view data from tables
                   Similar to creating tables but it does not contain any data as it
                   derives its data from the underlying tables
                   Created by using CREATE VIEW statement
                Syntax:
                 CREATE VIEW view_name
                 [(column_name [, column_name]...)]
                 [WITH ENCRYPTION] [, SCHEMABINDING]]
                 AS select_statement [WITH CHECK OPTION]
                Let’s see how…




     Ver. 1.0                        Session 11                              Slide 2 of 31
Querying and Managing Data Using SQL Server 2005
Indexing Views


                Indexing of Views:
                   Is done when the volume of data in the underlying table is
                   large and not updated frequently
                   Improves query performance
                   Is performed by creating an unique clustered index on a view
                   and afterwards nonclustered index can also be created
                   Is performed by using CREATE INDEX statement
                Let’s see how…




     Ver. 1.0                        Session 11                          Slide 3 of 31
Querying and Managing Data Using SQL Server 2005
Just a minute


                In which of the following conditions will you NOT create an
                indexed view:
                 1. When the data is large
                 2. When the data is regularly updated
                 3. When you need to improve the performance of the view




                Answer:
                 2. When the data is regularly updated




     Ver. 1.0                       Session 11                         Slide 4 of 31
Querying and Managing Data Using SQL Server 2005
Managing Views


               Managing a view involves altering, dropping, or renaming.
               Altering a view involves modifying a view without dropping
               it.
                  Syntax:
                   ALTER VIEW view_name [(column_name)]
                   WITH ENCRYPTION]
                   AS select_statement
                   WITH CHECK OPTION]
               Dropping a view involves deleting the view when it is no
               longer required.
                  Syntax:
                   DROP VIEW view_name




    Ver. 1.0                      Session 11                        Slide 5 of 31
Querying and Managing Data Using SQL Server 2005
Managing Views (Contd.)


                Renaming the name of the view without deleting it.
                   Syntax:
                    sp_rename old_viewname, new_viewname
                Let’s see how…




     Ver. 1.0                      Session 11                        Slide 6 of 31
Querying and Managing Data Using SQL Server 2005
Demo: Creating Views


                Problem Statement:
                   You are a database developer at AdventureWorks, Inc. You
                   need to frequently generate a report containing the following
                   details of the employees:
                       Employee ID
                       Employee First Name
                       Employee Last Name
                       Title
                       Manager First Name
                       Manager Last Name




     Ver. 1.0                        Session 11                            Slide 7 of 31
Querying and Managing Data Using SQL Server 2005
Demo: Creating Views (Contd.)


                Problem Statement (Contd.):
                   To retrieve this data, you need to execute the following
                   statement on the database:
                     SELECT e1.EmployeeID, c1.FirstName,
                     c1.LastName, e1.Title,
                     c2.FirstName AS [Manager First
                     Name],c2.LastName AS [Manager Last Name]
                     FROM HumanResources.Employee e1 INNER
                     JOIN Person.Contact c1
                     ON e1.ContactID = c1.ContactID INNER JOIN
                     HumanResources.Employee AS e2
                     ON e1.ManagerID = e2.EmployeeID INNER
                     JOIN Person.Contact AS c2
                     ON e2.ContactID = c2.ContactID

     Ver. 1.0                       Session 11                           Slide 8 of 31
Querying and Managing Data Using SQL Server 2005
Demo: Creating Views (Contd.)


                Problem Statement (Contd.):
                   As a database developer, you need to simplify the execution of
                   this query so that you do not need to send such a large query
                   to the database engine every time the report is required.




     Ver. 1.0                       Session 11                           Slide 9 of 31
Querying and Managing Data Using SQL Server 2005
Demo: Creating Views (Contd.)


                Solution:
                   To solve the preceding problem, you need to perform the
                   following tasks:
                     1. Create a view.
                     2. Verify the simplification of the query execution.




     Ver. 1.0                          Session 11                           Slide 10 of 31
Querying and Managing Data Using SQL Server 2005
Configuring Full-Text Search


                Full-Text Search:
                      Allows you to perform complex search on the data
                      Allows you to search for synonyms or antonyms of a particular
                      word
                      Feature is disabled by default
                To retrieve the required details by using full-text, you need
                to perform the following tasks:
                 1.   Enable the full-text search in the database.
                 2.   Create a full-text catalog.
                 3.   Create a unique index.
                 4.   Create a full-text index.
                 5.   Populate the full-text index.
                Let’s see how…


     Ver. 1.0                           Session 11                         Slide 11 of 31
Querying and Managing Data Using SQL Server 2005
Searching Data by Using a Full-Text Search


                To search using full-text index, use the following full-text
                predicates:
                 – FREETEXT: Searches for any variation of a word or a group of
                   words given in the search column.
                 – CONTAINS: Searches for a specific phrase, for the proximity
                   of words within a text or for the exact match.
                Let’s see how…




     Ver. 1.0                       Session 11                          Slide 12 of 31
Querying and Managing Data Using SQL Server 2005
Just a minute


                List the types of full-text index population methods.




                Answer:
                    The three types of full-text index population methods
                    are:
                     1. Full population
                     2. Change tracking based population
                     3. Incremental timestamp based population



     Ver. 1.0                        Session 11                             Slide 13 of 31
Querying and Managing Data Using SQL Server 2005
Just a minute


                Which predicate is used to search for a specific phrase or
                for an exact match?




                Answer:
                   CONTAINS




     Ver. 1.0                      Session 11                        Slide 14 of 31
Querying and Managing Data Using SQL Server 2005
Demo: Implementing Full-Text Search


                Problem Statement:
                   The users at AdventureWorks, Inc. need to frequently search
                   for employees, customers, or vendors based on the location.
                   The location details of all these entities are stored in the
                   Address table in the Person schema. The users want to search
                   for addresses with different combinations of the words
                   specified in the search criteria. For example, they need to
                   search for the locations that contain the words ‘Santa’ and
                   ‘drive’ in the AddressLine1 column. Similarly, they might need
                   to search for the locations that contain the words ‘Santa’ and
                   ‘Street’ in the AddressLine1 column.
                   How will you enable the users to perform such a data search?




     Ver. 1.0                       Session 11                           Slide 15 of 31
Querying and Managing Data Using SQL Server 2005
Demo: Implementing Full-Text Search (Contd.)


                Solution:
                   To solve the preceding problem, you need to perform the
                   following tasks:
                    1. Enable the Full-text search on the AdventureWorks database.
                    2. Create a default full-text catalog.
                    3. Create a full-text index.
                    4. Search the data by using the CONTAINS predicate.




     Ver. 1.0                        Session 11                               Slide 16 of 31
Querying and Managing Data Using SQL Server 2005
Creating Batches


                Batch:
                   Is created to send a group of SQL statements together to SQL
                   Server for execution
                   Compiles the statements as a single executable unit called an
                   execution plan
                   Uses GO command at the end to send the SQL statements to
                   the instance of SQL Server
                   Uses variables to store values. They are:
                    • Local variables
                    • Global variables
                   Uses PRINT statement to display user-defined messages and
                   values of variables
                Let’s see how…



     Ver. 1.0                        Session 11                         Slide 17 of 31
Querying and Managing Data Using SQL Server 2005
Just a minute


                Which of the following statements can be used within a
                batch?
                1. CREATE FUNCTION
                2. CREATE RULE
                3. DECLARE




                Answer:
                 3. DECLARE




     Ver. 1.0                      Session 11                      Slide 18 of 31
Querying and Managing Data Using SQL Server 2005
Using Constructs


                Allow batches to perform conditional execution of
                statements with the following control-of-flow statements:
                   IF…ELSE statement
                   CASE statement
                   WHILE statement




     Ver. 1.0                      Session 11                        Slide 19 of 31
Querying and Managing Data Using SQL Server 2005
Using Constructs (Contd.)


                IF…ELSE statement:
                   Performs a particular action based on the result of the boolean
                   expression
                   Syntax:
                    IF boolean_expression {sql_statement |
                   statement_block} [ELSE boolean_expression
                    {sql_statement | statement_block}]
                Let’s see how…




     Ver. 1.0                       Session 11                            Slide 20 of 31
Querying and Managing Data Using SQL Server 2005
Using Constructs (Contd.)


                CASE statement:
                   Evaluates a list of conditions and returns one of the various
                   possible results
                   Syntax:
                    CASE
                    WHEN boolean_expression THEN expression
                    [[WHEN boolean_expression THEN expression]
                    [...]]
                    [ELSE expression]
                    END
                Let’s see how…




     Ver. 1.0                        Session 11                            Slide 21 of 31
Querying and Managing Data Using SQL Server 2005
Using Constructs (Contd.)


                WHILE statement:
                   Executes a batch repeatedly as long as the given condition
                   holds true
                   Uses BREAK and CONTINUE statements to break the loop or
                   to continue the loop
                   Syntax:
                     WHILE boolean_expression
                     {sql_statement | statement_block}
                     [BREAK]
                     {sql_statement | statement_block}
                     [CONTINUE]
                Let’s see how…




     Ver. 1.0                      Session 11                         Slide 22 of 31
Querying and Managing Data Using SQL Server 2005
Handling Errors and Exceptions


                Errors in SQL Server can be handled on two levels:
                   Using the TRY-CATCH construct
                   Using the RAISERROR statement




     Ver. 1.0                     Session 11                         Slide 23 of 31
Querying and Managing Data Using SQL Server 2005
Handling Errors and Exceptions (Contd.)


                TRY-CATCH construct is used in the following way:
                   Try block encloses a group of T-SQL statements. If any error
                   occurs in the statements of TRY block, control passes to the
                   CATCH block.
                   CATCH block encloses another group of statements, which
                   gets executed when an error occurs.
                Let’s see how…




     Ver. 1.0                       Session 11                           Slide 24 of 31
Querying and Managing Data Using SQL Server 2005
Handling Errors and Exceptions (Contd.)


                RAISERROR:
                   Is used to return messages back to the called applications
                   Uses the same message format as a system error or warning
                   message generated by the SQL Server Database Engine
                   Can also return user-defined error messages
                Let’s see how…




     Ver. 1.0                      Session 11                          Slide 25 of 31
Querying and Managing Data Using SQL Server 2005
Just a minute


                Which system function returns the text of the error message
                when used in the CATCH block?




                Answer:
                   ERROR_MESSAGE()




     Ver. 1.0                      Session 11                       Slide 26 of 31
Querying and Managing Data Using SQL Server 2005
Just a minute


                How can you return a user-defined error message in a
                batch?




                Answer:
                   Using the RAISERROR statement




     Ver. 1.0                     Session 11                      Slide 27 of 31
Querying and Managing Data Using SQL Server 2005
Summary


               In this session, you learned that:
                  A view is a virtual table, which derives its data from one or
                  more tables known as the base or underlying tables.
                  Views serve as security mechanisms, thereby protecting data
                  in the base tables.
                  The 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.
                  The SQL Server enables users to search for a wide range of
                  text in the SQL tables through a full-text query feature.
                  You can enable the full-text search by using the command,
                  SP_FULLTEXT_DATABASE ENABLE.
                  A full-text catalog can be created by using the CREATE
                  FULLTEXT CATALOG statement.



    Ver. 1.0                       Session 11                            Slide 28 of 31
Querying and Managing Data Using SQL Server 2005
Summary (Contd.)



               – A full-text index can be created by using the CREATE
                 FULLTEXT INDEX statement.
               – Three types of full-text population methods are full population,
                 change tracking-based population, and incremental
                 timestamp-based population.
               – Full-text predicates that can be used to perform full-text search
                 are CONTAINS and FREETEXT.
               – A FREETEXT predicate searches for any variation of a word or
                 a group of words given in the search column.
               – The CONTAINS predicate searches for a specific phrase or for
                 the exact match.
               – 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.

    Ver. 1.0                       Session 11                            Slide 29 of 31
Querying and Managing Data Using SQL Server 2005
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 the 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.
               The BREAK statement causes an exit from the WHILE loop.




    Ver. 1.0                    Session 11                          Slide 30 of 31
Querying and Managing Data Using SQL Server 2005
Summary (Contd.)


               The CONTINUE statement causes the WHILE loop to restart,
               skipping any statements after the CONTINUE statement within
               the loop.
               Two ways to handle errors in batches are:
                • TRY-CATCH construct
                • RAISERROR statement




    Ver. 1.0                   Session 11                         Slide 31 of 31

More Related Content

What's hot

Installing ingres enterprise access in a system which already has an ingres i...
Installing ingres enterprise access in a system which already has an ingres i...Installing ingres enterprise access in a system which already has an ingres i...
Installing ingres enterprise access in a system which already has an ingres i...malu42
 
DB Optimizer Datasheet - Automated SQL Profiling & Tuning for Optimized Perfo...
DB Optimizer Datasheet - Automated SQL Profiling & Tuning for Optimized Perfo...DB Optimizer Datasheet - Automated SQL Profiling & Tuning for Optimized Perfo...
DB Optimizer Datasheet - Automated SQL Profiling & Tuning for Optimized Perfo...Embarcadero Technologies
 
Reviewing sql server permissions tech republic
Reviewing sql server permissions   tech republicReviewing sql server permissions   tech republic
Reviewing sql server permissions tech republicKaing Menglieng
 
Administrators manual
Administrators manualAdministrators manual
Administrators manualScrumDesk
 
Download Presentation
Download PresentationDownload Presentation
Download Presentationwebhostingguy
 

What's hot (9)

Installing ingres enterprise access in a system which already has an ingres i...
Installing ingres enterprise access in a system which already has an ingres i...Installing ingres enterprise access in a system which already has an ingres i...
Installing ingres enterprise access in a system which already has an ingres i...
 
Back2 Basic Tools
Back2 Basic ToolsBack2 Basic Tools
Back2 Basic Tools
 
DB Optimizer Datasheet - Automated SQL Profiling & Tuning for Optimized Perfo...
DB Optimizer Datasheet - Automated SQL Profiling & Tuning for Optimized Perfo...DB Optimizer Datasheet - Automated SQL Profiling & Tuning for Optimized Perfo...
DB Optimizer Datasheet - Automated SQL Profiling & Tuning for Optimized Perfo...
 
ssssss
ssssssssssss
ssssss
 
Isqlplus command
Isqlplus commandIsqlplus command
Isqlplus command
 
Reviewing sql server permissions tech republic
Reviewing sql server permissions   tech republicReviewing sql server permissions   tech republic
Reviewing sql server permissions tech republic
 
Administrators manual
Administrators manualAdministrators manual
Administrators manual
 
Readme
ReadmeReadme
Readme
 
Download Presentation
Download PresentationDownload Presentation
Download Presentation
 

Viewers also liked

Offre d'emploi Ingénieur réseaux et sécurité
Offre d'emploi Ingénieur réseaux et sécuritéOffre d'emploi Ingénieur réseaux et sécurité
Offre d'emploi Ingénieur réseaux et sécuritéSimstream
 
Presentatie framingham-bij-ouderen
Presentatie framingham-bij-ouderenPresentatie framingham-bij-ouderen
Presentatie framingham-bij-ouderenBo Gevers
 
Mesothelioma Lawyer | Asbestos Attorney - YouTube
Mesothelioma Lawyer | Asbestos Attorney - YouTubeMesothelioma Lawyer | Asbestos Attorney - YouTube
Mesothelioma Lawyer | Asbestos Attorney - YouTubeOswaldo Danna
 
AEMME Internacionalización de la Microempresa Española.
AEMME Internacionalización de la Microempresa Española. AEMME Internacionalización de la Microempresa Española.
AEMME Internacionalización de la Microempresa Española. Carmen Urbano
 
Microsoft - Acando Inspiration Day
Microsoft - Acando Inspiration DayMicrosoft - Acando Inspiration Day
Microsoft - Acando Inspiration DayAcando Sweden
 
Vittorio analisa: conheça o fenômeno Muguruza
Vittorio analisa: conheça o fenômeno MuguruzaVittorio analisa: conheça o fenômeno Muguruza
Vittorio analisa: conheça o fenômeno MuguruzaVittorioTedeschi
 
King - Acando Inspiration Day
King - Acando Inspiration DayKing - Acando Inspiration Day
King - Acando Inspiration DayAcando Sweden
 
30 DAY CHALLENGE OF PTEASLIM - LOSS YOUR WEIGHT WITHING 30 DAYS.
30 DAY CHALLENGE OF PTEASLIM - LOSS YOUR WEIGHT WITHING 30 DAYS.30 DAY CHALLENGE OF PTEASLIM - LOSS YOUR WEIGHT WITHING 30 DAYS.
30 DAY CHALLENGE OF PTEASLIM - LOSS YOUR WEIGHT WITHING 30 DAYS.noyon sazu
 
E1 teorias y modelos silispolo
E1 teorias y modelos silispoloE1 teorias y modelos silispolo
E1 teorias y modelos silispoloAlejandra Silis
 
20 claves Inycom del eCommerce internacional
20 claves Inycom del eCommerce internacional20 claves Inycom del eCommerce internacional
20 claves Inycom del eCommerce internacionalCarmen Urbano
 
20 claves Inycom del ecommerce internacional
20 claves Inycom del ecommerce internacional 20 claves Inycom del ecommerce internacional
20 claves Inycom del ecommerce internacional Carmen Urbano
 

Viewers also liked (13)

Offre d'emploi Ingénieur réseaux et sécurité
Offre d'emploi Ingénieur réseaux et sécuritéOffre d'emploi Ingénieur réseaux et sécurité
Offre d'emploi Ingénieur réseaux et sécurité
 
Presentatie framingham-bij-ouderen
Presentatie framingham-bij-ouderenPresentatie framingham-bij-ouderen
Presentatie framingham-bij-ouderen
 
Prensa – utn avellaneda cursos y talleres
Prensa – utn avellaneda   cursos y talleresPrensa – utn avellaneda   cursos y talleres
Prensa – utn avellaneda cursos y talleres
 
Mesothelioma Lawyer | Asbestos Attorney - YouTube
Mesothelioma Lawyer | Asbestos Attorney - YouTubeMesothelioma Lawyer | Asbestos Attorney - YouTube
Mesothelioma Lawyer | Asbestos Attorney - YouTube
 
AEMME Internacionalización de la Microempresa Española.
AEMME Internacionalización de la Microempresa Española. AEMME Internacionalización de la Microempresa Española.
AEMME Internacionalización de la Microempresa Española.
 
Microsoft - Acando Inspiration Day
Microsoft - Acando Inspiration DayMicrosoft - Acando Inspiration Day
Microsoft - Acando Inspiration Day
 
Vittorio analisa: conheça o fenômeno Muguruza
Vittorio analisa: conheça o fenômeno MuguruzaVittorio analisa: conheça o fenômeno Muguruza
Vittorio analisa: conheça o fenômeno Muguruza
 
PBM4029moores21239673
PBM4029moores21239673PBM4029moores21239673
PBM4029moores21239673
 
King - Acando Inspiration Day
King - Acando Inspiration DayKing - Acando Inspiration Day
King - Acando Inspiration Day
 
30 DAY CHALLENGE OF PTEASLIM - LOSS YOUR WEIGHT WITHING 30 DAYS.
30 DAY CHALLENGE OF PTEASLIM - LOSS YOUR WEIGHT WITHING 30 DAYS.30 DAY CHALLENGE OF PTEASLIM - LOSS YOUR WEIGHT WITHING 30 DAYS.
30 DAY CHALLENGE OF PTEASLIM - LOSS YOUR WEIGHT WITHING 30 DAYS.
 
E1 teorias y modelos silispolo
E1 teorias y modelos silispoloE1 teorias y modelos silispolo
E1 teorias y modelos silispolo
 
20 claves Inycom del eCommerce internacional
20 claves Inycom del eCommerce internacional20 claves Inycom del eCommerce internacional
20 claves Inycom del eCommerce internacional
 
20 claves Inycom del ecommerce internacional
20 claves Inycom del ecommerce internacional 20 claves Inycom del ecommerce internacional
20 claves Inycom del ecommerce internacional
 

Similar to 08 qmds2005 session11

10 qmds2005 session14
10 qmds2005 session1410 qmds2005 session14
10 qmds2005 session14Niit Care
 
09 qmds2005 session13
09 qmds2005 session1309 qmds2005 session13
09 qmds2005 session13Niit Care
 
05 qmds2005 session07
05 qmds2005 session0705 qmds2005 session07
05 qmds2005 session07Niit Care
 
4) databases
4) databases4) databases
4) databasestechbed
 
Session 2: SQL Server 2012 with Christian Malbeuf
Session 2: SQL Server 2012 with Christian MalbeufSession 2: SQL Server 2012 with Christian Malbeuf
Session 2: SQL Server 2012 with Christian MalbeufCTE Solutions Inc.
 
Getting Started with MySQL II
Getting Started with MySQL IIGetting Started with MySQL II
Getting Started with MySQL IISankhya_Analytics
 
06 qmds2005 session08
06 qmds2005 session0806 qmds2005 session08
06 qmds2005 session08Niit Care
 
Summary tables with flexviews
Summary tables with flexviewsSummary tables with flexviews
Summary tables with flexviewsJustin Swanhart
 
Sql server indexed views speed up your select queries part 1 - code-projec
Sql server indexed views   speed up your select queries  part 1 - code-projecSql server indexed views   speed up your select queries  part 1 - code-projec
Sql server indexed views speed up your select queries part 1 - code-projecKaing Menglieng
 
Ms sql server architecture
Ms sql server architectureMs sql server architecture
Ms sql server architectureAjeet Singh
 
Types of sql commands by naveen kumar veligeti
Types of sql commands by naveen kumar veligetiTypes of sql commands by naveen kumar veligeti
Types of sql commands by naveen kumar veligetiNaveen Kumar Veligeti
 
Access tips access and sql part 1 setting the sql scene
Access tips  access and sql part 1  setting the sql sceneAccess tips  access and sql part 1  setting the sql scene
Access tips access and sql part 1 setting the sql scenequest2900
 
Managing SQLserver for the reluctant DBA
Managing SQLserver for the reluctant DBAManaging SQLserver for the reluctant DBA
Managing SQLserver for the reluctant DBAConcentrated Technology
 
Sql server 2012 tutorials writing transact-sql statements
Sql server 2012 tutorials   writing transact-sql statementsSql server 2012 tutorials   writing transact-sql statements
Sql server 2012 tutorials writing transact-sql statementsSteve Xu
 

Similar to 08 qmds2005 session11 (20)

10 qmds2005 session14
10 qmds2005 session1410 qmds2005 session14
10 qmds2005 session14
 
09 qmds2005 session13
09 qmds2005 session1309 qmds2005 session13
09 qmds2005 session13
 
05 qmds2005 session07
05 qmds2005 session0705 qmds2005 session07
05 qmds2005 session07
 
6232 b 04
6232 b 046232 b 04
6232 b 04
 
4) databases
4) databases4) databases
4) databases
 
Session 2: SQL Server 2012 with Christian Malbeuf
Session 2: SQL Server 2012 with Christian MalbeufSession 2: SQL Server 2012 with Christian Malbeuf
Session 2: SQL Server 2012 with Christian Malbeuf
 
FREE Sql Server syllabus
FREE Sql Server syllabusFREE Sql Server syllabus
FREE Sql Server syllabus
 
Day2
Day2Day2
Day2
 
Winter course
Winter courseWinter course
Winter course
 
Getting Started with MySQL II
Getting Started with MySQL IIGetting Started with MySQL II
Getting Started with MySQL II
 
06 qmds2005 session08
06 qmds2005 session0806 qmds2005 session08
06 qmds2005 session08
 
Summary tables with flexviews
Summary tables with flexviewsSummary tables with flexviews
Summary tables with flexviews
 
Sql server indexed views speed up your select queries part 1 - code-projec
Sql server indexed views   speed up your select queries  part 1 - code-projecSql server indexed views   speed up your select queries  part 1 - code-projec
Sql server indexed views speed up your select queries part 1 - code-projec
 
Ms sql server architecture
Ms sql server architectureMs sql server architecture
Ms sql server architecture
 
Types of sql commands by naveen kumar veligeti
Types of sql commands by naveen kumar veligetiTypes of sql commands by naveen kumar veligeti
Types of sql commands by naveen kumar veligeti
 
Access tips access and sql part 1 setting the sql scene
Access tips  access and sql part 1  setting the sql sceneAccess tips  access and sql part 1  setting the sql scene
Access tips access and sql part 1 setting the sql scene
 
Sql views
Sql viewsSql views
Sql views
 
Oracle SQL Training in Chennai, Tambaram
Oracle SQL Training in Chennai, TambaramOracle SQL Training in Chennai, Tambaram
Oracle SQL Training in Chennai, Tambaram
 
Managing SQLserver for the reluctant DBA
Managing SQLserver for the reluctant DBAManaging SQLserver for the reluctant DBA
Managing SQLserver for the reluctant DBA
 
Sql server 2012 tutorials writing transact-sql statements
Sql server 2012 tutorials   writing transact-sql statementsSql server 2012 tutorials   writing transact-sql statements
Sql server 2012 tutorials writing transact-sql statements
 

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

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
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
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
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
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
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
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
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 

Recently uploaded (20)

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
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
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
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
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
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
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
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 

08 qmds2005 session11

  • 1. Querying and Managing Data Using SQL Server 2005 Objectives In this session, you will learn to: Create and manage views Implement a full-text search Implement batches Ver. 1.0 Session 11 Slide 1 of 31
  • 2. Querying and Managing Data Using SQL Server 2005 Creating Views A View is: Used to view data from tables Similar to creating tables but it does not contain any data as it derives its data from the underlying tables Created by using CREATE VIEW statement Syntax: CREATE VIEW view_name [(column_name [, column_name]...)] [WITH ENCRYPTION] [, SCHEMABINDING]] AS select_statement [WITH CHECK OPTION] Let’s see how… Ver. 1.0 Session 11 Slide 2 of 31
  • 3. Querying and Managing Data Using SQL Server 2005 Indexing Views Indexing of Views: Is done when the volume of data in the underlying table is large and not updated frequently Improves query performance Is performed by creating an unique clustered index on a view and afterwards nonclustered index can also be created Is performed by using CREATE INDEX statement Let’s see how… Ver. 1.0 Session 11 Slide 3 of 31
  • 4. Querying and Managing Data Using SQL Server 2005 Just a minute In which of the following conditions will you NOT create an indexed view: 1. When the data is large 2. When the data is regularly updated 3. When you need to improve the performance of the view Answer: 2. When the data is regularly updated Ver. 1.0 Session 11 Slide 4 of 31
  • 5. Querying and Managing Data Using SQL Server 2005 Managing Views Managing a view involves altering, dropping, or renaming. Altering a view involves modifying a view without dropping it. Syntax: ALTER VIEW view_name [(column_name)] WITH ENCRYPTION] AS select_statement WITH CHECK OPTION] Dropping a view involves deleting the view when it is no longer required. Syntax: DROP VIEW view_name Ver. 1.0 Session 11 Slide 5 of 31
  • 6. Querying and Managing Data Using SQL Server 2005 Managing Views (Contd.) Renaming the name of the view without deleting it. Syntax: sp_rename old_viewname, new_viewname Let’s see how… Ver. 1.0 Session 11 Slide 6 of 31
  • 7. Querying and Managing Data Using SQL Server 2005 Demo: Creating Views Problem Statement: You are a database developer at AdventureWorks, Inc. You need to frequently generate a report containing the following details of the employees: Employee ID Employee First Name Employee Last Name Title Manager First Name Manager Last Name Ver. 1.0 Session 11 Slide 7 of 31
  • 8. Querying and Managing Data Using SQL Server 2005 Demo: Creating Views (Contd.) Problem Statement (Contd.): To retrieve this data, you need to execute the following statement on the database: SELECT e1.EmployeeID, c1.FirstName, c1.LastName, e1.Title, c2.FirstName AS [Manager First Name],c2.LastName AS [Manager Last Name] FROM HumanResources.Employee e1 INNER JOIN Person.Contact c1 ON e1.ContactID = c1.ContactID INNER JOIN HumanResources.Employee AS e2 ON e1.ManagerID = e2.EmployeeID INNER JOIN Person.Contact AS c2 ON e2.ContactID = c2.ContactID Ver. 1.0 Session 11 Slide 8 of 31
  • 9. Querying and Managing Data Using SQL Server 2005 Demo: Creating Views (Contd.) Problem Statement (Contd.): As a database developer, you need to simplify the execution of this query so that you do not need to send such a large query to the database engine every time the report is required. Ver. 1.0 Session 11 Slide 9 of 31
  • 10. Querying and Managing Data Using SQL Server 2005 Demo: Creating Views (Contd.) Solution: To solve the preceding problem, you need to perform the following tasks: 1. Create a view. 2. Verify the simplification of the query execution. Ver. 1.0 Session 11 Slide 10 of 31
  • 11. Querying and Managing Data Using SQL Server 2005 Configuring Full-Text Search Full-Text Search: Allows you to perform complex search on the data Allows you to search for synonyms or antonyms of a particular word Feature is disabled by default To retrieve the required details by using full-text, you need to perform the following tasks: 1. Enable the full-text search in the database. 2. Create a full-text catalog. 3. Create a unique index. 4. Create a full-text index. 5. Populate the full-text index. Let’s see how… Ver. 1.0 Session 11 Slide 11 of 31
  • 12. Querying and Managing Data Using SQL Server 2005 Searching Data by Using a Full-Text Search To search using full-text index, use the following full-text predicates: – FREETEXT: Searches for any variation of a word or a group of words given in the search column. – CONTAINS: Searches for a specific phrase, for the proximity of words within a text or for the exact match. Let’s see how… Ver. 1.0 Session 11 Slide 12 of 31
  • 13. Querying and Managing Data Using SQL Server 2005 Just a minute List the types of full-text index population methods. Answer: The three types of full-text index population methods are: 1. Full population 2. Change tracking based population 3. Incremental timestamp based population Ver. 1.0 Session 11 Slide 13 of 31
  • 14. Querying and Managing Data Using SQL Server 2005 Just a minute Which predicate is used to search for a specific phrase or for an exact match? Answer: CONTAINS Ver. 1.0 Session 11 Slide 14 of 31
  • 15. Querying and Managing Data Using SQL Server 2005 Demo: Implementing Full-Text Search Problem Statement: The users at AdventureWorks, Inc. need to frequently search for employees, customers, or vendors based on the location. The location details of all these entities are stored in the Address table in the Person schema. The users want to search for addresses with different combinations of the words specified in the search criteria. For example, they need to search for the locations that contain the words ‘Santa’ and ‘drive’ in the AddressLine1 column. Similarly, they might need to search for the locations that contain the words ‘Santa’ and ‘Street’ in the AddressLine1 column. How will you enable the users to perform such a data search? Ver. 1.0 Session 11 Slide 15 of 31
  • 16. Querying and Managing Data Using SQL Server 2005 Demo: Implementing Full-Text Search (Contd.) Solution: To solve the preceding problem, you need to perform the following tasks: 1. Enable the Full-text search on the AdventureWorks database. 2. Create a default full-text catalog. 3. Create a full-text index. 4. Search the data by using the CONTAINS predicate. Ver. 1.0 Session 11 Slide 16 of 31
  • 17. Querying and Managing Data Using SQL Server 2005 Creating Batches Batch: Is created to send a group of SQL statements together to SQL Server for execution Compiles the statements as a single executable unit called an execution plan Uses GO command at the end to send the SQL statements to the instance of SQL Server Uses variables to store values. They are: • Local variables • Global variables Uses PRINT statement to display user-defined messages and values of variables Let’s see how… Ver. 1.0 Session 11 Slide 17 of 31
  • 18. Querying and Managing Data Using SQL Server 2005 Just a minute Which of the following statements can be used within a batch? 1. CREATE FUNCTION 2. CREATE RULE 3. DECLARE Answer: 3. DECLARE Ver. 1.0 Session 11 Slide 18 of 31
  • 19. Querying and Managing Data Using SQL Server 2005 Using Constructs Allow batches to perform conditional execution of statements with the following control-of-flow statements: IF…ELSE statement CASE statement WHILE statement Ver. 1.0 Session 11 Slide 19 of 31
  • 20. Querying and Managing Data Using SQL Server 2005 Using Constructs (Contd.) IF…ELSE statement: Performs a particular action based on the result of the boolean expression Syntax: IF boolean_expression {sql_statement | statement_block} [ELSE boolean_expression {sql_statement | statement_block}] Let’s see how… Ver. 1.0 Session 11 Slide 20 of 31
  • 21. Querying and Managing Data Using SQL Server 2005 Using Constructs (Contd.) CASE statement: Evaluates a list of conditions and returns one of the various possible results Syntax: CASE WHEN boolean_expression THEN expression [[WHEN boolean_expression THEN expression] [...]] [ELSE expression] END Let’s see how… Ver. 1.0 Session 11 Slide 21 of 31
  • 22. Querying and Managing Data Using SQL Server 2005 Using Constructs (Contd.) WHILE statement: Executes a batch repeatedly as long as the given condition holds true Uses BREAK and CONTINUE statements to break the loop or to continue the loop Syntax: WHILE boolean_expression {sql_statement | statement_block} [BREAK] {sql_statement | statement_block} [CONTINUE] Let’s see how… Ver. 1.0 Session 11 Slide 22 of 31
  • 23. Querying and Managing Data Using SQL Server 2005 Handling Errors and Exceptions Errors in SQL Server can be handled on two levels: Using the TRY-CATCH construct Using the RAISERROR statement Ver. 1.0 Session 11 Slide 23 of 31
  • 24. Querying and Managing Data Using SQL Server 2005 Handling Errors and Exceptions (Contd.) TRY-CATCH construct is used in the following way: Try block encloses a group of T-SQL statements. If any error occurs in the statements of TRY block, control passes to the CATCH block. CATCH block encloses another group of statements, which gets executed when an error occurs. Let’s see how… Ver. 1.0 Session 11 Slide 24 of 31
  • 25. Querying and Managing Data Using SQL Server 2005 Handling Errors and Exceptions (Contd.) RAISERROR: Is used to return messages back to the called applications Uses the same message format as a system error or warning message generated by the SQL Server Database Engine Can also return user-defined error messages Let’s see how… Ver. 1.0 Session 11 Slide 25 of 31
  • 26. Querying and Managing Data Using SQL Server 2005 Just a minute Which system function returns the text of the error message when used in the CATCH block? Answer: ERROR_MESSAGE() Ver. 1.0 Session 11 Slide 26 of 31
  • 27. Querying and Managing Data Using SQL Server 2005 Just a minute How can you return a user-defined error message in a batch? Answer: Using the RAISERROR statement Ver. 1.0 Session 11 Slide 27 of 31
  • 28. Querying and Managing Data Using SQL Server 2005 Summary In this session, you learned that: A view is a virtual table, which derives its data from one or more tables known as the base or underlying tables. Views serve as security mechanisms, thereby protecting data in the base tables. The 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. The SQL Server enables users to search for a wide range of text in the SQL tables through a full-text query feature. You can enable the full-text search by using the command, SP_FULLTEXT_DATABASE ENABLE. A full-text catalog can be created by using the CREATE FULLTEXT CATALOG statement. Ver. 1.0 Session 11 Slide 28 of 31
  • 29. Querying and Managing Data Using SQL Server 2005 Summary (Contd.) – A full-text index can be created by using the CREATE FULLTEXT INDEX statement. – Three types of full-text population methods are full population, change tracking-based population, and incremental timestamp-based population. – Full-text predicates that can be used to perform full-text search are CONTAINS and FREETEXT. – A FREETEXT predicate searches for any variation of a word or a group of words given in the search column. – The CONTAINS predicate searches for a specific phrase or for the exact match. – 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. Ver. 1.0 Session 11 Slide 29 of 31
  • 30. Querying and Managing Data Using SQL Server 2005 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 the 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. The BREAK statement causes an exit from the WHILE loop. Ver. 1.0 Session 11 Slide 30 of 31
  • 31. Querying and Managing Data Using SQL Server 2005 Summary (Contd.) The CONTINUE statement causes the WHILE loop to restart, skipping any statements after the CONTINUE statement within the loop. Two ways to handle errors in batches are: • TRY-CATCH construct • RAISERROR statement Ver. 1.0 Session 11 Slide 31 of 31

Editor's Notes

  1. Start the session by sharing the objectives with the students.
  2. In this slide, you will explain the concept of views to the students. Explain the students that views are used for two basic reasons – to simplify complex queries for users and to restrict users from viewing data directly from the table. While explaining views to the students you can take the example of the small window on your class room door. If somebody peeps through it he or she will only be able to see the only a small portion of the class and will think that the visible portion is the entire class room. Similarly for a view which displays only say 4 columns of a table, the user will perceive that the table contains only 4 columns. It is a good practice to follow naming conventions while creating views. Prefix the name using ‘vw’. If you want to restrict users from seeing the definition of the view using the sp_helptext command, you can encrypt the definition using the WITH ENCRYPTION option in the CREATE VIEW statement. You need to stress the fact that the views cannot store data by themselves, they get the data from the underlying base tables. A view is nothing but a query stored as an object. You can use the examples given in the Student Guide to clarify the concept to the students. Further, you can execute the following statements to explain the concept: Example: CREATE VIEW vwEmployeeDepData AS SELECT e.EmployeeID, MaritalStatus, DepartmentID FROM HumanResources.Employee e JOIN HumanResources.EmployeeDepartmentHistory d ON e.EmployeeID = d.EmployeeID Additional Input Views are slow as the query is processed each time the view is used.
  3. In this slide, you need to explain the concept, importance, and benefit of indexing the views. You can use the examples given in the Student Guide to clarify the concept of indexed views to the students. Further, you can execute the following statements to explain the concept: Example CREATE UNIQUE CLUSTERED INDEX idx_vwEmployeeDepData ON HumanResources.vwEmployeeDepData (EmployeeID, DepartmentID) Before executing the above statement, you need to bind the view vwEmployeeDepData to the schema as follows: ALTER VIEW HumanResources.vwEmployeeDepData WITH SCHEMABINDING AS SELECT e.EmployeeID, MaritalStatus, DepartmentID FROM HumanResources.Employee e JOIN HumanResources.EmployeeDepartmentHistory d ON e.EmployeeID = d.EmployeeID The above statement alters the existing view, vwEmployeeDepData and binds it with the schema of the underlying tables. After that, you can create a unique clustered index on the view as shown in the above statement.
  4. Reiterate the concepts taught in the preceding slides by asking the question.
  5. In this slide, you need to explain how to manage views to the students. You can use the examples given in the Student Guide to clarify the concept to the students. Further, you can execute the following statements to explain the concept: Example: (Alter) ALTER VIEW vwEmployeeDepData AS SELECT e.EmployeeID, LoginID, MaritalStatus, DepartmentID FROM HumanResources.Employee e JOIN HumanResources.EmployeeDepartmentHistory d ON e.EmployeeID = d.EmployeeID Example: (Drop) DROP VIEW vwEmployeeDepData
  6. In this slide, you will explain how to rename a view. You can execute the following statements to explain the concept: Example: (Rename) sp_rename vwSal, vwSalary
  7. You need to ensure that after the end of this demo, the students are able create views.
  8. You need to ensure that after the end of this demo, the students are able create views.
  9. You need to ensure that after the end of this demo, the students are able create views.
  10. In this slide, you need to explain the concept of full text search to the students. To clarify the concept of full-text search, you can give the example of a web based search engine. You can tell the students that if they want to include the functionalities similar to the functionality provided by a search engine like google, you can implement full-text in your database. Once you have explained the concepts, further you need to tell them about the full-text catalogs and full-text indexes. Stress on clarifying these concepts to the students. You can use the examples given in the Student Guide to clarify the concept to the students. Further, you can execute the following statements to explain the concept: Example: (Enable the full-text) Before using full text search feature of SQL Server, you first need to enable the database to use full-text service using the following statement: USE AdventureWorks GO SP_FULLTEXT_DATABASE ENABLE GO Example: (Create full-text catalog) The full-text catalog may have multiple full-text indexes. You can create a full-text catalog using the following command: CREATE FULLTEXT CATALOG CAT1 AS DEFAULT Example: (Create a unique index) You can create a unique index on the Production.ProductDescription table as shown below: CREATE UNIQUE INDEX IX_DESC ON Production.ProductDescription (ProductDescriptionID) Example: (Create full-text index) CREATE FULLTEXT INDEX ON PRODUCTION.PRODUCTDESCRIPTION (DESCRIPTION) KEY INDEX IX_DESC Example: (populate the full-text index) To create an empty full-text index on the ProductDescription table, you can execute the following statement: CREATE FULLTEXT INDEX ON PRODUCTION.PRODUCTDESCRIPTION (DESCRIPTION) KEY INDEX PK_ProductDescription_ProductDescriptionID WITH CHANGE_TRACKING OFF, NO POPULATION Next, to populate the index you need to execute the following statement: ALTER FULLTEXT INDEX ON Production.ProductDescription START FULL POPULATION The above statement will populate the full-text index created on the ProductDescription table. Additional Inputs: There are certain words that are used very often and may hinder a query. These words are called noise words and are excluded from your search string. For example, if your search string is “Who is the governor of California” , in this case full-text search will not look for the words like ‘is’ and ‘the’. Some of the noise words are a, an, the, and are. Full-text indexes can be created on the base tables but not on the views or the system tables. You need to have DBO rights to enable full-text search
  11. In this slide, you need to explain the concepts of various full-text predicates to the students. In addition, you also need to explain the difference between the two predicates. You can use the examples given in the Student Guide to clarify the concept to the students. Further, you can execute the following statements to explain the concept: Example: (FREETEXT) SELECT Description FROM Production.ProductDescription WHERE FREETEXT (Description, 'race winners') Example: (CONTAINS) SELECT Description FROM Production.ProductDescription WHERE CONTAINS (Description, 'Ride');
  12. Reiterate the concepts taught in the preceding slides by asking the question.
  13. Reiterate the concepts taught in the preceding slides by asking the question.
  14. You need to ensure that after the end of this demo, the students are able to implement full-text in a database.
  15. In this slide, you need to explain batches to the students. Explain the benefits of using batches clearly. Stress on the fact that batches help in reducing the network traffic. You can use the examples given in the Student Guide to clarify the concept to the students. Further, you can execute the following statements to explain the concept: Example: DECLARE @Rate int SELECT @Rate = max(Rate) FROM HumanResources.EmployeePayHistory PRINT @Rate GO
  16. Reiterate the concepts taught in the preceding slides by asking the question.
  17. In this slide, you need to define constructs to the students. As students have already read the constructs in previous modules, you need not to put much stress on the explanation of these.
  18. In this slide, you need to need to explain IF..ELSE construct to the students. You can use the examples given in the Student Guide to clarify the concept to the students. Further, you can execute the following statements to explain the concept: Example: (IF…ELSE) DECLARE @Rate money SELECT @Rate = Rate FROM HumanResources.EmployeePayHistory WHERE EmployeeID = 23 IF @Rate < 15 PRINT 'Review required' ELSE BEGIN PRINT 'Review not required' PRINT 'your rate =' PRINT @Rate END GO Tell your students that when more than one statement needs to be executed, the statements needs to be put within the BEGIN and END block, else only the first line of the SQL block gets executed. Remember that a block of SQL statements executed together is called a batch. Demonstrate a set of INSERT statements or SELECT statements as a batch.
  19. In this slide, you need to explain the CASE construct to the students. You can use the examples given in the Student Guide to clarify the concept to the students. Further, you can execute the following statements to explain the concept: Example: (CASE) SELECT EmployeeID, 'Marital Status' = CASE MaritalStatus WHEN 'M' THEN 'Married' WHEN 'S' THEN 'Single' ELSE 'Not specified' END FROM HumanResources.Employee GO
  20. In this slide, you need to explain the concept of WHILE construct to the students. You can use the examples given in the Student Guide to clarify the concept to the students. Further, you can execute the following statements to explain the concept: Example: (WHILE) WHILE (SELECT AVG(Rate)+1 from HumanResources.EmployeePayHistory) < 20 BEGIN UPDATE HumanResources.EmployeePayHistory SET Rate = Rate + 1 FROM HumanResources.EmployeePayHistory IF (Select MAX(Rate)+1 from HumanResources.EmployeePayHistory) > 127 BREAK ELSE CONTINUE END
  21. In this slide, you need to explain your students about errors. You can use the examples given in the Student Guide to clarify the concept to the students.
  22. In this slide, you will explain the concept of TRY-CATCH block. Tell your students that this type of handling is known as structured error handling. You can use the examples given in the Student Guide to clarify the concept to the students. Further, you can execute the following statements to explain the concept: Example BEGIN TRY -- This will generate an error, as EmployeeID is an IDENTITY column -- Ergo, we can't specify a value for this column... INSERT INTO HumanResources.Employee(EmployeeID, Title) VALUES(1, 'Tool Designer') END TRY BEGIN CATCH SELECT 'There was an error! ' + ERROR_MESSAGE() AS ErrorMessage, ERROR_LINE() AS ErrorLine, ERROR_NUMBER() AS ErrorNumber, ERROR_PROCEDURE() AS ErrorProcedure, ERROR_SEVERITY() AS ErrorSeverity, ERROR_STATE() AS ErrorState END CATCH Additional Input TRY-CATCH blocks were not in the previous versions of SQL Server.
  23. In this slide, you need to explain the concept of RAISERROR to the students. You can use the examples given in the Student Guide to clarify the concept to the students. Further, you can execute the following statements to explain the concept: Example: Declare @Start datetime Declare @End datetime Declare @Date_diff int Select @Start = '1900-01-01 23:00:00.000', @End = '1900-01-02 06:00:00.000' Select @Date_diff = DATEDIFF(hh, @Start, @End) If (@Date_diff != 8) RAISERROR('Error Raised', 16, 1) ELSE BEGIN Update HumanResources.Shift Set StartTime = @Start, EndTime = @End Where ShiftID = 3 End End TRY BEGIN CATCH SELECT 'The difference between Start and End time should be 8 hours' End CATCH
  24. Reiterate the concepts taught in the preceding slides by asking the question.
  25. Reiterate the concepts taught in the preceding slides by asking the question.
  26. You can summarize the session by running through the summary given in SG. In addition, you can also ask students summarize what they have learnt in this session.
  27. You can summarize the session by running through the summary given in SG. In addition, you can also ask students summarize what they have learnt in this session.
  28. You can summarize the session by running through the summary given in SG. In addition, you can also ask students summarize what they have learnt in this session.