Friday, May 27, 2016

difference between Top And Top with ties in Sql server

In this article, I will explain the use of the TOP WITH TIES Clause in SQL Server. In SQL Server, TOP WITH TIES,additional rows will be included if their values match, or tie, the values of the last row. So let's take a look at a practical example of how to use the TOP WITH TIES Clause in SQL Server. The example is developed in SQL Server 2012 using the SQL Server Management Studio.

The WITH TIES can be used only with the following clause:
  • Select Statemnet
  • Order by clause is necessary for using this clause
  • PERCENT clause
Creating a table in SQL Server
Now we create a table named employee using:
Create table Employee
(
EmpID int,
EmpName varchar(30),
EmpSalary int
)

The following is the sample data for the employee Table:

Employee-table-in-Sql-Server.jpg

Top Clause

The SELECT TOP N query always returns exactly N records. The following example defines the TOP clause.

Example

SELECT [EmpID]
      ,[EmpName]
      ,[EmpSalary]
  FROM [master].[dbo].[Employee]
  Go
 Select   top(3)  [EmpName]  from [Employee]
order by [EmpName]

Output

Top-Clause-With-orderby-in-SQL-server.jpg

Problem

In the above example, we can encounter the situation in which use of the top clause returns exactly N records and drops any record(s) arbitrarily that has the same value as the last record in the result set. Suppose the employee table has EmpName Rahul and the table contains one more EmpName with the same name, but it will not be in the result since they are ignored by the TOP clause.

Select Query Using TOP WITH TIES Clause

To see those recrods add the WITH TIES clause:

SELECT [EmpID]
      ,[EmpName]
      ,[EmpSalary]
  FROM [master].[dbo].[Employee]
  Go
 Select  top(3) [EmpName] from [Employee]
order by [EmpName]
 Go
 Select   top(3) with ties [EmpName]  from [Employee]
order by [EmpName]

Output

Top-Clause-With-ties-clause-in-SQL-server.jpg


Recently, on this blog, I published an article on SQL SERVER – Interesting Observation – TOP 100 PERCENT and ORDER BY; this article was very well received because of the observation made in it. One of the comments suggested the workaround was to use clause WITH TIES along with TOP and ORDER BY. That is not the correct solution; however, but the same comment brings up the question regarding how WITH TIES clause actually works.

First of all, the clause WITH TIES can be used only with TOP and ORDER BY, both the clauses are required. Let us understand from one simple example how this clause actually works. Suppose we have 100 rows in the table and out of that 50 rows have same value in column which is used in ORDER BY; when you use TOP 10 rows, it will return you only 10 rows, but if you use TOP 10 WITH TIES, it will return you all the rows that have same value as that of the last record of top 10 — which means a total of 50 records.
refer below link for more understanding:-
http://blog.sqlauthority.com/2009/12/23/sql-server-order-by-clause-and-top-with-ties/

TOP WITH TIES in Sql server

The TOP clause is commonly used to get the top required rows from a result set. The beauty of this clause is that it can be used with the WITH TIES clause to retrieve all similar rows to a base result set.

According to BOL “WITH TIES specifies that additional rows be returned from the base result set with the same value in the ORDER BY columns appearing as the last of the TOP n (PERCENT) rows. TOP...WITH TIES can be specified only in SELECT statements, and only if an ORDER BY clause is specified. “

For example, from the following simple table, I need to get records which have a minimum purchase date value. In the first method, we will use the common IN clause.

