Tag Archives: sqlserver

Default Values in Stored Procedures, Severity Levels

ALTER PROCEDURE testRaiseError @Severity int =18 AS BEGIN – SET NOCOUNT ON added to prevent extra result sets from – interfering with SELECT statements. SET NOCOUNT ON; Raiserror(‘Test Raiserrror’,@Severity,10) END Fehler ab einer Severity von 10 brechen ein laufendes TSQL Script ab und gelten als Fehler

Transaktion mit Fehlerbehandlung in TSQL

Beispiel Insert und Update in einer Transaktion, wenn eines fehlschlägt wird zurückgerollt und der Fehler weitergegeben BEGIN TRAN T1 BEGIN TRY –write all values to mcit.auditrail (@UserId) INSERT INTO [AuditTrail] ([ActionType],[ObjectType],[ObjectId],[OldValues],[NewValues],[UserId]) SELECT ‘Update’,’Products’,Id,CONVERT(Nvarchar(MAX),Price),CONVERT(Nvarchar(MAX),Price*@PriceFact),@UserId from product where [year]=@Year and SupplierGroupId=@SupplierGroupId and IsDeleted=0 update mcit.product set Price=Price*@PriceFact where [year]=@Year and SupplierGroupId=@SupplierGroupId and IsDeleted=0 END TRY BEGIN CATCH [...]