Wednesday, July 23, 2008

Good link - Script Repository

http://www.microsoft.com/technet/scriptcenter/scripts/sql/default.mspx?mfr=true

Tuesday, July 22, 2008

Cursor to fetch all Procedure text in Particular DB

DECLARE @name sysname, @sqlcmd NVARCHAR(400)
DECLARE mycur CURSOR for
SELECT name FROM sys.sysobjects WHERE type ='p'
OPEN mycur
FETCH NEXT FROM mycur INTO @Name
WHILE @@Fetch_Status = 0
BEGIN
SELECT @sqlcmd = N'exec sp_helptext @name'
EXECUTE sp_executesql @sqlcmd, N'@name sysname', @name
FETCH NEXT FROM mycur INTO @name
END
CLOSE mycur
DEALLOCATE mycur

SQL Cmd

SQL command syntax to generate script for store proc. c:\sql.sql is the i/p file.

sqlcmd -E -i c:\sql.sql -o c:\proc.sql

SQL Server 2005 Bug ?????????????????

Hi all ,

Found one Interesting & weird behavior in SQL Server 2005 . This might be a bug in SQL server 2005 . Thought of sharing with you please have a look at it.

Database Generation Wizard:

Right click on the database -> Tasks ->Generate Scripts -> continue clicking Next (select the DB and things…).

Stop here .. please read this msdn page http://msdn.microsoft.com/en-us/library/ms186472.aspx/ms186472.aspx

Include Descriptive Headers

When True, descriptive comments are added to the script separating the script into sections for each object. Default is False.

But according to Below diagram the default option is true ?????? This Question is to Microsoft . I had logged this Issue in Microsoft site. Waiting
For their reply, also this might have fixed in Latest service pack (Have to check this).











Because of this SQL Server behaves differently when it is true/false . Say if I Generate scripts 4 times to a file, SQL will create the same Store Proc 4 times in the file.
Change the option [Include Descriptive Headers ] to false continue taking the script for 4 times you will find the same proc 4 times in the scripts while if you set this option to true as shown aboveIt will overwrite the previous script without any duplication

DB Back-Up Script

Below is the script to back up the databases..

DECLARE @DBname VARCHAR(50) -- database name
DECLARE @Filepath VARCHAR(256) -- path for backup files
DECLARE @fileName VARCHAR(256) -- filename for backup
DECLARE @fileDate VARCHAR(20) -- used for file name
SET @Filepath = 'D:\Backup\'
SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112)
DECLARE db_cursor CURSOR FOR
SELECT name
FROM master.dbo.sysdatabases
WHERE name NOT IN ('Naveen','Ki','msdb','Chan')
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN
SET @fileName = @Filepath + @DBname + '_' + @fileDate + '.BAK'
BACKUP DATABASE @DBname TO DISK = @fileName
FETCH NEXT FROM db_cursor INTO @DBname
END
CLOSE db_cursor
DEALLOCATE db_cursor

Tuesday, July 8, 2008

Hi all

Hi Guys,
This is space for all those who Learn & Master Database .Lets share our ideas and knowledge.
Keep visiting this blog for more Database related Information (MS SQL,ORACLE)

Nav