Tuesday, 30 January 2007

Fetching local copy of Embedded Flash Movies

Saving Website Files in Firefox

[ 1 ] Tools > Page Info
[ 2 ] Media tab.
{ list of downloaded files for that page are shown }
[ 3 ] Locate desired .SWF file and click 'save as'

Get source movie

[ 1 ] Open SWF file in notepad. (Right-Click, Open With - Chose Notepad)
[ 2 ] Look in markup for name of file. (in this case .FLV file)
[ 3 ] Download the .FLV file directly from the original site.

Saturday, 27 January 2007

TSQL Function : fn_SqueezeSpaces

This function reduces Multiple Spaces to a single instance.
Good for cleaning up text -

CREATE FUNCTION [dbo].[fn_SqueezeSpaces] (@String varchar(8000)) 
RETURNS VARCHAR(8000) 
AS 
BEGIN 
RETURN REPLACE(REPLACE(REPLACE(@String,' ',' '),' ',''),'','')
END

Friday, 26 January 2007

Disabling / Disable an index (sql 2005+)

'Why?' was my first thought, but then I remembered that when loading data to a table, it is faster without indexes in place

-- disable an index
ALTER INDEX IX_Contact_EmailAddress ON Person.Contact DISABLE

-- re-enable the index (by rebuilding)
ALTER INDEX IX_Contact_EmailAddress ON Person.Contact REBUILD

-- see what indexes are disabled
SELECT [NAME], [TYPE_DESC], [IS_DISABLED] FROM SYS.INDEXES WHERE [IS_DISABLED] = 1

Thursday, 25 January 2007

Kill Processes Cursor

Kill all processes running by a user >
DECLARE KILLUSERPROCESS CURSOR FORWARD_ONLY FOR
SELECT SPID
FROM   SYSPROCESSES
WHERE  LTRIM(RTRIM(LOGINAME)) = 'login'

DECLARE  @intSPID INT
DECLARE  @chvDSQL VARCHAR(128)

OPEN KILLUSERPROCESS
FETCH NEXT FROM KILLUSERPROCESS INTO @intSPID

WHILE (@@FETCH_STATUS = 0)
  BEGIN
    IF EXISTS (SELECT SPID
               FROM   SYSPROCESSES
               WHERE  LTRIM(RTRIM(LOGINAME)) = 'login'
                      AND SPID = @intSPID)
      BEGIN
        SET @chvDSQL = 'KILL ' + CAST(@intSPID AS VARCHAR)        
        EXEC( @chvDSQL)
      END
    
    FETCH NEXT FROM KILLUSERPROCESS INTO @intSPID
  END

CLOSE KILLUSERPROCESS
DEALLOCATE KILLUSERPROCESS

Monday, 22 January 2007

Generic DML Trigger Code

Generic code to cope with DML actions on your tables
CREATE TRIGGER [schemaname].[TRG_triggername]
ON [schemaname].[tablename]
FOR DELETE,INSERT,UPDATE
AS
DECLARE @ACTION CHAR(6)
DECLARE @DELETE BIT
DECLARE @INSERT BIT

SET @DELETE = 0
SET @INSERT = 0

IF EXISTS (SELECT TOP 1 1 FROM DELETED)  SET @DELETE = 1
IF EXISTS (SELECT TOP 1 1 FROM INSERTED) SET @INSERT = 1

IF @INSERT = 1 AND @DELETE = 1 SET @ACTION = 'UPDATE'
IF @INSERT = 1 AND @DELETE = 0 SET @ACTION = 'INSERT'
IF @DELETE = 1 AND @INSERT = 0 SET @ACTION = 'DELETE'
IF @INSERT = 0 AND @DELETE = 0 RETURN

/*

Do Stuff here.
Refer to Added and Updated records from the INSERTED table
select * from inserted
Refer to Removed records from the DELETED table
select * from deleted
e.g.
INSERT INTO AuditTable 'INSERT', GETDATE(), * FROM INSERTED
*/
GO

Saturday, 20 January 2007

Service Pack / Hotfix on SQL clusters

Guidelines for a smooth install of a Service Pack or Hotfix on a clustered SQL environment >

1 Install SP on first node
2 Failover active cluster groups to second node (Cluster Administrator)
3 Restart first node

(await restart of first node)

4 Install SP on second node
5 Failover active cluster groups to first node
6 Restart second node

(await restart of second node)

7 Test failing over of each group, leaving them back on original nodes.