By using this website, you agree to our Terms of Use (click here)
I just learned about SQL syntax that apparently has been around since SQL Server 2016.
Previously, I would use this SQL code to create a VIEW:
DROP VIEW IF EXISTS dbo.MySQLSystemEvent GO CREATE VIEW dbo.MySQLSystemEvent AS SELECT 2 AS CompanyID,ID,TenantName,[Date],[Level],Source,EventID,ScreenID,[User],Details,Properties FROM SystemEvent
The reason for the first two lines is that I want to be able to run the same SQL Code whether the View already exists or not.
But there are two problems with it:
1. I can never remember the DROP VIEW IF EXISTS... part and always have to Google it.
2. The view gets a new Created date on it which I don't like. I'd like to know the date that the view was originally created.
Now, with my new discovery (that has existed for 8 year already apparently), I can write this instead:
CREATE OR ALTER VIEW dbo.MySQLSystemEvent AS SELECT 2 AS CompanyID,ID,TenantName,[Date],[Level],Source,EventID,ScreenID,[User],Details,Properties FROM SystemEvent
This is awesome! It solves both problems:
1. I can remember CREATE OR ALTER without having to Google it.
2. The Created date on the view doesn't change since an ALTER statement gets run if it already exists.
I love it!