Tuesday, November 18, 2008

SQL Script to shrink all user Databases

CREATE procedure dbo.proc_shrink_databases
as
begin
set nocount on
create table #temp_dbs_table
(

[db_name] sysname not null primary key,

[mod] tinyint not null default 1

)
insert into #temp_dbs_table ([db_name])
select name
from master..sysdatabases
where dbid > 4 ---skip master, tempdb, model and msdb databases
declare @db_name sysname
set @db_name = ''
while @db_name is not null
begin
set @db_name = NULL
select top 1 @db_name = [db_name] from #temp_dbs_table where [mod] = 1
if @db_name is NULL
break
print '> Database: ' + @db_name
print '> Changing recovery mode to simple'
declare @n_cmd nvarchar(4000)
set @n_cmd = 'alter database [' + @db_name + '] set recovery simple'
exec sp_executesql @n_cmd
print '> Shrinking database'
set @n_cmd = 'dbcc shrinkdatabase([' + @db_name + '])'
exec sp_executesql @n_cmd

print '> Changing recovery mode to Full'

set @n_cmd = 'alter database [' + @db_name + '] set recovery full'
exec sp_executesql @n_cmd
update #temp_dbs_table set [mod] = 0 where [db_name] = @db_name
end
drop table #temp_dbs_table
end

Tuesday, August 19, 2008

Find the backup details for a database

Following query can give you the details for the database backups that are being taken on the database.

select top 1 backup_start_date,backup_finish_date,name,* from backupset
where database_name ='Database name here'order by backupset.backup_start_date desc

Tuesday, August 12, 2008

Decrypt a SQL 2000 Procedure

For all the applications that charge hundreds for decrypting SQL stored procedures can take a hike. Here is a code that would decrypt any procedure. Thanks to the author who wrote this.
http://khsw.blogspot.com/2004/08/decrypt-stored-procedures-on-sql.html

Tuesday, July 1, 2008

Query to search Specifc value in a database

Create this procedure in the required database and here is how you run it:
--To search all columns of all tables in Pubs database for the keyword "Computer"EXEC SearchAllTables 'Computer'GO
Here is the complete stored procedure code:
CREATE PROC SearchAllTables

(
@SearchStr nvarchar(100))
AS
BEGIN
CREATE TABLE #Results (ColumnName nvarchar(370),

ColumnValue nvarchar(3630))
SET NOCOUNT ON

DECLARE @TableName nvarchar(256),
@ColumnName nvarchar(128),
@SearchStr2 nvarchar(110)
SET @TableName = ''
SET @SearchStr2 = QUOTENAME('%' + @SearchStr + '%','''')
WHILE @TableName IS NOT NULL

BEGIN
SET @ColumnName = ''
SET @TableName = (SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME from INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName AND OBJECTPROPERTY
(OBJECT_ID(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)), 'IsMSShipped') = 0)
WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL)

BEGIN
SET @ColumnName = (SELECT MIN(QUOTENAME(COLUMN_NAME)) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA= PARSENAME(@TableName, ) AND TABLE_NAME = PARSENAME(@TableName, 1) AND DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar') AND QUOTENAME(COLUMN_NAME) > @ColumnName )

IF @ColumnName IS NOT NULL
BEGIN
INSERT INTO #Results
EXEC('SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630) FROM ' + @TableName + ' (NOLOCK) ' + ' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2) END
END
END
SELECT ColumnName, ColumnValue FROM #Results

END

Thursday, June 19, 2008

White Paper: SQL Server 2008 Compared to Oracle Database 11g

Microsoft SQL Server has steadily gained ground on other database systems and now surpasses the competition in terms of performance, scalability, security, developer productivity, business intelligence (BI), and compatibility with the 2007 Microsoft Office System. It achieves this at a considerably lower cost than does Oracle Database 11g.
Included in This Document
Executive Summary
Performance and Scalability
Security
Business Intelligence
Integration with the Microsoft Office System
Windows Server 2008
Total Cost of Ownership
Conclusion
http://download.microsoft.com/download/6/9/d/69d1fea7-5b42-437a-b3ba-a4ad13e34ef6/SQL2008_vs_Oracle11g.docx

Wednesday, June 18, 2008

SQL Useful Links

How to make a table Read Only in SQL Server.
http://sqlblogcasts.com/blogs/tonyrogerson/archive/2007/08/26/how-to-make-a-table-read-only-in-sql-server.aspx


Capacity Planning for SQL Server 2000 Database Storage
http://www.mssqltips.com/tip.asp?tip=1267

Change the Recovery Model of All Databases on a SQL Server - SQL Server (T-SQL)
http://vstoolsforum.com/blogs/sqlserver/archive/2007/03/24/change-the-recovery-model-of-all-databases-on-a-sql-server.aspx

Changing table owner - MS SQL
http://www.thescripts.com/forum/thread79663.html

Configuring High Availability Features for Real Application Clusters
http://youngcow.net/doc/oracle9i/rac.920/a96600/haconfig.htm

Configuring High Availability Features for Real Application Clusters
http://www.mssqlcity.com/Articles/Adm/LockView.htm

Detect performance bottlenecks with these tools
http://searchsqlserver.techtarget.com/generic/0,295582,sid87_gci1265212,00.html

Detecting Performance Issues With Sysprocesses - SQL Server Central
http://www.sqlservercentral.com/articles/Performance+Tuning+and+Scaling/detectingperformanceissueswithsysprocesses/2180/

How do I search for special characters (e.g. %) in SQL Server
http://sqlserver2000.databases.aspfaq.com/how-do-i-search-for-special-characters-e-g-in-sql-server.html

How do I send e-mail from SQL Server
http://classicasp.aspfaq.com/email/how-do-i-send-e-mail-from-sql-server.html

How To Configure DTC to Support Oracle Transactions
http://msdn2.microsoft.com/en-us/library/aa344212.aspx

How to Manually Uninstall an Existing Instance of SQL Server 2005
http://msdn2.microsoft.com/en-us/library/aa337087.aspx

HowTo Backup to UNC name using Database Maintenance Wizard
http://support.microsoft.com/?kbid=555128

Reading and Writing Files in SQL Server using T-SQL
http://www.simple-talk.com/sql/t-sql-programming/reading-and-writing-files-in-sql-server-using-t-sql/

Shrink Log file for all Databases - SQL Server Central
http://www.sqlservercentral.com/scripts/Maintenance+and+Management/31533/

SQL SERVER - Index Optimization CheckList « Journey to SQL Authority with Pinal Dave
http://blog.sqlauthority.com/2007/05/08/sql-server-index-optimization-checklist/

SQL Performance tools

http://www.lakesidesql.com/products/products.html