SQL Server

Set vs Select – Assigning variables

SET and SELECT both can be used to assign values to the local variables in SQL Server. However, SET is an ANSI standard and also recommended by Microsoft to be used for variable assignment. In this post, “Set vs Select – Assigning variables“, we will explore the behavior of SET and SELECT statement during variable assignment from a query. When we […]

Set vs Select – Assigning variables Read More »

Change stored procedure output column names and data types

Can we change the output column name and their data type being received from a stored procedure during execution? Yes, SQL Server 2012 introduced “WITH RESULT SETS” option which can be used with EXECUTE command to redefine the metadata of result sets being received from stored procedures. This option can handle single as well as multiple result sets.

Change stored procedure output column names and data types Read More »

The join order has been enforced because a local join hint is used – Warning in SQL Server

When we use a local JOIN hint in T-SQL query, sometimes, SQL Server throws a warning message as “Warning: The join order has been enforced because a local join hint is used.“. For example, below is a query with a local join hint: After execution, we will get below warning message with result set: In above code, we are using

The join order has been enforced because a local join hint is used – Warning in SQL Server Read More »

Dynamic PIVOT query in SQL Server

PIVOT clause is used to generate cross tab outputs in SQL Server. We put unique data values from a column in the PIVOT clause to render them as multiple columns in aggregation with other columns required in the output. With the help of PIVOT clause, we can transpose the distinct values of a column into multiple columns. However, syntax of PIVOT clause requires these distinct values to

Dynamic PIVOT query in SQL Server Read More »

Insert statement

In this chapter “Insert statement“, we are going to discuss T-SQL’s Insert statement which is used to add new records in a table. Whenever, we need to add a record in a table, INSERT T-SQL statement is used. We can insert single or multiple records at a time using INSERT statement. INSERT statement can be used in two ways as below:

Insert statement Read More »

Convert number into year, month, day, hour, minute and second

We have an integer number which represents seconds (secs) part of the time. We need to convert this integer number into year, month, days, hour, minute and second parts. Let me explain with the help of the examples to make it clear. Examples: 100 Seconds – 0 year(s) 0 month(s) 0 day(s) 0 hour(s) 1 minute(s) 40

Convert number into year, month, day, hour, minute and second Read More »

Query Performance Tuning – General Guidelines

Query performance tuning – Introduction Query performance tuning is a process of improving system performance to make it faster and more scalable. It is a creative and iterative process which has a clear and well-defined objective. To achieve this goal, we have to follow certain defined steps. Why we need this Enterprise data changes frequently over

Query Performance Tuning – General Guidelines Read More »

Order max and min value rows alternatively in SQL Server

In this blog post “Order max and min value rows alternatively in SQL Server”, we are going to learn how we can order the rows of a table by combining max and min value rows alternatively. For example, from an employee table based on the salary column, we need to extract the records ordered as; First row: First highest salary Second row: First lowest

Order max and min value rows alternatively in SQL Server Read More »

Add constraint without checking existing data

In this post “Add constraint without checking existing data” we are going to learn how we can add a constraint on a column which already has invalid data. We create constraints to automatically enforce the integrity of the database. Using constraints we can define the rules for valid set of values for a given column. Let’s learn

Add constraint without checking existing data Read More »

Index on computed column

Today, we are going to learn how we can create index on a computed column which improves the performance of SELECT queries. To create a computed column, we will use an user defined function instead of inline code. Also to know about computed columns, refer my previous blog post “What is Computed Column in sql server“. Create index

Index on computed column Read More »