Wednesday, December 27, 2006

Don't start the user defined stored procedure with "SP_"

As you might be knowing the system stored procs would be prefixed with "SP_". If we prefix "sp_" in our user-defined stored procedure it would bring down the performance because SQL Server always looks for a stored procedure beginning with "sp_" in the following order:

1) Master DB,
2) The stored procedure based on the fully qualified name provided,
3) The stored procedure using dbo as the owner, if one is not specified.

So, when you have the SP with the prefix "sp_" in the DB other than master, the Master DB is always checked first, and if the user-created SP has the same name as a system stored proc, the user-created stored procedure will never be executed.

For exampe: Lets say that by mistake you have named one of your user defined stored procedure as "sp_help" within our database!

Create proc sp_help
as
Select * from dbo.empdetails


Now when you try executing the stored procedure using the below script you would realize that it has actually called the sp_help system stored proc of Master DB and not our SP.

Exec sp_help

Hope this helps!!

Technorati tags: , , ,

Monday, December 25, 2006

sp_executesql( ) vs Execute() -- Dynamic Queries

There were few questions regarding "Passing table names as parameters to stored procedures" in Dotnetspider forums. I don't feel this to be a write way of coding. Still many persons are asking similar questions in the forums thought would write a post on "SP_EXECUTESQL()" Vs "EXECUTE()".

Sample SP to pass table name as parameter:

Create proc SampleSp_UsingDynamicQueries
@table sysname
As

Declare @strQuery nvarchar(4000)
Select @strQuery = 'select * from dbo.' + quotename(@table)

exec sp_executesql @strQuery -------- (A)
--exec (@strQuery) ---------------------- (B)
go

Test: Execute dbo.samplesp_usingdynamicqueries 'EmpDetails'

In the above stored procedure irrespective of whether we use the line which is marked as (A) or (B) it would give us the same result. So what's the difference between them?

One basic difference is while using (A) we need to declare the @strQuery as nvarchar or nchar.

(i) It would throw the below error if we declare @query as varchar data type.
Msg 214, Level 16, State 2, Procedure sp_executesql, Line 1
Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'.

(ii) It would throw the below error if we declare @query nvarchar(5000).
Msg 2717, Level 16, State 2, Procedure SampleSp_UsingDynamicQueries, Line 1
The size (5000) given to the parameter '@query' exceeds the maximum allowed (4000).


Hope the first point is well made!

Lets move to the next point, Exec statement is Unparameterised whereas sp_executeSql is Parameterised. What does it mean?

1. EXECUTE() :: If we write a query which takes a parameter lets say "EmpID". When we run the query with "EmpID" as 1 and 2 it would be creating two different cache entry (one each for value 1 and 2 respectively).

It means for Unparameterised queries the cached plan is reused only if we ask for the same id again. So the cached plan is not of any major use.

2. SP_EXECUTESQL() :: In the similar situation for "Parameterised" queries the cached plan would be created only once and would be reused 'n' number of times. Similar to that of a stored procedure. So this would have better performance.

Let me create a sample to illustrate this:

Create table dbo.EmpDetails
(
EmpID int,
EmpName varchar(30)
)
Go

Insert into dbo.EmpDetails values (1, 'Vadivel')
Insert into dbo.EmpDetails values (2, 'Sailakshmi')
Insert into dbo.EmpDetails values (3, 'Velias')
Go

Create table dbo.EmpTimeSheet
(
EmpID int,
Day varchar(10),
HrsPut float
)
Go

Insert into dbo.EmpTimeSheet values (1, 'Mon',7.5)
Insert into dbo.EmpTimeSheet values (1, 'Tue',2)
Insert into dbo.EmpTimeSheet values (1, 'Wed',8)
Insert into dbo.EmpTimeSheet values (2, 'Mon',9)
Insert into dbo.EmpTimeSheet values (2, 'Tue',8.3)
Insert into dbo.EmpTimeSheet values (2, 'Wed',11)
Go

Time to test it out:

DBCC Freeproccache
Go

Declare @strQuery nvarchar(1000)

Select @strQuery = 'Select E.EmpName, TS.Day, TS.HrsPut
from dbo.empdetails E, dbo.EmpTimeSheet TS
where E.EmpID = TS.EmpID and TS.EmpID = N''1'''
Exec (@strQuery)

Select @strQuery = 'Select E.EmpName, TS.Day, TS.HrsPut
from dbo.empdetails E, dbo.EmpTimeSheet TS
where E.EmpID = TS.EmpID and TS.EmpID = N''2'''
Exec (@strQuery)

