Tuesday 12 June 2007

Comma delimited string to table

CREATE FUNCTION [dbo].[ufn_CSVToTable] ( @StringInput VARCHAR(8000) )
RETURNS @OutputTable TABLE ( [String] VARCHAR(10) )
AS
BEGIN

DECLARE @String VARCHAR(10)

WHILE LEN(@StringInput) > 0
BEGIN
SET @String = LEFT(@StringInput,
ISNULL(NULLIF(CHARINDEX(',', @StringInput) - 1, -1),
LEN(@StringInput)))
SET @StringInput = SUBSTRING(@StringInput,
ISNULL(NULLIF(CHARINDEX(',', @StringInput), 0),
LEN(@StringInput)) + 1, LEN(@StringInput))

INSERT INTO @OutputTable ( [String] )
VALUES ( @String )
END

RETURN
END
GO

Monday 11 June 2007

SQL Server performance counters are missing in Windows 64bit

When you run the PERFMON on a 64 bit windows server you are actually running the 64bit version of the application which can only load 64bit counters, but not counters for a local 32-bit applications. To workaround the problem, launch the application using /32 switch, like this mmc /32 perfmon.msc

Thursday 7 June 2007

Which action fired the trigger?

Using the following logic you can identify which action cased the trigger to fire

if exists (select 1 from deleted) and exists (select 1 from inserted)
print ‘Update’

if exists (select 1 from inserted) and not exists (select 1 from deleted)
print ‘Insert’

if exists (select 1 from deleted) and not exists (select 1 from inserted)
print ‘Delete’

Tuesday 5 June 2007

Display trigger status

--version sql server 2000

SELECT
name,
status = CASE WHEN OBJECTPROPERTY (id, 'ExecIsTriggerDisabled') = 0
THEN 'Enabled' ELSE 'Disabled' END,
owner = OBJECT_NAME (parent_obj)
FROM
sysobjects
WHERE
type = 'TR'