LoslegenKostenlos loslegen

THROW without parameters

You want to prepare a stored procedure to insert new products in the database. In that stored procedure, you want to insert the possible errors in a table called errors, and after that, re-throw the original error.

How do you prepare the stored procedure?

Diese Übung ist Teil des Kurses

Transactions and Error Handling in SQL Server

Kurs anzeigen

Anleitung zur Übung

  • Surround the error handling with a CATCH block.
  • Insert the error in the errors table.
  • End the insert statement with a semicolon (;).
  • Re-throw the original error.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

CREATE PROCEDURE insert_product
  @product_name VARCHAR(50),
  @stock INT,
  @price DECIMAL

AS

BEGIN TRY
	INSERT INTO products (product_name, stock, price)
		VALUES (@product_name, @stock, @price);
END TRY
-- Set up the CATCH block
___ ___
	-- Insert the error and end the statement with a semicolon
    ___ ___ errors VALUES ('Error inserting a product')___
    -- Re-throw the error
	___; 
___ ___
Code bearbeiten und ausführen