Select @strQuery = 'Select E.EmpName, TS.Day, TS.HrsPut
from dbo.empdetails E, dbo.EmpTimeSheet TS
where E.EmpID = TS.EmpID and TS.EmpID = @EmpID'
Exec sp_executesql @strQuery, N'@EmpID int', 1
Exec sp_executesql @strQuery, N'@EmpID int', 2

After this lets have a look at the cached plan by executing the below query. The first two (Unparameterised) has a execution_count of 1, the last one (Parameterised) would have an execution_count of 2.

Select sqlTxt.text, qStats.execution_count from sys.dm_exec_query_stats qStats
Cross Apply (Select [text] from sys.dm_exec_sql_text(qStats.sql_handle)) as sqlTxt option (Recompile)

DBCC Freeproccache has been used to just flush out all already cached plan and make our life easier to see our queries plan alone :)

Extract from MSDN: Use DBCC FREEPROCCACHE to clear the procedure cache. Freeing the procedure cache causes, for example, an ad hoc SQL statement to be recompiled instead of reused from the cache.

Please note: Run this statement only in Development Server and not in Production box :)

If you want to test it in SQL Server 2000 box, then query the system table "syscacheobjects".

Select cacheobjtype, usecounts, sql from syscacheobjects

Hope this helps!

Related Article: The curse and blessings of Dynamic SQL

Technorati tags: ,

Friday, December 22, 2006

I am now a Microsoft Certified Technology Specialist!

Let me start by saying, I have never taken a Microsoft exam previously. I was having a "Microsoft Exams 100% Discount Coupon" which was valid till 31-Dec-2006 so thought of utilizing it.

I prepared well for 70-431 Microsoft SQL Server 2005 - Implementation and Maintenance paper and today morning at 10.45 AM I took up the online test @ NIIT Adyar. I am glad that I have cleared it with a good score of 982 (out of 1000). Now I am a "Microsoft Certified Technology Specialist" :)

There were 52 questions and out of which approximately 15 where simulation questions (that was where I spent most of my time). Though the score might look big, I need to accept that I answered 20 to 30% 0f questions without much of confidence. Hope the choices I made where accidently correct :)

I was bit tensed in the morning till the time I answered the first question. Reason being, couple of my friends know that I am going to take up the test today. I was afraid what would they think if I fail in this exam :) I am happy I have proved to myself that one can pass a MS exam even without dumps :)

Technorati tags: ,

Sunday, December 17, 2006

Banks : Do Not Disturb Me

As per RBI regulation I guess all banks should have a "Do Not Distrub Me" or "Do not call me" :) web page inorder to value customers privacy.

I get atleast couple of telemarketing calls a day from one bank or other. Its really irritating and i was looking for a way to avoid them totally. Only then i came to know about these "Don't call registeration pages" for various banks. Looooong Live RBI :) :)

1. ICICI Bank - As per the site itseems it would take 15 days for the number to be removed from the telemarketing list!

2. Citibank -- I didn't find the time frame in this site.

3. Deutsche bank -- As per the site itseems it would take 30 days for the number to be removed from the telemarketing list!

4. Standard Chartered Bank -- As per the site itseems it would take 30 days for the number to be removed from the telemarketing list!

5. HDFC Bank -- As per the site itseems it would take 45 days for the number to be removed from the telemarketing list!

6. ABN Amro Bank -- I didn't find the time frame in this site.

Hope this would be useful for many of you guys too :) By the way, if you guys find any such pages for Hutch /Airtel please let me know. Those guys are also really irritating :(

Wednesday, December 13, 2006

Rolling back a truncate operation!

First I suggest you to go through my earlier article on subject "Delete VS Truncate" here.

Truncate is also a logged operation, but in a different way. It logs the deallocation of the data pages in which the data exists.

Let us create a dummy table for understanding this example:

Create Table dbo.TruncateTblDemo (Sno int)
Go


Insert few records into it:

Insert into dbo.TruncateTblDemo values (1)
Insert into dbo.TruncateTblDemo values (2)
Insert into dbo.TruncateTblDemo values (3)
Go

You could see that the table has 3 records in it:

Select * from dbo.TruncateTblDemo

Execute a truncate statement:

Truncate Table dbo.TruncateTblDemo

After the above statement you can't retrieve the data back because it is an explicit transaction. Unless or until you have set Implicit_Transactions to off.

That is, Internally sql would have taken our above truncate statement as follow:

Begin Tran
Truncate Table dbo.TruncateTblDemo
Commit Tran

Select * from dbo.truncatetbldemo

Getting back data using Truncate!!

Let me insert the same records into that table for test purpose again.

Insert into dbo.TruncateTblDemo values (1)
Insert into dbo.TruncateTblDemo values (2)
Insert into dbo.TruncateTblDemo values (3)

Begin Tran
Truncate table dbo.truncatetbldemo
Rollback Tran

Now you could see those 3 records which were truncated:

Select * from dbo.truncatetbldemo

Clean up:

Drop table dbo.truncatetbldemo

Technorati tags: ,

Tuesday, December 12, 2006

Windows development chief: 'I would buy a Mac if I didn't work for Microsoft'

Check out this article in computerworld.com. May be after this atleast we need to take our company policies very seriously :) How many of you guys are deleting unwanted mails from your official mailbox as per your company policy?

