How to skip checking the existence of objects when creating Stored Procedure

  • Hi,

    Just wondering if there is a way to force SQL Server to skip checking if a table or a column of a table, which a used by the SP, exist when creating a Stored Procedure.

    Thanks.

  • You could convert the sql statement inside your proc to be dynamic SQL. That way the compiler won't check.

  • Do not know why you would want to create an SP and not know if it would function, but yes it can be done for example:

    CREATE PROCEDURE Dbo.Garbage

    AS

    SELECT * FROM Dbo.NoneExisting

    WHERE Col1 > 0

    Just click on the execute button to create the procedure, and it is, providing the syntax is correct. Now invoking the procedure of course will result in errors since in my DB the table named NoneExisting does not exist.

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • bitbucket-25253 (10/1/2009)


    Do not know why you would want to create an SP and not know if it would function, but yes it can be done for example:

    CREATE PROCEDURE Dbo.Garbage

    AS

    SELECT * FROM Dbo.NoneExisting

    WHERE Col1 > 0

    Just click on the execute button to create the procedure, and it is, providing the syntax is correct. Now invoking the procedure of course will result in errors since in my DB the table named NoneExisting does not exist.

    Nope, this one does not work. It errors that some columns are missing.

    The reason I want this is because I need to run an import script which runs better when indexed views are not there yet, which relies on a stored procedure script (one of the stored procedures used in the import script.) Some of the other stored procedures rely on the views, which should not be there yet for the import script to work. I understand that I could split the stored procedures script into the part used in the import script and the rest and break the circle, but I do not want to do this.

  • philip-seamark (10/1/2009)


    You could convert the sql statement inside your proc to be dynamic SQL. That way the compiler won't check.

    I guess this would work, but it involves to many changes to the scripts.

    I thought there would be some sort of option which you set in the begining of the script and then run the script creating stored procedures safely.

  • Roust_m (10/1/2009)


    bitbucket-25253 (10/1/2009)


    Do not know why you would want to create an SP and not know if it would function, but yes it can be done for example:

    CREATE PROCEDURE Dbo.Garbage

    AS

    SELECT * FROM Dbo.NoneExisting

    WHERE Col1 > 0

    Just click on the execute button to create the procedure, and it is, providing the syntax is correct. Now invoking the procedure of course will result in errors since in my DB the table named NoneExisting does not exist.

    Nope, this one does not work. It errors that some columns are missing.

    Just out of curiosity!, it should compile just fine and if you are executing then only it would throw an error!

    ---------------------------------------------------------------------------------

  • PP-564103 (10/2/2009)


    Roust_m (10/1/2009)


    bitbucket-25253 (10/1/2009)


    Do not know why you would want to create an SP and not know if it would function, but yes it can be done for example:

    CREATE PROCEDURE Dbo.Garbage

    AS

    SELECT * FROM Dbo.NoneExisting

    WHERE Col1 > 0

    Just click on the execute button to create the procedure, and it is, providing the syntax is correct. Now invoking the procedure of course will result in errors since in my DB the table named NoneExisting does not exist.

    Nope, this one does not work. It errors that some columns are missing.

    Just out of curiosity!, it should compile just fine and if you are executing then only it would throw an error!

    These will compile fine if the entire table does not exist. They will not compile if the table exists but the column you're referencing does not. It will error. Try it.

    Seth Phelabaum


    Consistency is only a virtue if you're not a screwup. 😉

    Links: How to Post Sample Data[/url] :: Running Totals[/url] :: Tally Table[/url] :: Cross Tabs/Pivots[/url] :: String Concatenation[/url]

  • Garadin

    These will compile fine if the entire table does not exist. They will not compile if the table exists but the column you're referencing does not. It will error. Try it.

    Tried in using 2005 with the table existing ... works fine for me; Now remember I said just click on the Execute button which does not parse the procedure, it just saves it...

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • See below.

    Seth Phelabaum


    Consistency is only a virtue if you're not a screwup. 😉

    Links: How to Post Sample Data[/url] :: Running Totals[/url] :: Tally Table[/url] :: Cross Tabs/Pivots[/url] :: String Concatenation[/url]

  • This fails on my 2K8 server on 80/90/100 Compatibility levels. I'm honestly not sure how you have it working on yours.

    --CREATE DATABASE Test

    --GO

    ALTER DATABASE Test SET COMPATIBILITY_LEVEL = 100

    GO

    USE Test

    CREATE TABLE #1(

    aint)

    GO

    CREATE PROCEDURE sethtest1

    AS

    SELECT a,b

    FROM #1

    GO

    DROP TABLE #1

    Error:

    Msg 207, Level 16, State 1, Procedure sethtest1, Line 5

    Invalid column name 'b'.

    Looking into this a bit resulted in the following:

    http://msdn.microsoft.com/en-us/library/ms190686.aspx


    When a stored procedure is created, the statements in the procedure are parsed for syntactical accuracy. If a syntactical error is encountered in the procedure definition, an error is returned and the stored procedure is not created. If the statements are syntactically correct, the text of the stored procedure is stored in the sys.sql_modules catalog view.

    When a stored procedure is executed for the first time, the query processor reads the text of the stored procedure from the sys.sql_modules catalog view and checks that the names of the objects used by the procedure are present. This process is called deferred name resolution because table objects referenced by the stored procedure need not exist when the stored procedure is created, but only when it is executed.

    Note:

    Deferred name resolution can only be used when you reference nonexistent table objects. All other objects must exist at the time the stored procedure is created.For example, when you reference an existing table in a stored procedure you cannot list nonexistent columns for that table.

    Seth Phelabaum


    Consistency is only a virtue if you're not a screwup. 😉

    Links: How to Post Sample Data[/url] :: Running Totals[/url] :: Tally Table[/url] :: Cross Tabs/Pivots[/url] :: String Concatenation[/url]

  • Garadin

    These were my test conditions using Developer version of SQL Server 2008 Microsoft SQL Server Management Studio 10.0.1600.22 ((SQL_PreRelease).080709-1414 )

    /

    ALTER DATABASE Test

    SET COMPATIBILITY_LEVEL = 100

    CREATE TABLE Dbo.NonExisting

    (ABC VARCHAR(10))

    CREATE PROCEDURE Dbo.Garbage

    AS

    SELECT * FROM Dbo.NoneExisting

    WHERE Col1 > 0

    CREATE PROCEDURE Dbo.Garbage2

    AS

    SELECT ABC FROM Dbo.NoneExisting

    WHERE Col1 > 0

    DROP TABLE Dbo.NonExisting

    DROP PROCEDURE Dbo.Garbage

    DROP PROCEDURE Dbo.Garbage2

    Message for each of the above was

    Command(s) completed successfully.

    Now I am as puzzled as you are as to what is the difference / why there is a difference. Could it have been an error in the pre-release SSMS and fixed in the production release? Not likely.

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • Aside from the discussion about how to create stored procs that reference non-existent objects - isn't the best solution for the OP to keep the views, but drop the indexes from them, rather than drop the views entirely? Or am I missing something?

    Atlantis Interactive - SQL Server Tools
    My blog[/url]
    Why I wrote a sql query analyzer clone

  • The original question by the OP was

    Just wondering if there is a way to force SQL Server to skip checking if a table or a column of a table, which a used by the SP, exist when creating a Stored Procedure.

    It then became more and more complex, and the discussion of the original question has just continued on the basis that others jumped in with solutions to the more complex question.

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • Yeah, but you said you didn't know why the OP would want to do that, and he clarified:

    ...The reason I want this is because I need to run an import script which runs better when indexed views are not there yet, which relies on a stored procedure script...

    Atlantis Interactive - SQL Server Tools
    My blog[/url]
    Why I wrote a sql query analyzer clone

  • bitbucket-25253 (10/2/2009)


    Garadin

    These were my test conditions using Developer version of SQL Server 2008 Microsoft SQL Server Management Studio 10.0.1600.22 ((SQL_PreRelease).080709-1414 )

    /

    ALTER DATABASE Test

    SET COMPATIBILITY_LEVEL = 100

    CREATE TABLE Dbo.NonExisting

    (ABC VARCHAR(10))

    CREATE PROCEDURE Dbo.Garbage

    AS

    SELECT * FROM Dbo.NoneExisting

    WHERE Col1 > 0

    CREATE PROCEDURE Dbo.Garbage2

    AS

    SELECT ABC FROM Dbo.NoneExisting

    WHERE Col1 > 0

    DROP TABLE Dbo.NonExisting

    DROP PROCEDURE Dbo.Garbage

    DROP PROCEDURE Dbo.Garbage2

    Message for each of the above was

    Command(s) completed successfully.

    Now I am as puzzled as you are as to what is the difference / why there is a difference. Could it have been an error in the pre-release SSMS and fixed in the production release? Not likely.

    If only your SSMS had a spell check! 😉

    Seth Phelabaum


    Consistency is only a virtue if you're not a screwup. 😉

    Links: How to Post Sample Data[/url] :: Running Totals[/url] :: Tally Table[/url] :: Cross Tabs/Pivots[/url] :: String Concatenation[/url]

Viewing 15 posts - 1 through 15 (of 21 total)

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