Finding all Stored Procedures that calls a Function in SQL Server | SQL Server Tutorial

The below Stored Procedure searches within all the stored procedures in the SQL server database, views, and functions for the given function name. This stored procedure also search for any text written in the stored procedure, not just function names.

    @SearchValue nvarchar(500)

SELECT DISTINCT'.' AS Object_Name,o.type_desc
    FROM sys.sql_modules        m
        INNER JOIN sys.objects  o ON m.object_id=o.object_id
        INNER JOIN sys.schemas  s ON o.schema_id=s.schema_id
    WHERE m.definition Like '%'+@SearchValue+'%'
        --AND o.Type='P'  --<uncomment if you only want to search procedures
    ORDER BY 1

Finding Stored Procedures that Accesses Particular Table or Column in SQL Server | SQL Server Tutorial

By querying the Procedures table in Sys as given below, we can find the SQL Server stored procedures that refers particular column are tables.

FROM sys.procedures

The result will be the list of SQL Server stored procedure names that refers to particular table or column.

Writing Effective SQL Queries | SQL Query 10 Best Practices Tips | SQL Server Programmer Guide

1. Eliminate Cursors from the Query

Try to remove cursors from the query and use set-based query; set-based query is more efficient than cursor-based. If there is a need to use cursors, then avoid dynamic cursors as they tend to limit the choice of plans available to the query optimizer. For example, dynamic cursor limits the optimizer to using nested loopjoins.

2. Avoid Use of Non-correlated Scalar Sub Query

You can re-write your query to remove non-correlated scalar sub query as a separate query instead of part of the main query and store the output in a variable, which can be referred to in the main query or later part of the batch. This will give better options to Optimizer, which may help to return accurate cardinality estimates along with a better plan.

3. Avoid Multi-statement Table Valued Functions (TVFs)

Multi-statement TVFs are more costly than inline TFVs. SQL Server expands inline TFVs into the main query like it expands views but evaluates multi-statement TVFs in a separate context from the main query and materializes the results of multi-statement into temporary work tables. The separate context and work table make multi-statement TVFs costly.

4. Creation and Use of Indexes

We are aware of the fact that Index can magically reduce the data retrieval time but have a reverse effect on DML operations, which may degrade query performance. With this fact, Indexing is a challenging task, but could help to improve SQL query performance and give you best query response time.

5. Understand the Data

Understand the data, its type and how queries are being performed to retrieve the data before making any decision to create an index. If you understand the behavior of data thoroughly, it will help you to decide which column should have either a clustered index or non-clustered index. If a clustered index is not on a unique column then SQL Server will maintain uniqueness by adding a unique identifier to every duplicate key, which leads to overhead. To avoid this type of overhead, choose the column correctly or make the appropriate changes.

6. Create a Highly Selective Index

Selectivity define the percentage of qualifying rows in the table (qualifying number of rows/total number of rows). If the ratio of the qualifying number of rows to the total number of rows is low, the index is highly selective and is most useful. A non-clustered index is most useful if the ratio is around 5% or less, which means if the index can eliminate 95% of the rows from consideration. If index is returning more than 5% of the rows in a table, it probably will not be used; either a different index will be chosen or created or the table will be scanned.

7. Position a Column in an Index

Order or position of a column in an index also plays a vital role to improve SQL query performance. An index can help to improve the SQL query performance if the criteria of the query matches the columns that are left most in the index key. As a best practice, most selective columns should be placed leftmost in the key of a non-clustered index.

8. Drop Unused Indexes

Dropping unused indexes can help to speed up data modifications without affecting data retrieval. Also, you need to define a strategy for batch processes that run infrequently and use certain indexes. In such cases, creating indexes in advance of batch processes and then dropping them when the batch processes are done helps to reduce the overhead on the database.

9. Statistic Creation and Updates

You need to take care of statistic creation and regular updates for computed columns and multi-columns referred in the query; the query optimizer uses information about the distribution of values in one or more columns of a table statistics to estimate the cardinality, or number of rows, in the query result. These cardinality estimates enable the query optimizer to create a high-quality query plan.

10. Revisit Your Schema Definitions

Last but not least, revisit your schema definitions; keep an eye out that appropriate FOREIGN KEYNOT NULL and CHECK constraints are in place or not. Availability of the right constraint at the right place always helps to improve the query performance, like FOREIGN KEYconstraint helps to simplify joins by converting some outer or semi-joins to inner joins and CHECK constraint also helps a bit by removing unnecessary or redundant predicates.

What is UNIQUE KEY constraint? | SQL Server Interview Question

A UNIQUE constraint enforces the uniqueness of the values in a set of columns, so no duplicate values are entered. The unique key constraints are used to enforce entity integrity as the primary key constraints.

What is PRIMARY KEY? | SQL Server Interview Question

A PRIMARY KEY constraint is a unique identifier for a row within a database table. Every table should have a primary key constraint to uniquely identify each row and only one primary key constraint can be created for each table. The primary key constraints are used to enforce entity integrity

What is SQL Server Agent? | SQL Server Interview Question

SQL Server agent plays an important role in the day-to-day tasks of a database administrator (DBA). It is often overlooked as one of the main tools for SQL Server management. Its purpose is to ease the implementation of tasks for the DBA, with its full-function scheduling engine, which allows you to schedule your own jobs and scripts.

What are the Types of Authentication Modes in SQL Server? How can it be changed? | SQL Server Interview Question

Windows mode and Mixed Mode – SQL & Windows.
To change authentication mode in SQL Server click Start, Programs, Microsoft SQL Server and click SQL Enterprise Manager to run SQL Enterprise Manager from the Microsoft SQL Server program group. Select the server then from the Tools menu select SQL Server Configuration Properties, and choose the Security page.