--Create temporary table
CREATE TABLE #MyTable (Purchase_Date DATETIME, Amount INT)
--Insert few rows to hold
INSERT INTO #MyTable
SELECT '11/11/2011', 100 UNION ALL
SELECT '11/12/2011', 110 UNION ALL
SELECT '11/13/2011', 120 UNION ALL
SELECT '11/14/2011', 130 UNION ALL
SELECT '11/11/2011', 150
--Get all records which has minimum purchase date (i.e. 11/11/2011)
SELECT * FROM #MyTable
WHERE Purchase_Date IN

       (SELECT MIN(Purchase_Date) FROM #MyTable)
We can also get our desired results by using TOP…WITH TIES.
SELECT TOP(1) WITH TIES * FROM #MyTable
ORDER BY Purchase_Date

Performance

By executing the above query, you can find TOP WITH TIES worked amazingly, but is this short code really smart code? Let’s compare their performances.
Though the TOP…WITH TIES clause really shortened our code, you can see that it performed poorly as compared to our traditional code. This happened just because of the ORDER BY clause. This poor performance can be controlled by placing a well defined index.

Thursday, May 19, 2016

Tuesday, May 17, 2016

Inserting default values in database while inserting record in Entity Framework

You can set "StoredGeneratedProperty" attribute of table column in the EDMX file to Computed to enable default value insertion in entity framework.

The Problem

While working with Entity Framework, you would have noticed that it does not insert default value into the database. You need to manually set values for those properties which have already a default value set in database table. This seems as an extra work to set values for default columns. Suppose, you are saving created date of a row in your table and for this you are using default constraint as today's days. Now when you insert a new record in database, it works absolutely fine but if you perform this operation using entity framework, it will not let you doing so. To insert default value, you need to manually assign a value to that column.

The Solution

If you are using code first approach, you can set default values using [DefaultValue()]  attribute or if you are using database first and using edmx file then you can set StoreGeneratedPattern = "Computed" of that particular column. Just follow the steps below.
Step 1: Open EDMX file and locate your table in that file.
step 1
Step 2: Select the property name. Right click and click on property.
step 2
Step 3: You will find a property named as StoreGeneratedPattern with value set as None.
step 3
Step 4: Set StoreGeneratedPattern = "Computed".
step 4



What should be the default behavior of LINQ-to-Entities while setting the default value of a field in SQL’s field designer ?
Sometimes it’s difficult to come up with a topic that we feel inspired to write about. I am writing about the problem I encountered while working with entity datamodel. After discussing with the team members I feel comfortable writing about it. I also feel good that this article would then help others who might have encountered the same problem.
Lets assume we create a table named TechZeus_Logging_Details having four fieldsPK_ID (auto incremented primary key) , LAST_NAME(varchar) , FIRST_NAME(varchar) and LOG_TIME(datetime)..
CREATE TABLE [dbo].[TECHZEUS_LOGGING_DETAILS] (
[PK_ID] [int] IDENTITY(1,1) NOT NULL,
[LAST_NAME] [varchar](50) NULL,
[FIRST_NAME] [varchar](50) NULL,
[LOG_TIME] [datetime] NULL )

Then we set the default value of a datetime field by getdate() bydefault to set the current time for newly inserted row
ALTER TABLE [dbo].[TECHZEUS_LOGGING_DETAILS] ADD CONSTRAINT [DF_TECHZEUS_LOGGING_DETAILS] DEFAULT (getdate()) FOR [LOG_TIME]

Now while inserting values into this table using direct SQL query , we can just omit the default field because that will be automatically taken care of as the default value is set in SQL server so that we dont need to set it explicitly.

INSERT INTO [dbo].[TECHZEUS_LOGGING_DETAILS](
LAST_NAME,FIRST_NAME)
VALUES(
‘ABC’, ‘XYZ’)

Here the matter of concern is that what we should do while using entity datamodel for such database operations?
The general misconception is to leave the default fields as such while saving changes using context object.
Something like this:

using (var contextObject = new DatabaseEntities())

{

  try {

 
         TECHZEUS_LOGGING_DETAILS objTZLD = new TECHZEUS_LOGGING_DETAILS(); //Create object of the table to insert data

         objTZLD.LAST_NAME = "XYZ"; // Assign value to LAST_FIELD field

         objTZLD.FIRST_NAME = "ABC"; // Assign value to FIRST_NAME field

 
         contextObject.AddToTECHZEUS_LOGGING_DETAILS(objTZLD); //Add object to insert data into table

         contextObject.SaveChanges(); //Commit inserted data or changed data.

       }

  catch(ex)

       {

         //Log exception into Exception Log table.

        }

}

This approach creates problem because Properties connected to [LOG_TIME] column which is set as default in SQL’s field designer will not auto-populate.after the SaveChanges action is performed on the Entity Context, rather NULL or default value(C# default value) will be written to the [LOG_TIME](datetime) field instead of setting the database level default value!!


So, to overcome this problem what should we do ? Well the answer is, we need to follow these 2 steps:

  1. Set the default value in SQL Server as usual as we do.
  2. After that Set ‘StoredGeneratedPattern‘ attribute to ‘Computed‘ against the default field in the .edmx file.We still need the default value in SQL Server though, the above setting will ensure Entity Framework’s expected behaviour.

Now comes the question what is StoredGeneratedPattern in entitymodel?

Basically it represents an enumeration that specifies three options indicating whether the column in the store schema definition language (SSDL) (.ssdl) needs a value during insert and update operations and it tells us what should be done if we insert or update rows. We can set it as None, Identity or Computed.
i) None: No auto generated value is generated
ii)Identity: A new value is generated on insert, but not changed on update
iii)Computed: A new value is generated on insert and update.

