Translate

Monday, February 25, 2013

Ignore Duplicates in Oracle

When I wrote post about Ignore duplicates in SQL Server, I got the following comment from one of the LinkedIN group.

“

Also, Oracle has a great way to handle this in 10g and beyond.
By James Williams
@LinkedIN group SQL Server DBA

“

I thought looking into this with my very limited knowledge in Oracle.

I used Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 version.

First let me create the table with the unique constraints.

image

Let me insert three rows to the table.

image

Now the question is how oracle is managing duplicate inserts for unique constraints.

image

The above statement create an error logging table using the DBMS_ERRLOG package, and one of the inserts violates the check constraint on CITY, and that row can be seen in city_errors. If more than ten errors had occurred, the statement would have aborted, rolling back any insertions made:

Let’s say there is a another table of data (tempCity) and we want to populate CITY with tempCity.

image

If you simply insert tempCity into CITY entire transaction will be failed since Denver city already exists.

However, you can insert using following syntax.

image

So you can see only two rows will be inserted while other duplicate row is ignored.

by analyzing city_errors table you will be able to get which records were failed with the reason.

image

Saturday, February 23, 2013

Ignore Duplicates

Today I was asked about situation where entire data insertion is failing due to violation of Unique Index. In this scenario, actually they need to ignore the duplicate data and continue with the other inserts.

  1:  CREATE TABLE Name (ID INT IDENTITY PRIMARY KEY CLUSTERED,
  2:  FirstName VARCHAR(100),
  3:  LastName VARCHAR(100))
  4:  GO
  5: 
  6:  CREATE UNIQUE NONCLUSTERED INDEX [IX_Name_FirstName_LastName]
  7:  ON [dbo].Name
  8:  (
  9:  [FirstName] ASC,
 10:  [LastName] ASC
 11:  ) ON [PRIMARY]
 12:  GO

So above script will create a table and unique index on that table.


Lets insert two rows for this table.

  1:  INSERT INTO Name
  2:  VALUES
  3:  ('John','Samuels'),
  4:  ('Steve','John')


Since the above two records does not violate the unique key constraints you will not find any difficulties with the above query and you will have following records in the table.


image


Then we will insert this set of rows. Highlighted row is the row which will violate the unique constraint. 

  1:   INSERT INTO Name
  2:  VALUES
  3:   ('Steve','Smith'),
  4:  ('John','Samuels'),
  5:  ('Steve','Rodes'),
  6:  ('Phill','Samuels')

As expected this will fail with the following error


Msg 2601, Level 14, State 1, Line 1
Cannot insert duplicate key row in object 'dbo.Name' with unique index 'IX_Name_FirstName_LastName'.
The duplicate key value is (John, Samuels).
The statement has been terminated.


If you query the table you will see that not only the duplicate row but other rows are not in the table.


So the solution is to use IGNORE DUPLICATE option.This is how you create the constraints. Option is highlighted. By default this option is OFF.

  1:  CREATE UNIQUE NONCLUSTERED INDEX [IX_Name_FirstName_LastName]
  2:  ON [dbo].Name
  3:  (
  4:  [FirstName] ASC,
  5:  [LastName] ASC
  6:  ) WITH (IGNORE_DUP_KEY = ON) ON  [PRIMARY]
  7:  GO

When you inserts same data set again, no errors will be generated but this message.


Duplicate key was ignored.


(3 row(s) affected)


So, duplicate record was ignored and no errors were generated. 

Saturday, February 16, 2013

Temp Tables Vs Table Variables

I have posted couple of blog post on the above topic which are very popular.

Those blog posts were,

Temp Tables Vs Table Variables Vs CTE

What's the difference between a temp table and table variable in SQL Server-

Today I came across with nice picture which explains every thing from http://sqlserverplanet.com

image

Isn’t this very simple and easy to understand?

Sunday, February 10, 2013

Is SET ROWCOUNT a Depreciated Feature?

I have read in some blog post which are saying the SET ROWCOUNT feature is a depreciated feature in SQL Server 2012.

This is half truth.

SET ROWCOUNT for INSERT, UPDATE, and DELETE statements will be depreciated but not for SELECT. 

If you want to us INSERT / UPDATE / DELETE with SET ROWCOUNT, you need to use TOP key word.

ROWCOUNT & TOP

`Just came across with this scenario. What if you are using both ROWCOUNT & TOP together in one T-SQL statement.

  1: DECLARE @rowcnt int = 100
  2: SET ROWCOUNT @rowcnt
  3: 
  4: SELECT Top 105 * FROM Sales.SalesOrderHeader

 


This will return 100 rows and easily you can come to a conclusion that ROWCOUNT will override TOP. Well, let us examine another scenario.

  1: 
  2: DECLARE @rowcnt int = 100
  3: SET ROWCOUNT @rowcnt
  4: 
  5: SELECT Top 5 * FROM Sales.SalesOrderHeader
  6: 

This will return only 5!!.


So the rule is,


SET ROWCOUNT overrides the SELECT statement TOP keyword if the ROWCOUNT is the smaller value.

Saturday, February 2, 2013

SQL Server Disaster Recovery poster

You need to be registered with SQL Server Magazine to download the poster written by Paul Randel as a free PDF and you’ll also be sent one in the mail. The poster has links to a page with more explanations on our website.

Check it out at http://www.sqlmag.com/whitepaper/sql-server/sql-server-disaster-recovery-step-step-145134

Something to note,

image

Monday, January 28, 2013

Copy only Backups Differential Backups

Copy only backup feature was introduced in SQL Server 2005 to support ad-hoc backups. However, this feature is valid for Full backups and Log backups to not to disturb backup chains. Since ad-hoc Differential Backups do not disturb the backup chains, you don’t need Copy only backups for Differential Backups.

However, in SQL Server 2008, still you have the option of taking a Copy only differential backups as shown in below image.

image

In SQL Server 2012, this is changed as you are not allows to take ad-hoc differential backups as that option is grayed out.

image

SSSLUG January 2013

Slide decks, sample code and photos from the January 2013 #SSSLUG meet-up are now ready for downloading and viewing.
Downloads: http://www.sqlserveruniverse.com/SSSLUG/Downloads/2013.aspx
Gallery: http://www.sqlserveruniverse.com/SSSLUG/Gallery/January2013.aspx

Saturday, January 26, 2013

What are the key qualities for a SQL Server DBA?

  • Productive Under Pressure
  • Problem Solver
  • Understand Both the Business Needs and Technology Landscape
  • Willingness to Learn and Open Minded
  • Experience to Know When to be Firm and When to be Flexible
  • Trust, Honesty and Integrity
  • Communication

Read all in detail