This has been asked many times, but the top rated answer doesn't work.
BEGIN TRY
USE [**DATABASE**]
END TRY
BEGIN CATCH
set noexec on
raiserror('Oh no a fatal error', 20, -1) with log
return
END CATCH
GO
IF EXISTS (SELECT * FROM sys.check_constraints WHERE object_id = OBJECT_ID(N'[dbo].[xxxxx]') AND parent_object_id = OBJECT_ID(N'[dbo].[xxxxxx]'))
ALTER TABLE [dbo].[xxxxx] DROP CONSTRAINT [xxxxxx]
GO
Seems like no matter what I do that IF EXISTS
still runs. I've tried GOTO
. I've tried set noexec on
. I've tried RETURN
.
It definitely fails on the USE statement and it definitely continues to run the next block after the first GO.
The point here is that I'm giving this script to other people and they must set the database name before running it. But they'll forget so I need a nice descriptive error message.