Note: There is always an another method to achieve the same behavior. Here also we have one more option i.e. we can manually set required value in the code itself and don’t rely on the SQL or Entity default settings. That will also ensure that we are not inserting nulls or any default values in tables but my point is why to avoid such provided properties instead of doing everything in our code. At the end of the day these are individual choices. So always take care of such small points which can lead to big issues later on.

Tuesday, May 10, 2016

The maximum recursion 100 has been exhausted before statement completion. in sqlserver

Error:-
WITH DateTable
AS
(
    SELECT CAST('2014-01-30 17:43:00.000' as Date) AS [DATE]
    UNION ALL
    SELECT DATEADD(dd, 1, [DATE]) FROM DateTable
    WHERE DATEADD(dd, 1, [DATE]) <= cast('2016-05-10 05:47:03.000' as Date)
)
SELECT  dt.[DATE] FROM [DateTable] dt

Msg 530, Level 16, State 1, Line 1
The statement terminated. The maximum recursion 100 has been exhausted before statement completion.

Solution:-
WITH DateTable
AS
(
    SELECT CAST('2014-01-30 17:43:00.000' as Date) AS [DATE]
    UNION ALL
    SELECT DATEADD(dd, 1, [DATE]) FROM DateTable
    WHERE DATEADD(dd, 1, [DATE]) <= cast('2016-05-10 05:47:03.000' as Date)
)
SELECT  dt.[DATE] FROM [DateTable] dt
option (maxrecursion 0);

Monday, May 9, 2016

Sql server interview questions

To rename db
sp_renamedb 'old_database_name' , 'new_database_name'

If someone is using db it will not accept sp_renmaedb. In that case first bring db to single user using sp_dboptions. Use sp_renamedb to rename database. Use sp_dboptions to bring database to multi user mode.
E.g.

USE master;
EXEC sp_dboption AdventureWorks, 'Single User', True
EXEC sp_renamedb 'AdventureWorks', 'AdventureWorks_New'


EXEC sp_dboption AdventureWorks, 'Single User', False

To rename Table
We can change the table name using sp_rename as follows, 
sp_rename 'old_TableName' 'new_TableName'
E.g.
sp_RENAME 'Table_First', 'Table_Last'

To rename Column
The script for renaming any column :
sp_rename 'TableName.[Old_columnName]', 'New_ColumnName', 'Column'
E.g.
sp_RENAME 'Table_First.Name', 'NameChange' , 'COLUMN'

What command do we use to rename a db? 
sp_renamedb ‘oldname’ , ‘newname’
If someone is using db it will not accept sp_renmaedb. In that case first bring db to single user using sp_dboptions. Use sp_renamedb to rename database. Use sp_dboptions to bring database to multi user mode.
What is sp_configure commands and set commands?
Use sp_configure to display or change server-level settings. To change database-level settings, use ALTER DATABASE. To change settings that affect only the current user session, use the SET statement.
What are the different types of replication? Explain.
The SQL Server 2000-supported replication types are as follows:
  • Transactional
  • Snapshot
  • Merge
Snapshot replication distributes data exactly as it appears at a specific moment in time and does not monitor for updates to the data. Snapshot replication is best used as a method for replicating data that changes infrequently or where the most up-to-date values (low latency) are not a requirement. When synchronization occurs, the entire snapshot is generated and sent to Subscribers.
Transactional replication, an initial snapshot of data is applied at Subscribers, and then when data modifications are made at the Publisher, the individual transactions are captured and propagated to Subscribers.
Merge replication is the process of distributing data from Publisher to Subscribers, allowing the Publisher and Subscribers to make updates while connected or disconnected, and then merging the updates between sites when they are connected.
What are the OS services that the SQL Server installation adds?
MS SQL SERVER SERVICE, SQL AGENT SERVICE, DTC (Distribution transac co-ordinator)
What are three SQL keywords used to change or set someone’s permissions?GRANT, DENY, and REVOKE.
What does it mean to have quoted_identifier on? What are the implications of having it off?
When SET QUOTED_IDENTIFIER is ON, identifiers can be delimited by double quotation marks, and literals must be delimited by single quotation marks. When SET QUOTED_IDENTIFIER is OFF, identifiers cannot be quoted and must follow all Transact-SQL rules for identifiers.
What is the STUFF function and how does it differ from the REPLACE function? 
STUFF function to overwrite existing characters. Using this syntax, STUFF(string_expression, start, length, replacement_characters), string_expression is the string that will have characters substituted, start is the starting position, length is the number of characters in the string that are substituted, and replacement_characters are the new characters interjected into the string.
REPLACE function to replace existing characters of all occurance. Using this syntax REPLACE(string_expression, search_string, replacement_string), where every incidence of search_string found in the string_expression will be replaced with replacement_string.
Using query analyzer, name 3 ways to get an accurate count of the number of records in a table?
SELECT *FROM table1SELECT COUNT(*)FROM table1SELECT rowsFROM sysindexesWHERE id OBJECT_ID(table1)
AND 
indid 2

How to rebuild Master Database?
Shutdown Microsoft SQL Server 2000, and then run Rebuildm.exe. This is located in the Program Files\Microsoft SQL Server\80\Tools\Binn directory.
In the Rebuild Master dialog box, click Browse.
In the Browse for Folder dialog box, select the \Data folder on the SQL Server 2000 compact disc or in the shared network directory from which SQL Server 2000 was installed, and then click OK.
Click Settings. In the Collation Settings dialog box, verify or change settings used for the master database and all other databases.
Initially, the default collation settings are shown, but these may not match the collation selected during setup. You can select the same settings used during setup or select new collation settings. When done, click OK.
In the Rebuild Master dialog box, click Rebuild to start the process.
The Rebuild Master utility reinstalls the master database.
To continue, you may need to stop a server that is running.
Source: http://msdn2.microsoft.com/en-us/library/aa197950(SQL.80).aspx
What is the basic functions for master, msdb, model, tempdb databases?
The Master database holds information for all databases located on the SQL Server instance and is the glue that holds the engine together. Because SQL Server cannot start without a functioning master database, you must administer this database with care.
The msdb database stores information regarding database backups, SQL Agent information, DTS packages, SQL Server jobs, and some replication information such as for log shipping.
The tempdb holds temporary objects such as global and local temporary tables and stored procedures.
The model is essentially a template database used in the creation of any new user database created in the instance.
What are primary keys and foreign keys? 
Primary keys are the unique identifiers for each row. They must contain unique values and cannot be null. Due to their importance in relational databases, Primary keys are the most fundamental of all keys and constraints. A table can have only one Primary key.
Foreign keys are both a method of ensuring data integrity and a manifestation of the relationship between tables.
What is data integrity? Explain constraints?
Data integrity is an important feature in SQL Server. When used properly, it ensures that data is accurate, correct, and valid. It also acts as a trap for otherwise undetectable bugs within applications.
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.
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.
FOREIGN KEY constraint prevents any actions that would destroy links between tables with the corresponding data values. A foreign key in one table points to a primary key in another table. Foreign keys prevent actions that would leave rows with foreign key values when there are no primary keys with that value. The foreign key constraints are used to enforce referential integrity.
CHECK constraint is used to limit the values that can be placed in a column. The check constraints are used to enforce domain integrity.
NOT NULL constraint enforces that the column will not accept null values. The not null constraints are used to enforce domain integrity, as the check constraints.

What is sp_configure commands and set commands?


Use sp_configure to display or change server-level settings. To change database-level settings, use ALTER DATABASE. To change settings that affect only the current user session, use the SET statement.

@@SPID (Transact-SQL)

Returns the session ID of the current user process.
@@SPID

-- Azure SQL Data Warehouse and Parallel Data Warehouse
@@SPID
This example returns the session ID, login name, and user name for the current user process.

SELECT @@SPID AS 'ID', SYSTEM_USER AS 'Login Name', USER AS 'User Name';

ID     Login Name                     User Name                     
------ ------------------------------ ------------------------------
54     SEATTLE\joanna                 dbo    

Examples: Azure SQL Data Warehouse Public Preview and Parallel Data Warehouse

