Author Archive

Post

Favorite SQL Server Interview Questions

In Interview Questions,Microsoft SQL Server,SQL Server 2008 on August 18, 2009 by sqlapostle

Every interviewer has some favorite questions that he likes to ask in almost each and every interview. Here are some of mine, and a bit of advice for those who think these are easy, I would hold off on the answers till next week for you to try and take a guess at the answers (Without executing the commands on SQL Server, of course)

1. The Setup :

Employee

  • EmployeeId int
  • EmployeeName nvarchar(50)
  • Sex ( constraint M / F) char(1)
  • DepartmentId int

 

The requirement is to produce the following output in an optimized way

DepartmentId                          CountFemale                      CountMale                  EmployeeCount

 

2. The Setup :

USE PlayGround
GO
BEGIN TRAN
    Create Table Employee
    (
        EmployeeId int
        ,EmployeeName nvarchar(100)
    )
    exec xp_cmdshell ‘bcp Playground.dbo.Employee in c:\Employee.txt -E -c’
    TRUNCATE TABLE Employee
ROLLBACK TRAN
GO
SELECT * FROM Employee
GO

What is the expected result of the above script ?

3. A variation on the above :

USE PlayGround
GO
    Create Table Employee
    (
        EmployeeId int
        ,EmployeeName nvarchar(100)
    )
    Insert Into Employee Select 1, ‘TestEmployee’
GO

BEGIN TRAN
    TRUNCATE TABLE Employee
ROLLBACK TRAN
GO
SELECT * FROM Employee

4. Is it possible to insert into two tables having same structure using a single insert statement ?

Post

Ignore_Dup_key Index Option

In Microsoft SQL Server,SQL Server 2005,SQL Server 2008 on June 17, 2009 by sqlapostle

The Ignore_Dup_Key is an index option that lets you specify the error response to a duplicate key value insert into a column having a unique clustered or unique non clustered index.

If you have Ignore_Dup_Key option set to ON for a unique index, when presented with a duplicate key insert into the column, it will issue a warning message only and rows violating the constraint will fail.

Set it to OFF and you will get an error message along with the transaction being rolled back and aborted.

Lets see some code which explains this , coz 10 lines of code are worth 100 lines of documentation :) .

   1:  Use PlayGround
   2:  -- Use your personal test database name here , mine's called the Playground 
   3:  GO
   4:  -- Lets create the Test Table 
   5:  CREATE Table MyDupKeyTest
   6:  (
   7:      KeyCol int
   8:      ,AnotherCol varchar(20)
   9:  )
  10:  GO
  11:  -- Create a unique index with the Ignore_dup_key set to ON
  12:  CREATE UNIQUE INDEX ixuc_MyDupKeyTest_KeyCol ON MyDupKeyTest(KeyCol) WITH (IGNORE_DUP_KEY = ON)
  13:  GO
  14:  -- Insert our test row
  15:  INSERT INTO MyDupKeyTest SELECT '1','SqlApostle'
  16:      17:  --Now lets try inserting another row with the duplicate values
  18:  BEGIN TRY
  19:      INSERT INTO MyDupKeyTest SELECT '1','SqlApostle'
  20:  END TRY
  21:  BEGIN CATCH
  22:      PRINT 'IN CATCH BLOCK'
  23:  END CATCH
  24:  -- Didn't reach the Catch block , there is only an warning issued
  25:      26:  GO
  27:  -- Now lets turn the option to OFF 
  28:  ALTER INDEX ixuc_MyDupKeyTest_KeyCol ON MyDupKeyTest REBUILD WITH (IGNORE_DUP_KEY = OFF )
  29:  GO
  30:  BEGIN TRY
  31:      INSERT INTO MyDupKeyTest SELECT '1','SqlApostle'
  32:  END TRY
  33:  BEGIN CATCH
  34:      PRINT 'IN CATCH BLOCK'
  35:  END CATCH
  36:  -- Into the catch block , error has occured
  37:  GO
 
Further on this in later posts

Post

SQLKit.com goes live …

In Announcements,Site Related on February 19, 2009 by sqlapostle Tagged: , ,

Hello and Wecome to SQL Kit.

I had intended to get this site up with a good blogging engine since last 3 years, and the domain has seen the light of day in various platforms ( CommunityServer / BlogEngine.Net / SubText etc.). None of these platforms sufficed my needs.

In between setting up the site, and getting myself to blog again  ( previous blog ) , a lot of time (close to 4 years) has passed.

In the time I was away from the blogging world, Microsoft SQL Server 2005 became a thing of the past. SQL Server 2008 made its appearance and took over the cloak of the newest version in the market.

Recently meeting someone who has been blogging as he travels all over the world, training people on SQL Server set me thinking about the time I used to blog regularly.

So I decided to take the plunge back in and viola, found the perfect blogging solution from WordPress, which allowed me all the features of a good blogging engine, while keeping me away from the headaches of maintaining a hosting provider.

So, I am back once again. Let’s see what posts the next few days churn up. Making no promises right now, I guess that will determine the direction this blog will go.

Ciao for now …

Follow

Get every new post delivered to your Inbox.