Monday, 29 May 2006

Stored Procedures - Output Parameters

Demonstrates the use of the OUTPUT parameter from a stored procedure -
CREATE PROCEDURE dbo.GetCountByLastName (
@DepartmentName NVARCHAR(50),
@DepartmentCount INT OUTPUT )
AS
select @DepartmentCount = COUNT(*) from HumanResources.EmployeeDepartmentHistory h
inner join HumanResources.Department d
on h.DepartmentID = d.DepartmentID
where d.Name = @DepartmentName and EndDate IS NULL
group by d.Name


Call the procedure like this to fetch the output -
declare @intDepartmentCount int

exec dbo.GetCountByLastName
@DepartmentName = 'Marketing',
@DepartmentCount = @intDepartmentCount OUTPUT

select @intDepartmentCount  as DepartmentCount

Sunday, 28 May 2006

Occasional SQL User?


SQL Server runs as a service. Brilliant on a server, not so brilliant if you just wasn't to do the occasional bit of development on a laptop!

This is where placing a couple of batch files (remember them?) on your desktop can help.
Create two files, say StartSQL.bat and StopSQL.bat and place the following lines in them respectively.

net start mssqlserver

net stop mssqlserver

Then go to Services (located in Control Panel > Administrative Tools) and set SQL Server (MSSQLSERVER) to Manual startup.



Now, SQL wont start on reboot and you turn it on only when you need to.

Wednesday, 24 May 2006

Generic Cursor Template

Generic CURSOR Template -
DECLARE @Variable VARCHAR(200)
DECLARE CURSORNAME CURSOR FOR
SELECT ColumnName FROM Table         
OPEN CURSORNAME
FETCH NEXT FROM CURSORNAME INTO @Variable

WHILE @@FETCH_STATUS = 0
BEGIN

-- DO STUFF HERE !!

FETCH NEXT FROM CURSORNAME INTO @Variable
END

CLOSE CURSORNAME
DEALLOCATE CURSORNAME

Tuesday, 23 May 2006

SQL 2000 : Sending Mail WITHOUT Sql Mail

Creating the procedure -

create procedure [dbo].[CdoSysEMail] 
 (
 @email_from    varchar(200), 
 @email_to      varchar(200), 
 @email_bcc     varchar(200) = '', 
 @email_subject varchar(200) = '', 
 @email_body    varchar(7000) =''
 )
as 

begin
 -- declare variables
 declare @obj_message_id int 
 declare @output int 
 declare @error_output varchar(5000) 
 declare @error_source varchar(255) 
 declare @error_description varchar(1000) 
  
 -- create CDO.Message object 
 exec @output = sp_OACreate 'CDO.Message', @obj_message_id OUT 

 -- set server properties in the object
 exec @output = sp_OASetProperty @obj_message_id, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value','2' 
 exec @output = sp_OASetProperty @obj_message_id, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport").Value','25'
 exec @output = sp_OASetProperty @obj_message_id, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate").Value','0'
 exec @output = sp_OASetProperty @obj_message_id, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value', '193.19.88.36'

 -- save those properties
 exec @output = sp_OAMethod @obj_message_id, 'Configuration.Fields.Update', Null
 
 -- pass the procedure parameters through to the e-mail
 exec @output = sp_OASetProperty @obj_message_id, 'To', @email_to
 exec @output = sp_OASetProperty @obj_message_id, 'Bcc', @email_bcc
 exec @output = sp_OASetProperty @obj_message_id, 'From', @email_from
 exec @output = sp_OASetProperty @obj_message_id, 'Subject', @email_subject
 exec @output = sp_OASetProperty @obj_message_id, 'TextBody', @email_body

 -- send the email
 exec @output = sp_OAMethod @obj_message_id, 'Send', NULL
 
 -- error handling 
 if @output <> 0 
 begin
  declare @No Int
  Set @No=@output
  Select @No
      exec @output = sp_OAGetErrorInfo NULL, @error_source OUT, @error_description OUT
  if @output = 0
   begin
   Set @error_output = 'Error : ' + cast(@No As varchar) + ', Source: ' + @error_source + ', Description: ' + @error_description
   Raiserror(@error_output,16,1)
   end
  else
   begin
      Raiserror('could not determine error (sp_OAGetErrorInfo failed)', 16,1)
      end
 end
 
 -- clean up
 exec @output = sp_OADestroy @obj_message_id