Technorati tags:

Friday, December 08, 2006

Fun with SQL Server ...

Read this first:

1. This has no real time usage. So try this out only when you are free :)
2. This script was just done for fun and nothing more.
3. Before executing this script change your result display mode to 'Text' (Ctrl + T)
4. There are easier way of doing the same thing. Just to make it look complex I have done this way :)

Code snippet Starts here:

Set nocount on
Declare @TblLayout table([ID] int Identity, Canvas Char(75))

Insert into @TblLayout Select Replicate (Convert(Varchar, 0x7E), 75)
Insert into @TblLayout Select Replicate (Convert(Varchar, 0x7E), 75)
Insert into @TblLayout Select Replicate (Convert(Varchar, 0x7E), 75)
Insert into @TblLayout Select Replicate (Convert(Varchar, 0x7E), 75)
Insert into @TblLayout Select Replicate (Convert(Varchar, 0x7E), 75)
Insert into @TblLayout Select Replicate (Convert(Varchar, 0x7E), 75)
Insert into @TblLayout Select Replicate (Convert(Varchar, 0x7E), 75)
Insert into @TblLayout Select Replicate (Convert(Varchar, 0x7E), 75)
Insert into @TblLayout Select Replicate (Convert(Varchar, 0x7E), 75)
Insert into @TblLayout Select Replicate (Convert(Varchar, 0x7E), 75)
Insert into @TblLayout Select Replicate (Convert(Varchar, 0x7E), 75)
Insert into @TblLayout Select Replicate (Convert(Varchar, 0x7E), 75)
Insert into @TblLayout Select Replicate (Convert(Varchar, 0x7E), 75)
Insert into @TblLayout Select Replicate (Convert(Varchar, 0x7E), 75)
Insert into @TblLayout Select Replicate (Convert(Varchar, 0x7E), 75)
Insert into @TblLayout Select Replicate (Convert(Varchar, 0x7E), 75)
Insert into @TblLayout Select Replicate (Convert(Varchar, 0x7E), 75)
Insert into @TblLayout Select Replicate (Convert(Varchar, 0x7E), 75)
Insert into @TblLayout Select Replicate (Convert(Varchar, 0x7E), 75)
Insert into @TblLayout Select Replicate (Convert(Varchar, 0x7E), 75)
Insert into @TblLayout Select Replicate (Convert(Varchar, 0x7E), 75)
Insert into @TblLayout Select Replicate (Convert(Varchar, 0x7E), 75)
Insert into @TblLayout Select Replicate (Convert(Varchar, 0x7E), 75)
Insert into @TblLayout Select Replicate (Convert(Varchar, 0x7E), 75)
Insert into @TblLayout Select Replicate (Convert(Varchar, 0x7E), 75)
Insert into @TblLayout Select Replicate (Convert(Varchar, 0x7E), 75)
Insert into @TblLayout Select Replicate (Convert(Varchar, 0x7E), 75)
Insert into @TblLayout Select Replicate (Convert(Varchar, 0x7E), 75)

--- Vz employees House ...
Update @TblLayout Set Canvas = Stuff(Canvas, 42, 24 , Convert(Varchar, 0x7C5F5F5F5F5F5F5F5F5F5F5F5F5F5F7C5F5F5F5F5F7C))Where ID = 5


Update @TblLayout Set Canvas = Stuff(Canvas, 42, 24 , Convert(Varchar, 0x7C2020205B20205D20205B20205D207C20202020207C)) Where ID = 4

