Collaborate Chapter 4 ¤NIIT Querying, Managing, and Administering Databases Using SQL Server 2005 4.3 In this section, you will learn about: Creating partitioned indexes System-defined stored procedures Read the following topic in the section Creating and Managing Indexes of Chapter 6 of the book Querying and Managing Data Using SQL Server 2005: Creating Partitioned Indexes Read the following topic in the Appendix of the book Querying and Managing Data Using SQL Server 2005: System-Defined Procedures Knowledge Byte Creating Partitioned Indexes System-Defined Procedures 4.4 Querying, Managing, and Administering Databases Using SQL Server 2005 ¤NIIT This section contains: Best practices Tips and tricks FAQs The following best practices can be considered while using the views, batches, stored procedures, and functions in SQL Server 2005: Create small stored procedures: Keep the stored procedures as small as possible. This is useful in a situation where two users are accessing the same stored procedure at the same time and the two query plans are stored in the cache. It is better to have smaller stored procedure calls than one very large stored procedure because it makes maintaining the code easier. Return values from stored procedures: Ensure that all stored procedures return a value indicating their status. This will help evaluate their success or failure status. Use the UNION ALL operator to combine the records of two tables: While creating a view if the records are combined from multiple tables having different number of columns, you should use the UNION ALL operator. Ensure that the output of both the SELECT statements involved in the UNION ALL operator has the same number of columns. Refresh the views when updating the table: When performing any operation on a table, such as inserting columns in a table, keep refreshing the dependent views. For example, suppose you are adding a new column to a table that is being referenced by a view. In such a situation, the new column will not automatically be reflected to the view, especially if the view is performing a SELECT * from the table. For the new column to be reflected on the view, you have to refresh the definition of the view by using the sp_refreshview system stored procedure. From the Expert’s Des k Best Practices ¤NIIT Querying, Managing, and Administering Databases Using SQL Server 2005 4.5 The following tips and tricks will help you use views, batches, stored procedures, and functions in SQL Server 2005: Use SET_NOCOUNT_ON at the beginning of SQL batches, stored procedures, and triggers. This will increase the performance by reducing network traffic. Setting SET_NOCOUNT_ON suppresses the messages regarding how many rows are affected after executing the INSERT, UPDATE, SELECT and DELETE statements. Use the following procedure to find keywords in stored procedures: CREATE PROCEDURE <proc name> @<var name> VARCHAR(100) AS SELECT routine_name FROM INFORMATION_SCHEMA.ROUTINES WHERE routine_definition LIKE '%' + @<var name> + '%' ORDER BY routine_name The preceding code sample will allow you to search the stored procedures defined in the SQL Server, which contains some specific keyword such as INDEX, TRIGGER or UPDATE. Use catalog views to access metadata of different objects in SQL Server 2005. It is a rich mechanism that does not require access to system tables. Use a batch to execute a set of SQL statements. Use procedures to use a set of SQL statements more than once. Procedures allow you to save a set of SQL statements whereas batches are temporary in nature. Can you write SQL Server 2005 stored procedures without the knowledge of any T- SQL statements? The belief that the SQL Server 2005 stored procedures can be written without the knowledge of T-SQL statements is partially true. While it is technically possible to create a stored procedure without using T-SQL, it is not possible to access any data without using T-SQL statements. Can a view contain a CASE statement? Yes, you can use a CASE statement in a view, but you have to create the view by using Query Analyzer and not SQL Server Enterprise Manager. There is a limitation in creating views in Enterprise Manager when the view contains a CASE statement. Tips and Tricks FAQs 4.6 Querying, Managing, and Administering Databases Using SQL Server 2005 ¤NIIT Can you use the FREETEXT predicate in a programming language with a SELECT statement? Yes, the FREETEXT predicate can be used in a programming language with a SELECT statement in the same way as it is used in SQL Server. Is it must to create a unique index on a table before creating a full-text index on it? Yes, it is must to create a unique index on a table before creating a full-text index on it. Can you create more that one full-text index on a table? Yes, you can create more than one full-text index on table on different columns. When you use the different full-text predicates with the SELECT statement, the suitable full-text index is automatically selected. ¤NIIT Querying, Managing, and Administering Databases Using SQL Server 2005 4.7 1. Which system-defined stored procedure is used to rename a view? a. sp_renamedb b. sp_configure c. sp_recompile d. sp_rename 2. Which of the following is the correct description for the sp_configure stored procedure? a. It is used to display information about SQL Server configuration. b. It is used to set the configuration of SQL Server 2005. c. It is used to enable the CLR integration feature in the database. d. It is used to extract the CLR integration information of the database. 3. Which edition of SQL Server 2005 allows partitioning? a. Express Edition b. Enterprise Edition c. Standard Edition d. Developer Edition 4. While creating a partitioned index, what do you create after creating a partition scheme? a. Partition function b. Partition table c. Clustered index d. Nonclustered index 5. Which task determines the boundary values for creating partitions? a. Creating a partition function b. Creating a partition scheme c. Creating a clustered index d. Creating nonclustered index Challenge 4.8 Querying, Managing, and Administering Databases Using SQL Server 2005 ¤NIIT 1. Which of the following is true about renaming a view? a. A view is renamed by using the sp_renameview procedure. b. A view can only be renamed by the owner of the database. c. A view can be renamed from another database. d. A view name must follow the rules for identifiers. 2. Michael has created a full-text index on the Author column of the Titles table by using the following command. CREATE FULLTEXT INDEX ON Titles (Author) KEY INDEX Ix.A Which of the following is an alternative to the given command to create a full-text index? a. Right-click the table in the Object Explorer window. Select Full-Text IndexoDefine Full-Text Index. b. Right-click the database in the Object Explorer window. Select Full-Text IndexoDefine Full-Text Index. c. Select the database in the Object Explorer window. Select Full-Text IndexoDefine Full-Text Index. d. Select the table in the Object Explorer window. Select Full-Text IndexoDefine Full-Text Index. 3. Which population method should be used first on a table after creating a full-text index? a. Change tracking-based population method b. Incremental population method c. Full population method d. Incremental timestamp-based population method 4. Harry has populated a table that is full-text indexed. After a month, Harry wants to update the new records from the base table to the full-text indexed table. Which population method should Harry use? a. Change tracking-based population method b. Incremental population method c. Full population method d. Incremental timestamp-based population method Home Assignment ¤NIIT Querying, Managing, and Administering Databases Using SQL Server 2005 4.9 5. Sam wants to add a column named Incentive in the Employee table. Sam also wants to update the Incentive column of the existing records depending on the grade of the employees. H wants to create a batch to perform the task but is facing a problem while creating a batch. Identify the problem. a. Sam cannot create a batch for the ALTER TABLE statement. b. Sam cannot create a batch that alters the table by adding a column and then updates the same column in the table. c. Sam cannot create a batch that uses the ALTER TABLE and UPDATE statements together. d. Sam cannot create a batch to add new columns in the table. 6. You are using the TRY…CATCH construct to execute a set of statements. Which system function returns the name of the stored procedure or trigger in which the error has occurred? a. ERROR_PROCEDURE() b. ERROR_LINE() c. ERROR_TRIGGER() d. ERROR_MESSAGE() 7. Which RAISERROR severity level transfers the control from the TRY block to the CATCH block? a. 11 to 16 b. 11 to 19 c. 10 to 19 d. 10 to 16 8. Which of the following is an invalid statement to execute a procedure named myproc? a. EXEC myproc b. EXEC PROCEDURE myproc c. EXEC PROC myproc d. EXEC PROCEDURE 9. Consider the following code snippet: CREATE PROCEDURE myproc @pr1 int, @pr2 char(50) OUTPUT, @pr3 int OUTPUT AS BEGIN …………… ………… END 4.10 Querying, Managing, and Administering Databases Using SQL Server 2005 ¤NIIT Which of the following is a correct explanation of the preceding code snippet? a. The code creates a procedure named myproc with three input parameters. b. The code creates a procedure named myproc with one input and two output parameters. c. The code creates a procedure named myproc with three variables to store data. d. The code creates a procedure named myproc with invalid syntax. 10. Which of the following is not a component of UDF declaration? a. Input parameter name and data type b. Options applicable to the input parameter c. Return parameter data type and optional name d. Output parameter name and data type . Collaborate Chapter 4 ¤NIIT Querying, Managing, and Administering Databases Using SQL Server 2005 4. 3 In this section, you will learn about: Creating partitioned indexes System-defined. System-Defined Procedures Knowledge Byte Creating Partitioned Indexes System-Defined Procedures 4. 4 Querying, Managing, and Administering Databases Using SQL Server 2005 ¤NIIT This section contains:. boundary values for creating partitions? a. Creating a partition function b. Creating a partition scheme c. Creating a clustered index d. Creating nonclustered index Challenge 4. 8 Querying, Managing,