This example returns the SQL Data Warehouse session ID, the SQL Server Control node session ID, login name, and user name for the current user process
SELECT SESSION_ID() AS ID, @@SPID AS 'Control ID', SYSTEM_USER AS 'Login Name', USER AS 'User Name'

SQL SERVER – Index Seek Vs. Index Scan (Table Scan)

Index Scan retrieves all the rows from the table. Index Seek retrieves selective rows from the table.
Index Scan: Since a scan touches every row in the table whether or not it qualifies, the cost is proportional to the total number of rows in the table. Thus, a scan is an efficient strategy if the table is small or if most of the rows qualify for the predicate.
Index Seek: Since a seek only touches rows that qualify and pages that contain these qualifying rows, the cost is proportional to the number of qualifying rows and pages rather than to the total number of rows in the table.
Index Scan is nothing but scanning on the data pages from the first page to the last page. If there is an index on a table, and if the query is touching a larger amount of data, which means the query is retrieving more than 50 percent or 90 percent of the data, and then optimizer would just scan all the data pages to retrieve the data rows. If there is no index, then you might see a Table Scan (Index Scan) in the execution plan.
Index seeks are generally preferred for the highly selective queries. What that means is that the query is just requesting a fewer number of rows or just retrieving the other 10 (some documents says 15 percent) of the rows of the table.
In general query optimizer tries to use an Index Seek which means that optimizer has found a useful index to retrieve recordset. But if it is not able to do so either because there is no index or no useful indexes on the table then SQL Server has to scan all the records that satisfy query condition.
What is the Maximum Size per Database for SQL Server Express?
Maximum database size of 10 GB per database in SQL Server 2014, SQL Server 2012, and 2008 R2 Express (4 GB for SQL Server 2008 Express and earlier; compared to 2 GB in the former MSDE).

What is Aggregate Functions?

Aggregate functions perform a calculation on a set of values and return a single value. Aggregate functions ignore NULL values except COUNT function. HAVING clause is used, along with GROUP BY for filtering query using aggregate values.
The following functions are aggregate functions.
AVG, MIN, CHECKSUM_AGG, SUM, COUNT, STDEV, COUNT_BIG, STDEVP, GROUPING, VAR, MAX, VARP (Read more here )

What is Use of @@ SPID in SQL Server?

A SPID is the returns sessions ID of the current user process. And using that session ID, we can find out that the last query was executed. (Read more here)

What is the Difference between Index Seek vs. Index Scan?

An index scan means that SQL Server reads all the rows in a table, and then returns only those rows that satisfy the search criteria. When an index scan is performed, all the rows in the leaf level of the index are scanned. This essentially means that all the rows of the index are examined instead of the table directly. This is sometimes compared to a table scan, in which all the table data is read directly. However, there is usually little difference between an index scan and a table scan.
An index seek, on the other hand, means that the Query Optimizer relies entirely on the index leaf data to locate rows satisfying the query condition. An index seek will be most beneficial in cases where a small percentage of rows will be returned. An index seek will only affect the rows that satisfy a query condition and the pages that contain these qualifying rows; in terms of performance, this is highly beneficial when a table has a very large number of rows. (Read more here)

What is the Maximum Size per Database for SQL Server Express?

SQL Server Express supports a maximum size of 4 GB per database, which excludes all the log files. 4 GB is not a very large size; however, if the database is properly designed and the tables are properly arranged in a separate database, this limitation can be resolved to a certain extent.

How do We Know if Any Query is Retrieving a Large Amount of Data or very little data?

In one way, it is quite easy to figure this out by just looking at the result set; however, this method cannot be relied upon every time as it is difficult to reach a conclusion when there are many columns and many rows.
It is easy to measure how much data is retrieved from server to client side. The SQL Server Management Studio has feature that can measure client statistics. (Read more here)

What is the Difference between GRANT and WITH GRANT while Giving Permissions to the User?

In case of only GRANT, the username cannot grant the same permission to other users. On the other hand, with the option WITH GRANT, the username will be able to give the permission after receiving requests from other users. (Read more here)

How to Create Primary Key with Specific Name while Creating a Table?

CREATE TABLE [dbo].[TestTable]([ID] [int] IDENTITY(1,1)NOTNULL,[FirstName] [varchar](100)NULL,CONSTRAINT [PK_TestTable] PRIMARYKEYCLUSTERED([ID] ASC))GO