Update @TblLayout Set Canvas = Stuff(Canvas, 42, 24 , Convert(Varchar, 0x2F2F2F2F2F2F2F2F2F2F2F2F2F2F2F2F5C5C5C5C5C5C)) Where ID = 3

Update @TblLayout Set Canvas = Stuff(Canvas, 43, 23 , Convert(Varchar, 0x2F2F2F2F2F2F2F2F2F2F2F2F2F2F2F2F5C5C5C5C)) Where ID = 2

Update @TblLayout Set Canvas = Stuff(Canvas, 44, 21, Convert(Varchar, 0x5F5F5F5F5F5F5F5F5F5F5F5F5F5F5F5F5F5F)) Where ID = 1

-- Picture of a Guy ...
Update @TblLayout Set Canvas = Stuff(Canvas, 10,10, Convert(Varchar, 0x285F5F5F5B5F5F5F29)) Where ID = 27

Update @TblLayout Set Canvas = Stuff(Canvas, 11,8, Convert(Varchar, 0x7C5F5F7C5F5F7C)) Where ID = 26

Update @TblLayout Set Canvas = Stuff(Canvas, 11,8, Convert(Varchar, 0x7C20207C20207C)) Where ID = 25

Update @TblLayout Set Canvas = Stuff(Canvas, 11,8, Convert(Varchar, 0x7C20207C20207C)) Where ID = 24

Update @TblLayout Set Canvas = Stuff(Canvas, 10,9, Convert(Varchar, 0x5C7C5F5F5F5F2F202F)) Where ID = 23

Update @TblLayout Set Canvas = Stuff(Canvas, 9,11, Convert(Varchar, 0x5C207C20416553202F202F)) Where ID = 22

Update @TblLayout Set Canvas = Stuff(Canvas, 9,11, Convert(Varchar, 0x2F5F2F20415453205C5F5C)) Where ID = 21

Update @TblLayout Set Canvas = Stuff(Canvas, 10,9, Convert(Varchar, 0x2F202060606020205C)) Where ID = 20

Update @TblLayout Set Canvas = Stuff(Canvas, 11,7, Convert(Varchar, 0x5F5C5F755F2F5F)) Where ID = 19

Update @TblLayout Set Canvas = Stuff(Canvas, 11,7, Convert(Varchar, 0x5C20302030202F)) Where ID = 18

Update @TblLayout Set Canvas = Stuff(Canvas, 11,7, Convert(Varchar, 0x2F2F2F2F2F2F2F)) Where ID = 17

--Time to travel
Update @TblLayout Set Canvas = Stuff(Canvas, 11,3, Convert(Varchar, 0x282029)) Where ID = 6

Update @TblLayout Set Canvas = Stuff(Canvas, 10,3, Convert(Varchar, 0x282029)) Where ID = 7

Update @TblLayout Set Canvas = Stuff(Canvas, 7,27, Convert(Varchar, 0x5F5F2F202F5F5F5F5F5F20202020205F5F5F5F5F5F5F5F5F)) Where ID = 8

Update @TblLayout Set Canvas = Stuff(Canvas, 5,27, Convert(Varchar, 0x287C202020202020202020207C2020207C2020202020202020207C)) Where ID = 9

Update @TblLayout Set Canvas = Stuff(Canvas, 6,26, Convert(Varchar, 0x7C205B5D205B5D205B5D207C292D287C205B5D205B5D205B5D7C)) Where ID = 10

Update @TblLayout Set Canvas = Stuff(Canvas, 6,27, Convert(Varchar,
0x7C5F5F5F5F5F5F5F5F5F5F7C2020207C5F5F5F5F5F5F5F5F5F7C)) Where ID = 11

Update @TblLayout Set Canvas = Stuff(Canvas, 7,24, Convert(Varchar, 0x303030303030303030302020202020303030303030303030)) Where ID = 12

-- Art by ....
Update @TblLayout Set Canvas = Stuff(Canvas, 54,14, Convert(Varchar, 0x417274204279205661646976656C))Where ID = 27

Update @TblLayout Set Canvas = Stuff(Canvas, 32,20, Convert(Varchar, 0x5368616D656C6573732070726F6D6F74696F6E3A))Where ID = 28

Update @TblLayout Set Canvas = Stuff(Canvas, 52,23, Convert(Varchar, 0x7661646976656C2E626C6F6773706F742E636F6D203A29))Where ID = 28

Select Canvas [ ] FROM @TblLayout


Closeing Notes: I actually got this idea after going through Roji's post here

Technorati tags: ,