What is SQL Profiler? | SQL Server Interview Question

SQL Profiler is a graphical tool that allows system administrators to monitor events in an instance of Microsoft SQL Server. You can capture and save data about each event to a file or SQL Server table to analyze later. For example, you can monitor a production environment to see which stored procedures are hampering performances by executing too slowly.
Use SQL Profiler to monitor only the events in which you are interested. If traces are becoming too large, you can filter them based on the information you want, so that only a subset of the event data is collected. Monitoring too many events adds overhead to the server and the monitoring process and can cause the trace file or trace table to grow very large, especially when the monitoring process takes place over a long period of time.

What Are The Different Types of Sub-Queries? | SQL Server Interview Question

  • Single-row sub-query, where the sub-query returns only one row.
  • Multiple-row sub-query, where the sub-query returns multiple rows,. and
  • Multiple column sub-query, where the sub-query returns multiple columns

Explain the Properties of Sub-Queries? | SQL Server Interview Question

  • A sub-query must be enclosed in the parenthesis.
  • A sub-query must be put in the right hand of the comparison operator, and
  • A sub-query cannot contain an ORDER-BY clause.
  • A query can contain more than one sub-query.

Difference Between HAVING CLAUSE and WHERE CLAUSE? | SQL Server Interview Question

They specify a search condition for a group or an aggregate. But the difference is that HAVING can be used only with the SELECT statement. HAVING is typically used in a GROUP BY clause. When GROUP BY is not used, HAVING behaves like a WHERE clause. Having Clause is basically used only with the GROUP BY function in a query whereas WHERE Clause is applied to each row before they are part of the GROUP BY function in a query

Usage of UPDATE_STATISTICS command? | SQL Server Interview Question

This command is basically used when a large processing of data has occurred. If a large amount of deletions any modification or Bulk Copy into the tables has occurred, it has to update the indexes to take these changes into account. UPDATE_STATISTICS updates the indexes on these tables accordingly.

Difference Between DELETE & TRUNCATE Commands? | SQL Server Interview Question

Delete command removes the rows from a table based on the condition that we provide with a WHERE clause. Truncate will actually remove all the rows from a table and there will be no data in the table after we run the truncate command.
  • TRUNCATE is faster and uses fewer system and transaction log resources than DELETE.
  • TRUNCATE removes the data by deallocating the data pages used to store the table’s data, and only the page deallocations are recorded in the transaction log.
  • TRUNCATE removes all rows from a table, but the table structure, its columns, constraints, indexes and so on, remains. The counter used by an identity for new rows is reset to the seed for the column.
  • You cannot use TRUNCATE TABLE on a table referenced by a FOREIGN KEY constraint. Because TRUNCATE TABLE is not logged, it cannot activate a trigger.
  • TRUNCATE cannot be rolled back.
  • TRUNCATE is DDL Command.
  • TRUNCATE Resets identity of the table
  • DELETE removes rows one at a time and records an entry in the transaction log for each deleted row.
  • If you want to retain the identity counter, use DELETE instead. If you want to remove table definition and its data, use the DROP TABLE statement.
  • DELETE Can be used with or without a WHERE clause
  • DELETE Activates Triggers.
  • DELETE can be rolled back.
  • DELETE is DML Command.
  • DELETE does not reset identity of the table.

Difference Between Primary Key and Unique Key? | SQL Server Interview Question

Both primary key and unique key enforces uniqueness of the column on which they are defined. But by default primary key creates a clustered index on the column, where are unique creates a nonclustered index by default. Another major difference is that, primary key doesn’t allow NULLs, but unique key allows one NULL only.

What is OLTP (Online Transaction Processing)? | SQL Server Interview Question

In OLTP – online transaction processing systems relational database design use the discipline of data modeling and generally follow the Codd rules of data normalization in order to ensure absolute data integrity. Using these rules complex information is broken down into its most simple structures (a table) where all of the individual atomic level elements relate to each other and satisfy the normalization rules.

What are different types of Collation Sensitivity? | SQL Server Interview Question

Case sensitivity – A and a, B and b, etc.
Accent sensitivity – a and á, o and ó, etc.
Kana Sensitivity – When Japanese kana characters Hiragana and Katakana are treated differently, it is called Kana sensitive.
Width sensitivity – A single-byte character (half-width) and the same character represented as a double-byte character (full-width) are treated differently than it is width sensitive. 

Index Type Configurable to a Table? | SQL Server Interview Question

A table can have one of the following index configurations:
  • No indexes
  • A clustered index
  • A clustered index and many nonclustered indexes
  • A nonclustered index
  • Many nonclustered indexes

Difference Between Clustered and Non-Clustered Index? | SQL Server Interview Question

A clustered index is a special type of index that reorders the way records in the table are physically stored. Therefore table can have only one clustered index. The leaf nodes of a clustered index contain the data pages.
A non clustered index is a special type of index in which the logical order of the index does not match the physical stored order of the rows on disk. The leaf node of a non clustered index does not consist of the data pages. Instead, the leaf nodes contain index rows.

What are the properties of the Relational tables? | SQL Server Interview Question

Relational tables have six properties:
  • Values are atomic.
  • Column values are of the same kind.
  • Each row is unique.
  • The sequence of columns is insignificant.
  • The sequence of rows is insignificant.
  • Each column must have a unique name.

What is RDBMS? | SQL Server Interview Question

Relational Data Base Management Systems (RDBMS) are database management systems that maintain data records and indices in tables. Relationships may be created and maintained across and among the data and tables. In a relational database, relationships between data items are expressed by means of tables. Interdependencies among these tables are expressed by data values rather than by pointers. This allows a high degree of data independence. An RDBMS has the capability to recombine the data items from different files, providing powerful tools for data usage