Difference between revisions of "SQL Snippets"

From SpenchWiki
Jump to: navigation, search
(CONVERT uniqueidentifier to text)
Line 1: Line 1:
 +
* Cannot have ORDER BY clause in a view's definition
 +
 +
----
 +
 
<syntaxhighlight lang="tsql">
 
<syntaxhighlight lang="tsql">
 
SELECT *
 
SELECT *

Revision as of 22:58, 5 July 2009

  • Cannot have ORDER BY clause in a view's definition

SELECT *
    FROM INFORMATION_SCHEMA.ROUTINES 
   WHERE SPECIFIC_NAME = N'uspFindComponent'
     AND SPECIFIC_SCHEMA = N'<Schema_Name, sysname, Schema_Name>'

Special data types

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

SELECT Version FROM LanguageVersion WHERE EXISTS
	(
		SELECT * FROM LanguageVersion
	)

DECLARE @i int
DECLARE @t TABLE (L bigint, ID bigint, V nvarchar(64))
INSERT @t
EXEC @i = uspFindLanguageVersion 'PureData'
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

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);


SELECT     ComponentName.Name, derivedtbl_1.ID
FROM         ComponentName CROSS JOIN
                          (SELECT     ID
                            FROM          Component) AS derivedtbl_1
	WHERE derivedtbl_1.ID = ComponentName.Component

SELECT     ComponentReference.Reference, ComponentReference.Component, Reference.Location, ComponentReference.ID
FROM         ComponentReference INNER JOIN
                      Reference ON ComponentReference.Reference = Reference.ID
WHERE     (ComponentReference.Component = 983)

DECLARE @id uniqueidentifier
SET @id = NEWID()
DECLARE @txt nvarchar(36)
SET @txt = CONVERT(nvarchar(36), @id)
SELECT @id, @txt, LEN(@txt)