Search This Blog

Thursday, September 29, 2011

Find 2nd Monday Of The Month

Hi,

Recently one of our fellow DBA friend asked a question on MSDN. Question was, he want to have a job with 2 steps, First step will run everyday and pass on the information but second step should only get executed on 2nd Monday of the Month.

Now how to do that, ofcourse he has to put some conditions to check whether its 2nd Monday of the Month or not and then process code accordingly. So question is how to find 2nd Monday of the Month. Here is the code friends.

DECLARE @DAY VARCHAR(10)


DECLARE @DATE VARCHAR(10)

DECLARE @TODAY VARCHAR(10)


SET @DAY='Monday'

SET @DATE=CONVERT(varchar(10),DATEADD(wk, DATEDIFF(wk,0, dateadd(dd,14-datepart(day,getdate()),getdate())), 0),111)
SET @TODAY=CONVERT(VARCHAR(10),getdate(),111)
IF @DAY= DATENAME(WEEKDAY,DATEADD(wk, DATEDIFF(wk,0, dateadd(dd,14-datepart(day,getdate()),getdate())), 0))

AND @TODAY=@DATE

BEGIN

SELECT 'Its Second '+@DAY+' Of The Month And Date Is '+@TODAY

END

ELSE

SELECT 'Its '+@DAY+ ' Today and Date Is '+@TODAY
 
Hope this will help.

Friday, September 2, 2011

Update Statistics For Specific Schema

Hi All,

One of my fellow dba asked a question.

I have a database having multiple schema's in it. I want to update statistics for some specific schema's only?

Now how to do that. Again SP_MSFOREACHTABLE (ofcourse non documented) but the savior here.

--This will print table names first for specific schema.
Exec sp_MSforeachtable @command1 = " print '?' ", @whereand = " and uid = (SELECT schema_id FROM sys.schemas WHERE name = 'dbo') "
--This will update statistics for specific schema.
Exec sp_MSforeachtable @command1 = " Update Statistics ? ", @whereand = " and uid = (SELECT schema_id FROM sys.schemas WHERE name = 'sales') "


So using above command we can do this. If we have multiple schema's simply change Where condition and make it WHERE NAME in ('a','b','c')