I have a table called Sales.Customers with the following columns:custid, companyname, contactname, contacttitle, address, city, postalcode, country, phone, fax
.
I need to create a stored procedure called 'Sales.InsertCustomers' that inserts data into the Customers table.
The stored procedure must meet the following requirements:
- Data changes occur as a single unit of work.
- Data modifications that are successful are committed and a value of O is returned.
- Data modifications that are unsuccessful are rolled back. The exception severity level is set to 16 and a value of -1 is returned.
- The stored procedure uses a built-it scalar function to evaluate the current condition of data modifications.
- The entire unit of work is terminated and rolled back if a run-time error occurs during execution of the stored procedure.
Here is the code below:
CREATE PROCEDURE Sales.InsertCustomers
@Companyname nvarchar(100),
@ContactName nvarchar(100),
@Contacttitle nvarchar(100),
@Address nvarchar(100),
@City nvarchar(80),
@PostalCode nvarchar(20),
@country nvarchar(50),
@phone nvarchar(20),
@fax nvarchar(20)
AS
BEGIN
SET NOCOUNT ON
SET XACT_ABORT ON
BEGIN TRY
BEGIN TRANSACTION
INSERT INTO Sales.Customers(companyname, contactname, contacttitle, address, city, postalcode, country, phone, fax)
VALUES(@Companyname, @ContactName, @Contacttitle, @Address, @City, @PostalCode, @country, @phone, @fax)
COMMIT TRANSACTION
END TRY
BEGIN CATCH
IF XACT_STATE() <> 0 ROLLBACK TRANSACTION
PRINT 'Unable to create a customer record.'
THROW
RETURN -1
END CATCH
RETURN 0
END;
When I run the code, I get this error:
Msg 102, Level 15, State 1, Procedure InsertCustomers, Line 30 Incorrect syntax near 'THROW'.
How can I fix this?