Peter Larsson Blog

Patron Saint of Lost Yaks

Another sequencing algorithm

This problem originated here http://sqlforums.windowsitpro.com/web/forum/messageview.aspx?catid=87&threadid=90916&enterthread=y and I post the solution here for two reasons. 1) The forum above does not support code tags 2) The common interest is high enough -- Prepare sample data DECLARE@Sample TABLE (  HoleID CHAR(8),  mFrom SMALLMONEY,  mTo SMALLMONEY,  Result SMALLMONEY,  PRIMARY KEY CLUSTERED  (  HoleID,  mFrom  ),  Seq INT  ) INSERT @Sample  (  HoleID,  mFrom,  mTo,  Result  ) SELECT'TWDD0004',1,2,0. Read more →

How to tell if a number is a "POWER of 2"-number

SELECT Number, 1 - SIGN(Number & (Number - 1)) FROM master..spt_values WHERE Type = 'P' AND Number > 0  Other way is  CREATEFUNCTION dbo.isPowerOf2 ( @i INT ) RETURNSBIT AS BEGIN DECLARE @x FLOAT    SET @x = LOG(Number) / LOG(2) RETURN CASE WHEN FLOOR(@x) = CEILING(@x) THEN 1 ELSE 0 END END Legacy Comments RamiReddy 2008-08-13 re: How to tell if a number is a "POWER of 2"-number Is that first query will work for the number 524288 which is 2 power 19. Read more →

How to get IP address

DECLARE@Interfaces TABLE ( RowID INT IDENTITY(0, 1), Interface CHAR(38), IP VARCHAR(15) )   INSERT@Interfaces ( Interface ) EXECmaster..xp_regenumkeys N'HKEY_LOCAL_MACHINE', N'System\CurrentControlSet\Services\TcpIP\Parameters\Interfaces'   DECLARE@RowID INT, @IP VARCHAR(15), @Key NVARCHAR(200)   SELECT@RowID = MAX(RowID) FROM@Interfaces   WHILE@RowID >= 0 BEGIN SELECT @Key = N'System\CurrentControlSet\Services\TcpIP\Parameters\Interfaces\' + Interface FROM @Interfaces WHERE RowID = @RowID   EXEC master. Read more →

How to get authentication mode in SQL Server

CREATEFUNCTION dbo.fnGetSQLServerAuthenticationMode ( ) RETURNSINT AS   BEGIN DECLARE @InstanceName NVARCHAR(1000), @Key NVARCHAR(4000), @LoginMode INT   EXEC master..xp_regread N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\Microsoft SQL Server\Instance Names\SQL\', N'MSSQLSERVER', @InstanceName OUTPUT   IF @@ERROR <> 0 OR @InstanceName IS NULL RETURN NULL   SET @Key = N'Software\Microsoft\Microsoft SQL Server\' + @InstanceName + N'\MSSQLServer\'   EXEC master. Read more →

How to change authentication mode in SQL Server

CREATEPROCEDURE dbo.uspSetSQLServerAuthenticationMode ( @MixedMode BIT ) AS   SETNOCOUNT ON   DECLARE@InstanceName NVARCHAR(1000), @Key NVARCHAR(4000), @NewLoginMode INT, @OldLoginMode INT   EXECmaster..xp_regread N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\Microsoft SQL Server\Instance Names\SQL\', N'MSSQLSERVER', @InstanceName OUTPUT   IF@@ERROR <> 0 OR @InstanceName IS NULL BEGIN RAISERROR('Could not read SQL Server instance name. Read more →

Some SQL Server network properties

DECLARE@Stage TABLE ( RowID INT IDENTITY(0, 1) PRIMARY KEY CLUSTERED, Data VARCHAR(90), Section INT )   INSERT@Stage ( Data ) EXECxp_cmdshell 'ipconfig /all'   DECLARE@Section INT   SET@Section = 0   UPDATE @Stage SET@Section = Section = CASE WHENASCII(LEFT(Data, 1)) > 32 THEN @Section + 1 ELSE@Section END   SELECTMAX(CASE WHEN x. Read more →

Horizontal partitioning, Enterprise style

CREATEPARTITION FUNCTION pfOrderDate ( DATETIME ) ASRANGE RIGHT FOR VALUES ( '20000101', '20010101', '20020101' ) GO   CREATEPARTITION SCHEME psYak ASPARTITION pfOrderDate ALLTO ([PRIMARY]) GO   CREATETABLE Orders ( OrderID INT NOT NULL, CustomerID VARCHAR(15) NOT NULL, OrderDate DATETIME NOT NULL ) ONpsYak(OrderDate) GO   CREATECLUSTERED INDEX IX_OrderID ONOrders ( OrderID ) CREATENONCLUSTERED INDEX IX_OrderDate ONOrders ( OrderDate ) INCLUDE( OrderID, CustomerID ) GO INSERTOrders ( OrderID, CustomerID, OrderDate ) SELECT1, 'Peso', '20011225' UNIONALL SELECT2, 'Jennie', '20020314' SELECTOrderID, OrderDate FROMOrders WHEREOrderDate = '20011225'   SELECTOrderID, OrderDate FROMOrders WHEREOrderID = 1   DROPTABLE Orders DROPPARTITION SCHEME psYak DROPPARTITION FUNCTION pfOrderDate   Read more →

SQL Injection

Every now and then I see sites where commands are concatenated and sent to database server. The author must really trust the user inputs! For every system built this way, you can expect at least one attack with SQL injection. Read more →

Index pages

SQL Server 2005 introduced the new DMV views. They are great and a real improvement to debug and optimize queries. I find sys.dm_db_index_physical_stats very useful and often write this type of code Read more →

Some XML search approaches

I just played around with some different techniques to fetch relevant data from XML content.  DECLARE@XMLString XML, @Search VARCHAR(50)   SELECT@XMLString = ' <Customers> <Customer> <FirstName>Kevin</FirstName> <LastName>Goff</LastName> <City type="aca">Camp Hill</City> </Customer> <Customer> <FirstName>Steve</FirstName> <LastName>Goff</LastName> <City type="acb">Philadelphia</City> </Customer> </Customers>', @Search = 'Camp Hill'   -- Get all customers living in Camp Hill SELECTcust. Read more →