What is T-SQL Script to Take Database Offline – Take Database Online?

-- Take the Database OfflineALTER DATABASE [myDB] SET OFFLINE WITH ROLLBACK IMMEDIATEGO-- Take the Database OnlineALTER DATABASE [myDB] SET ONLINE GO

How to Enable/Disable Indexes?

--Disable IndexALTER INDEX [IndexName] ON TableName DISABLE GO--Enable IndexALTER INDEX [IndexName] ON TableName REBUILD GO

Can we Insert Data if Clustered Index is Disabled?

No, we cannot insert data if Clustered Index is disabled because Clustered Indexes are in fact original tables which are physically ordered according to one or more keys (Columns). (Read more here)

How to Recompile Stored Procedure at Run Time?

We can Recompile Stored Procedure in two ways.
Option 1:
CREATE PROCEDURE dbo.PersonAge(@MinAge INT@MaxAge INT)WITH RECOMPILEAS SELECT*FROM dbo.tblPersonWHERE Age <= @MinAge AND Age >= @MaxAgeGO
Option 2:
EXEC dbo.PersonAge6570 WITHRECOMPILE
We can use RECOMPILE hint with a query and recompile only that particular query. However, if the parameters are used in many statements in the stored procedure and we want to recompile all the statements, then instead of using the RECOMPILE option with all the queries, we have one better option that uses WITH RECOMPILE during stored procedure creation or execution.
This method is not recommended for large stored procedures because the recompilation of so many statements may outweigh the benefit of a better execution plan. (Read more here)

Is there any Performance Difference between IF EXISTS (Select null from table) and IF EXISTS (Select 1 from table)?

There is no performance difference between IF EXISTS (Select null from table) and IF EXISTS (Select 1 from table).(Read more here)

What is Difference in Performance between INSERT TOP (N) INTO Table and Using Top with INSERT?

INSERT TOP (N) INTO Table is faster than Using Top with INSERT but when we use INSERT TOP (N) INTO Table, the ORDER BY clause is totally ignored. (Read more here)

Does the Order of Columns in UPDATE statements Matter?

No, the order of columns in UPDATE statement does not matter for results updated.
Both the below options produce the same results.
Option 1:
UPDATE TableNameSET Col1 ='Value'Col2 ='Value2'
Option 2:
UPDATE TableNameSET Col2 ='Value2'Col1 ='Value'

How do We Know if Any Query is Retrieving a Large Amount of Data or very little data?

Client Statistics is very important. Many a time, people relate queries execution plan with query cost. This is not a good comparison. Both are different parameters, and they are not always related. It is possible that the query cost of any statement is less, but the amount of the data returned is considerably large, which is causing any query to run slow. How do we know if any query is retrieving a large amount data or very little data? In one way, it is quite easy to figure this out by just looking at the result set; however, this method cannot be relied upon all the time as it is difficult to reach a conclusion when there are many columns and many rows.
To measure how much data is retrieved from server to client side is very simple. SQL Server Management Studio has feature that can measure client statistics. There are three different ways to enable client statistics.
Method 1
Press SHIFT+ALT+S together.
Method 2
Go to Menu >> Query >> Include Client Statistics
SQL SERVER - SQL Server Management Studio and Client Statistics clientstats1
Method 3
SQL SERVER - SQL Server Management Studio and Client Statistics clientstats2
Once you enable the client statistics, it will display another tab in the result span; when clicked, we obtain the following:
SQL SERVER - SQL Server Management Studio and Client Statistics clientstats3
Method 1:

