Explain Codes LogoExplain Codes Logo

How to make CREATE OR REPLACE VIEW work in SQL Server?

sql
dynamic-sql
best-practices
view-management
Nikita BarsukovbyNikita Barsukov·Nov 6, 2024
TLDR

Here's a script functioning like CREATE OR REPLACE VIEW in SQL Server:

IF OBJECT_ID('dbo.YourViewName', 'V') IS NOT NULL EXEC('ALTER VIEW dbo.YourViewName AS -- View SQL here'); ELSE EXEC('CREATE VIEW dbo.YourViewName AS -- View SQL here');

Just replace dbo.YourViewName and -- View SQL here with your view's name and SQL code. This ensures the view is updated if it exists or created if it doesn't. It's like hitting two birds with one stone.

Embracing dynamic SQL

Leverage the power of dynamic SQL with EXEC for conditional operations, altering or creating views.

DECLARE @sqlCommand NVARCHAR(MAX) = N''; IF OBJECT_ID('dbo.YourViewName', 'V') IS NULL SET @sqlCommand = 'CREATE VIEW dbo.YourViewName AS -- View SQL here'; ELSE SET @sqlCommand = 'ALTER VIEW dbo.YourViewName AS -- New View SQL here'; EXEC sp_executesql @sqlCommand;

In action, we use sp_executesql for bulletproof execution of dynamic SQL. The whole view creation or modification SQL fits nicely into @sqlCommand.

Juggling large views

Dealing with gargantuan views? Transactions and error handling can be your savior for consistency:

BEGIN TRY BEGIN TRANSACTION; IF OBJECT_ID('dbo.YourViewName', 'V') IS NOT NULL EXEC('DROP VIEW dbo.YourViewName -- Sayonara, old view!'); EXEC('CREATE VIEW dbo.YourViewName AS -- Complex View SQL here'); COMMIT TRANSACTION; END TRY BEGIN CATCH ROLLBACK TRANSACTION; THROW; -- Toss the error like a frisbee END CATCH

Encapsulated operations in a transaction mean any hiccup causes an instant rollback. It's like a time machine for your database.

The Fine Art of Deciding: ALTER vs DROP and CREATE

Here are some guidelines on picking between ALTER and DROP/CREATE:

Loving your dependencies

Use ALTER VIEW to keep all those precious permissions and dependencies. Dropping and creating a view could upset your permissions, and dependencies could go into an existential crisis.

Tweaking with ALTER

Getting itch to tweak minor stuff in your view? Go on with ALTER VIEW. It is efficient, looks cool and makes you feel like a pro.

An Extreme Makeover with DROP and CREATE

For a completely new look and feel, go for DROP and CREATE. It's like getting a brand new car instead of fixing the old one.

Keep these best practices in your SQL toolbox

Here are some practices to swear by:

The truth is in the sys.views

Check your view's existence using sys.views for a robust method:

IF EXISTS (SELECT 1 FROM sys.views WHERE object_id = OBJECT_ID('dbo.YourViewName')) -- The view is alive, time for action!

This approach is like scanning your system's DNA—clean, efficient, reliable.

Nothingness and IS NULL

The IF...IS NULL pattern elegantly handles the non-existent views:

IF OBJECT_ID('dbo.YourViewName', 'V') IS NULL -- Poof! Let's create a view from thin air.

It's like a smoke detector, saving you from unnecessary error firefighting.