Constraints einer Tabelle aktivieren bzw. deaktivieren in MSSQL

CREATE PROCEDURE sp_ConstraintState 
                    @TblName    VARCHAR(128),
                    @State        BIT = 1
AS
DECLARE @SQLState VARCHAR(500)
IF @State = 0
    BEGIN
        SET @SQLState = 'ALTER TABLE '+ @TblName + ' NOCHECK CONSTRAINT ALL'
    END
ELSE
    BEGIN
        SET @SQLState = 'ALTER TABLE ' + @TblName + ' CHECK CONSTRAINT ALL'
    END
EXEC (@SQLState)
GO

Aufruf via

exec sp_ConstraintState  'products',0

(Quelle: http://www.databasejournal.com/scripts/article.php/1459571/Disabling-or-Enabling-All-Constraints-of-a-Table-or-Database.htm)

DateTime des Monatsanfangs in MSSQL

Nicht schön, aber selten:

SELECT
    CONVERT
    (
        DATETIME,
        CONVERT(VARCHAR, DATEPART(year, GETDATE())) + '/' 
            + CONVERT(VARCHAR, DATEPART(month, GETDATE())) + '/'
            + '1',
        111
    ) AS Monatsanfang

Ergibt z.B. „2010-12-01 00:00:00.000“