end
go


Testing the procedure -

execute CdoSysEMail 
 @email_from ='sqladmin@mydomain.net',
 @email_to = 'recipient@mydomain.net', 
 @email_subject = 'test, please ignore!', 
 @email_body = 'test message body'

Friday, 19 May 2006

TSQL : Swap Databases

Swap databases code.

Useful for quickly switching content, e.g for websites.
ALTER DATABASE Database1  SET SINGLE_USER with rollback immediate
ALTER DATABASE Database2  SET SINGLE_USER with rollback immediate

EXEC sp_renamedb 'Database1', 'Database3'
EXEC sp_renamedb 'Database2', 'Database1'
EXEC sp_renamedb 'Database3', 'Database2'

ALTER DATABASE Database1  SET MULTI_USER
ALTER DATABASE Database2  SET MULTI_USER

Monday, 15 May 2006

TSQL : Restoring a database to a point in time (Litespeed version)

Sql to restore LiteSpeed backups.

-- restore main bak first

exec master.dbo.xp_restore_database
@database = 'Accounts' ,
@filename = '\\FILESTORE\SQL$\Accounts\Accounts_20060502001507_Friday_LS.bak' ,
@filenumber = 1,
@with = 'NORECOVERY',
@with = 'NOUNLOAD',
@with = 'STATS = 10',
@with = 'REPLACE'
go

-- restore latest diff

exec master.dbo.xp_restore_database
@database = 'Accounts',
@filename = '\\FILESTORE\SQL$\Accounts\Accounts_20060502014913_Thursday.diff',
@filenumber = 1,
@with = 'NORECOVERY',
@with = 'NOUNLOAD',
@with = 'STATS = 10',
@with = 'REPLACE'
go

-- restore transaction log backups
-- repeat for each one, changing NORECOVERY to RECOVERY for the last one.

exec master.dbo.xp_restore_database
@database = 'Accounts',
@filename = '\\FILESTORE\SQL$\Accounts\Accounts_20060502060107_Thursday.trn',
@filenumber = 1,
@with = 'NORECOVERY',
@with = 'NOUNLOAD',
@with = 'STATS = 10',
@with = 'REPLACE'
go

Saturday, 13 May 2006

TSQL : Restoring a database to a point in time

Sql to use once you have identified which backup files need to be restored...
-- restore last full backup
-- use NORECOVERY to state you want to add further backup files

RESTORE DATABASE Sales
FROM DISK = '\\FILESTORE\SQL$\Sales\Sales_20060423180002_Wednesday_MS.bak'
WITH NORECOVERY

-- add last differential backup
-- use NORECOVERY to state you want to add further backup files

RESTORE DATABASE Sales
FROM DISK = '\\FILESTORE\SQL$\Sales\Sales_20060425010002_Friday.diff'
WITH NORECOVERY
go

-- apply transaction log backups , up until the point you wish to restore to
-- use NORECOVERY to state you want to add further backup files
-- on the final restore, omit WITH NORECOVERY or specify WITH RECOVERY so the db is readable.

RESTORE LOG Sales FROM DISK = '\\FILESTORE\SQL$\Sales\Sales_20060425060000_Friday_MS.trn' WITH NORECOVERY
RESTORE LOG Sales FROM DISK = '\\FILESTORE\SQL$\Sales\Sales_20060425070000_Friday_MS.trn' WITH NORECOVERY
RESTORE LOG Sales FROM DISK = '\\FILESTORE\SQL$\Sales\Sales_20060425080000_Friday_MS.trn' WITH NORECOVERY
RESTORE LOG Sales FROM DISK = '\\FILESTORE\SQL$\Sales\Sales_20060425090000_Friday_MS.trn' WITH NORECOVERY
RESTORE LOG Sales FROM DISK = '\\FILESTORE\SQL$\Sales\Sales_20060425100000_Friday_MS.trn' WITH NORECOVERY
RESTORE LOG Sales FROM DISK = '\\FILESTORE\SQL$\Sales\Sales_20060425110000_Friday_MS.trn' WITH NORECOVERY
RESTORE LOG Sales FROM DISK = '\\FILESTORE\SQL$\Sales\Sales_20060425120000_Friday_MS.trn'
go