3. Write a script that attempts to insert a new category named “Guitars” into th
ID: 3740924 • Letter: 3
Question
3. Write a script that attempts to insert a new category named “Guitars” into the Categories table. If the insert is successful, the script should display this message: SUCCESS: Record was inserted. SQL, Murach
If the update is unsuccessful, the script should display a message something like this: FAILURE: Record was not inserted. Error 2627: Violation of UNIQUE KEY constraint 'UQ__Categori__8517B2E0A87CE853'. Cannot insert duplicate key in object 'dbo.Categories'. The duplicate key value is (Guitars)
Explanation / Answer
USE MyGuitarShop;
BEGIN TRY
INSERT INTO Categories(CategoryName)
VALUES('Guitars')
PRINT 'SUCCESS: Record was inserted'
END TRY
BEGIN CATCH
PRINT'FAILURE: Record was not inserted.';
PRINT 'ERROR'+CONVERT(varchar,ERROR_NUMBER(),1)+': '+ERROR_MESSAGE()
END
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.