SQL Snippets

From SpenchWiki
Jump to: navigation, search

Snippets

  • Update index statistics:
EXEC sp_updatestats
  • Server version:
SELECT @@VERSION
  • Case-sensitive string comparison:
WHERE	(ComponentName.Name = @name COLLATE SQL_Latin1_General_CP1_CS_AS)
  • Finding a sproc in the meta-database
SELECT *
    FROM INFORMATION_SCHEMA.ROUTINES 
   WHERE SPECIFIC_NAME = N'uspFindComponent'
     AND SPECIFIC_SCHEMA = N'<Schema_Name, sysname, Schema_Name>'
  • Declaring a table variable
DECLARE @TableVar TABLE (Cola int PRIMARY KEY, Colb char(3))

INSERT INTO @TableVar VALUES (1, 'abc')
INSERT INTO @TableVar VALUES (2, 'def')

SELECT * FROM @TableVar
GO
  • WHERE EXISTS condition
SELECT Version FROM LanguageVersion WHERE EXISTS
	(
		SELECT * FROM LanguageVersion
	)
  • Selecting results into a table variable & CTE using WITH
DECLARE @i int
DECLARE @t TABLE (L bigint, ID bigint, V nvarchar(64))
INSERT @t
EXEC @i = uspFindLanguageVersion 'My Language'
SELECT V FROM @t
SELECT @i AS 'Return'
GO

WITH DirReps(ManagerID, DirectReports) AS
(
    SELECT Version, Language
    FROM LanguageVersion AS e
)
SELECT ManagerID, DirectReports
FROM DirReps
ORDER BY ManagerID;
GO
  • TRANSACTION
BEGIN TRANSACTION;

BEGIN TRY

-- Statements

END TRY
BEGIN CATCH
 SELECT
        ERROR_NUMBER() AS ErrorNumber,
        ERROR_SEVERITY() AS ErrorSeverity,
        ERROR_STATE() as ErrorState,
        ERROR_PROCEDURE() as ErrorProcedure,
        ERROR_LINE() as ErrorLine,
        ERROR_MESSAGE() as ErrorMessage;

    IF @@TRANCOUNT > 0
        ROLLBACK TRANSACTION;
	RETURN(1);
END CATCH;

IF @@TRANCOUNT > 0
    COMMIT TRANSACTION;
	RETURN(0);

Notes

  • Cannot have ORDER BY clause in a view's definition
  • Performing replication on sprocs with GO not commented out using standard SQL -- comment will result in replication failure:
-- Drop stored procedure if it already exists
/*IF EXISTS (
  SELECT * 
    FROM INFORMATION_SCHEMA.ROUTINES 
	WHERE SPECIFIC_NAME = N'uspFindComponent'
)
   DROP PROCEDURE uspFindComponent
--GO

CREATE PROCEDURE uspFindComponent
*/ALTER PROCEDURE [dbo].[uspFindComponent]

MSDN links

More snippets

  • Derived query
SELECT     ComponentName.Name, derivedtbl_1.ID
FROM         ComponentName CROSS JOIN
                          (SELECT     ID
                            FROM          Component) AS derivedtbl_1
	WHERE derivedtbl_1.ID = ComponentName.Component
  • INNER JOIN
SELECT     ComponentReference.Reference, ComponentReference.Component, Reference.Location, ComponentReference.ID
FROM         ComponentReference INNER JOIN
                      Reference ON ComponentReference.Reference = Reference.ID
WHERE     (ComponentReference.Component = 983)
  • Generating a GUID
DECLARE @id uniqueidentifier
SET @id = NEWID()
DECLARE @txt nvarchar(36)
SET @txt = CONVERT(nvarchar(36), @id)
SELECT @id, @txt, LEN(@txt)
  • Procedure for converting a hexadecimal string into the original binary data
CREATE PROCEDURE dbo.uspHexToBinary
	@s nvarchar(MAX),
	@b varbinary(MAX) OUT
AS
	DECLARE @sql nvarchar(MAX)
	SET @sql = 'SELECT @b = ' + @s
	EXEC sp_executesql @sql, N'@b VARBINARY(MAX) OUTPUT', @b OUTPUT
GO
  • Clever EXISTS ( SELECT 1 ) JOIN trick
SELECT  r2.*
FROM    (
        SELECT  TOP 50 CustomerID
        FROM    (
                SELECT  MovieID
                FROM    Ratings
                WHERE   CustomerID = 915
                ) q
        JOIN    Ratings r
        ON      r.MovieID = q.MovieID
                AND CustomerID <> 915
                AND EXISTS
                (
                SELECT  1
                FROM    Ratings re
                WHERE   re.MovieID = 1
                        AND re.CustomerID = r.CustomerID
                )
        GROUP BY
                CustomerID
        ORDER BY
                COUNT(*) DESC
        ) ro
JOIN    Ratings r2
ON      r2.MovieID = 1
        AND r2.CustomerID = ro.CustomerID
  • Spatial index statistics (useful for debugging slow query or plan warnings)

If query is slow (especially when using a JOIN or limiting row count using TOP or ROW_NUMBER), force plan to use spatial index with (no quotes):

WITH (INDEX(<spatial index name>))

This will avoid the case where a plan is generated that suggests it's faster to seek over all rows and filter by a spatial predicate, instead of actually using the spatial index.

Plan utilising spatial index might show a warning on the "Clustered Index Seek (Spatial)" saying "Columns with no statistics" (Cell_Attributes, SRID, pk0). This may have something to do with the sub-optimal plan being generated when the query has certain conditions (e.g. TOP 690 is slow, while TOP 691 and above is fast).

declare c cursor static for
select object_id, name, index_id from sys.indexes where type_desc = 'SPATIAL'
open c
      declare @obj int
      declare @statsname varchar(max)
      declare @indid int
      declare @tname varchar(max)
fetch next from c into @obj, @statsname, @indid
while (@@fetch_status = 0)
begin
      declare @internaltablename varchar(max)
      declare @stmt varchar(max)
      fetch next from c into @obj, @statsname, @indid
      select top 1 @tname = name from sys.objects where object_id = @obj
      set @internaltablename = 'sys.extended_index_' + cast(@obj as varchar(max)) + '_' + cast(@indid as varchar(max))
    select 'Showing statistics for ', @tname
      select @stmt = 'dbcc show_statistics (''' + @internaltablename + ''', ''' + @statsname + ''')'
      exec (@stmt)
end
close c
deallocate c

Spatial index statistics missing