INSERT INTO TABLE … SELECT TOP (N) Cols… FROM Table1
Method 2:
INSERT TOP(N) INTO TABLE … SELECT Cols… FROM Table1
Today we will go over the second method which in fact is the enhancement in TOP along with INSERT. It is very interesting to also observe difference between both the methods. Let us play with one real example and we understand what exactly is happening in either of the case.
USE tempdb GO-- Create TableIF EXISTS (SELECT FROM sys.objects WHERE OBJECT_ID OBJECT_ID(N'TestValue') AND type IN (N'U'))DROP TABLE TestValue GOCREATE TABLE TestValue(ID INT)INSERT INTO TestValue (ID)SELECT 1UNION ALLSELECT 2UNION ALLSELECT 3UNION ALLSELECT 4UNION ALLSELECT 5 GO-- Select Data from TableSELECT *FROM TestValue GO-- Create Two Table where Data will be InsertedIF EXISTS (SELECT FROM sys.objects WHERE OBJECT_ID OBJECT_ID(N'InsertTestValue') AND type IN (N'U'))DROP TABLE InsertTestValue GOCREATE TABLE InsertTestValue (ID INT)IF EXISTS (SELECT FROM sys.objects WHERE OBJECT_ID OBJECT_ID(N'InsertTestValue1') AND type IN (N'U'))DROP TABLE InsertTestValue1 GOCREATE TABLE InsertTestValue1 (ID INT)GO-- Option 1: Top with SelectINSERT INTO InsertTestValue (ID)SELECT TOP (2IDFROM TestValueORDER BY ID DESCGO-- Option 2: Top with InsertINSERT TOP (2INTO InsertTestValue1 (ID)SELECT IDFROM TestValueORDER BY ID DESCGO-- Check the DataSELECT *FROM InsertTestValue GOSELECT *FROM InsertTestValue1 GO-- Clean upDROP TABLE InsertTestValueDROP TABLE InsertTestValue1DROP TABLE TestValue GO
Now let us check the result of above SELECT statements.
SQL SERVER - INSERT TOP (N) INTO Table - Using Top with INSERT inserttop
It is very interesting to see when Option 2 is used, ORDER BY is absolutely ignored and data is inserted in any order.
In future articles, we will talk about performance for these queries. What are your thoughts on this feature? Have you used INSERT TOP(N) in your application?

SQL SERVER – SELECT 1 vs SELECT * – An Interesting Observation

Many times I have seen the issue of SELECT 1 vs SELECT * discussed in terms of performance or readability while checking for the existence of rows in the table. I ran quick 4 tests about this observed that I am getting same result when used SELECT 1 and SELECT *. I think smart readers of this blog will come up the situation when SELECT 1 and SELECT * have a different execution plan when used to find the existence of rows.
Let us see 4 tests I ran and note their result is same and their Execution Plan take the same amount of resources (50% and 50%)
You can click on the images to see larger images.
Test 1: Whole Table SELECT
1
2
3
4
5
6
7
8
9
10
11
12
USE AdventureWorks
GO
IF EXISTS(
SELECT 1
FROM Production.Product)
SELECT 'SELECT 1'
GO
IF EXISTS(
SELECT *
FROM Production.Product)
SELECT 'SELECT *'
GO
SQL SERVER - SELECT 1 vs SELECT * - An Interesting Observation select1s
Test 2: Condition WHERE on Indexed Column
1
2
3
4
5
6
7
8
9
10
11
12
13
14
USE AdventureWorks
GO
IF EXISTS(
SELECT 1
FROM Production.Product
WHERE Name = 'Adjustable Race')
SELECT 'SELECT 1'
GO
IF EXISTS(
SELECT *
FROM Production.Product
WHERE Name = 'Adjustable Race')
SELECT 'SELECT *'
GO
SQL SERVER - SELECT 1 vs SELECT * - An Interesting Observation select2s
Test 3: Using Aggregate function COUNT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
USE AdventureWorks
GO
IF (
SELECT 1
FROM Production.Product
WHERE Name = 'Adjustable Race') = 1
SELECT 'SELECT 1'
GO
IF (
SELECT COUNT(*)
FROM Production.Product
WHERE Name = 'Adjustable Race') = 1
SELECT 'SELECT *'
GO
SQL SERVER - SELECT 1 vs SELECT * - An Interesting Observation select3s
Test 4: Using COUNT with search on non-indexed Column

USE AdventureWorks
GO
IF (
SELECT COUNT(1)
FROM Production.Product
WHERE SafetyStockLevel = '800') &amp;gt; 1
SELECT 'SELECT 1'
GO
IF (
SELECT COUNT(*)
FROM Production.Product
WHERE SafetyStockLevel = '800') &amp;gt; 1
SELECT 'SELECT *'
GO
SQL SERVER - SELECT 1 vs SELECT * - An Interesting Observation select4s
Note: I have used single quotes around 800, which is numeric and I am aware of that fact. Above queries are for testing purpose only.
I have been using SELECT 1 instead of SELECT * when checking existence of rows. I would like to see what my readers have an opinion about this. Please have your opinion and make your comment here.