SQL Fehler erzeugen

Squicky

Lt. Commander
Dabei seit
Sep. 2002
Beiträge
1.395
Hallo

Wie kann man in einem Microsoft T-SQL Skript einen eigenen Fehler inkl. Fehlermeldung erzeugen und auswerfen, so dass das Skript dann auch die Arbeit abbricht.

Code:
RAISERROR ('The record does not exist.', 11, 0);

Gibt einen Fehler/Meldung aus, aber das Skript läuft dann weiter.

Es soll ein Fehler wie bei:
Code:
declare @a int;
select @ = 'abc';

erzeugt werden.

Danke
 
Hi Squicky,

versuch es mal mit

PHP:
BEGIN TRY
    -- RAISERROR with severity 11-19 will cause execution to 
    -- jump to the CATCH block.
    RAISERROR ('Error raised in TRY block.', -- Message text.
               16, -- Severity.
               1 -- State.
               );
END TRY
BEGIN CATCH
    DECLARE @ErrorMessage NVARCHAR(4000);
    DECLARE @ErrorSeverity INT;
    DECLARE @ErrorState INT;

    SELECT 
        @ErrorMessage = ERROR_MESSAGE(),
        @ErrorSeverity = ERROR_SEVERITY(),
        @ErrorState = ERROR_STATE();

    -- Use RAISERROR inside the CATCH block to return error
    -- information about the original error that caused
    -- execution to jump to the CATCH block.
    RAISERROR (@ErrorMessage, -- Message text.
               @ErrorSeverity, -- Severity.
               @ErrorState -- State.
               );
END CATCH;

Quelle: Microsoft MSDN
 
Hi.

Leider kein Erfolg:
Code:
print 'aaaa'

BEGIN TRY
    -- RAISERROR with severity 11-19 will cause execution to 
    -- jump to the CATCH block.
    RAISERROR ('Error raised in TRY block.', -- Message text.
               16, -- Severity.
               1 -- State.
               );
END TRY
BEGIN CATCH
    DECLARE @ErrorMessage NVARCHAR(4000);
    DECLARE @ErrorSeverity INT;
    DECLARE @ErrorState INT;

    SELECT 
        @ErrorMessage = ERROR_MESSAGE(),
        @ErrorSeverity = ERROR_SEVERITY(),
        @ErrorState = ERROR_STATE();

    -- Use RAISERROR inside the CATCH block to return error
    -- information about the original error that caused
    -- execution to jump to the CATCH block.
    RAISERROR (@ErrorMessage, -- Message text.
               @ErrorSeverity, -- Severity.
               @ErrorState -- State.
               );
END CATCH;

print 'bbbb'

Denn die Ausgabe lautet:
Code:
aaaa
Meldung 50000, Ebene 16, Status 1, Zeile 26
Error raised in TRY block.
bbbb

Bei dem Beispiel sollte "bbbb" nicht mehr ausgegeben werden.
 
Ich hätte da nur noch folgendes im Angebot:

PHP:
print 'hi'
go
raiserror('Oh no a fatal error', 20, -1) with log
go
print 'ho'
 
Zurück
Top