SQL Server: How do I ignore errors on a view creation?

  • Joseph_Schrag

    SSC Enthusiast

    Points: 168

    I have a sql script which creates a view. This view references a table that does not exist when the view is created. So, it throws an error. I am wondering how I can catch the error and allow the view to be created anyway with no error thrown (important). I have tried wrapping it in a try catch, but it seems that is not allowed. I have tried using an if (@@error <> 0) statement, but it doesn't stop the error from being thrown.

    I just want to prevent the error from being returned at all. I have also found that Oracle has exactly what I want (CREATE FORCE VIEW), but that is not available in SQL Server.

    Any ideas?

  • Lowell

    SSC Guru

    Points: 323349

    Sorry, it's not possible with a view.

    in SQL Server, only a procedure can be created where the objects it uses do not exist yet;

    everything else...tables with foreign keys, views and functions require the object to exist.

    Can't you change the order of your script so that the dependent items are created first?


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Joseph_Schrag

    SSC Enthusiast

    Points: 168

    Unfortunately no. The table is created manually later. It's not even in the script.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply