CREATE statement

  • Comments posted to this topic are about the item CREATE statement

    --------------------------------------
    ;-)β€œEverything has beauty, but not everyone sees it.” ― Confucius

  • First of all. Thanks for good question and trying to confuse uses πŸ™‚ .

    If you check msdn, then you will find that every create store procedure and create function, always contains a "GO" statement before the definition.

    GO statement will use to create a separate batch. Check below link.

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

    Good try and keep it up. πŸ™‚

    Thanks
    Vinay Kumar
    -----------------------------------------------------------------
    Keep Learning - Keep Growing !!!

  • Nice question Gary!!

    This is how Create procedure and function should be written, so that they work:

    -- Query #4

    USE DBDB;

    GO

    IF OBJECT_ID('dbo.uspUSP') IS NULL

    EXEC ('CREATE PROCEDURE dbo.uspUSP AS SET NOCOUNT OFF')

    GO

    -- Query #6

    USE DBDB;

    GO

    IF OBJECT_ID('dbo.udfUDF') IS NULL

    EXEC ('CREATE FUNCTION dbo.udfUDF(@f int) RETURNS INT AS BEGIN SET @f=1 RETURN @f END ')

    GO

    ~ Lokesh Vij


    Guidelines for quicker answers on T-SQL question[/url]
    Guidelines for answers on Performance questions

    Link to my Blog Post --> www.SQLPathy.com[/url]

    Follow me @Twitter

  • Danny Ocean (3/6/2013)


    If you check msdn, then you will find that every create store procedure and create function, always contains a "GO" statement before the definition.

    Danny, I did not get you. Please elaborate.

    Thanks!

    ~ Lokesh Vij


    Guidelines for quicker answers on T-SQL question[/url]
    Guidelines for answers on Performance questions

    Link to my Blog Post --> www.SQLPathy.com[/url]

    Follow me @Twitter

  • did I miss something ???

    or there are more than 2 failures ...

    select db_id('dbdb')

    -- and the second one

    select object_id('dbdb')

    I don't think it will return the same value for a database.

    I dig a little bit on the Object_id for a reference .. and in msdn

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

    it says

    OBJECT_ID Returns the database object identification number of a schema-scoped object.

    It would return NULL ..

    so the query

    USE master;

    GO

    IF OBJECT_ID('DBDB') IS NULL CREATE DATABASE DBDB

    -- it should be IF DB_ID('DBDB') IS NULL CREATE DATABASE DBDB

    GO

    it would fail, if the database already exist..

    EDIT : Okay, in the question , the first query drops the database .. so , it would be fine in this Qotd , but in general.. I think it would be an information while using it in the code..

    ~ demonfox
    ___________________________________________________________________
    Wondering what I would do next , when I am done with this one :ermm:

  • the explanations says

    The CREATE PROCEDURE statement cannot be combined with other Transact-SQL statements in a single batch. The same rule applies to Functions as well, but this is not noted in MSDN.

    simplifying it a little bit .. "Create Procedure" can't be inside a BEGIN..END statement..

    But Drop Procedure can be

    so this will work..

    IF OBJECT_ID('dbo.uspUSP') IS not NULL

    drop PROCEDURE dbo.uspUSP

    GO

    so, as lokesh says .. Create a proc using dynamic query πŸ˜€ , With IF condition πŸ˜›

    ~ demonfox
    ___________________________________________________________________
    Wondering what I would do next , when I am done with this one :ermm:

  • demonfox (3/6/2013)


    select db_id('dbdb')

    -- and the second one

    select object_id('dbdb')

    Nice catch demonfox....I misread and thought both are using DB_ID() πŸ™‚

    ~ Lokesh Vij


    Guidelines for quicker answers on T-SQL question[/url]
    Guidelines for answers on Performance questions

    Link to my Blog Post --> www.SQLPathy.com[/url]

    Follow me @Twitter

  • demonfox (3/6/2013)


    the explanations says

    The CREATE PROCEDURE statement cannot be combined with other Transact-SQL statements in a single batch. The same rule applies to Functions as well, but this is not noted in MSDN.

    simplifying it a little bit .. "Create Procedure" can't be inside a BEGIN..END statement..

    But Drop Procedure can be

    so this will work..

    IF OBJECT_ID('dbo.uspUSP') IS not NULL

    drop PROCEDURE dbo.uspUSP

    GO

    so, as lokesh says .. Create a proc using dynamic query πŸ˜€ , With IF condition πŸ˜›

    +1

    Drop statement can be also be used πŸ™‚

    ~ Lokesh Vij


    Guidelines for quicker answers on T-SQL question[/url]
    Guidelines for answers on Performance questions

    Link to my Blog Post --> www.SQLPathy.com[/url]

    Follow me @Twitter

  • Lokesh Vij (3/6/2013)


    Danny Ocean (3/6/2013)


    If you check msdn, then you will find that every create store procedure and create function, always contains a "GO" statement before the definition.

    Danny, I did not get you. Please elaborate.

    Thanks!

    Lokesh, If you check below links then you will find that before creating store procedure and function there is a "GO" keyword.

    GO is used to separate two batches.

    Check below two examples. Example 1 generate an error like 'CREATE/ALTER PROCEDURE' must be the first statement in a query batch. But example 2 executes successfully.

    ---- Example 1

    select 1

    Create proc Test

    AS

    select 2

    GO

    ---- Example 2

    select 1

    GO

    Create proc Test

    AS

    select 2

    http://msdn.microsoft.com/en-us/library/ms187926(v=sql.100).aspx

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

    I hope this will help you. πŸ™‚

    Thanks
    Vinay Kumar
    -----------------------------------------------------------------
    Keep Learning - Keep Growing !!!

  • Thank you Danny! I know how GO is used as a batch separator πŸ™‚

    Actually the confusion was this statement

    "every create store procedure and create function, always contains a "GO" statement before the definition."

    Reading this, I thought that "GO" is embedded inside create procedure and create function statements automatically........."human craziness, keeps them thinking in all wacky ways :-D"

    I am clear now.

    Thank! Much Appreciated..

    ~ Lokesh Vij


    Guidelines for quicker answers on T-SQL question[/url]
    Guidelines for answers on Performance questions

    Link to my Blog Post --> www.SQLPathy.com[/url]

    Follow me @Twitter

  • Lokesh Vij (3/7/2013)


    Thank you Danny! I know how GO is used as a batch separator πŸ™‚

    Actually the confusion was this statement

    "every create store procedure and create function, always contains a "GO" statement before the definition."

    Reading this, I thought that "GO" is embedded inside create procedure and create function statements automatically........."human craziness, keeps them thinking in all wacky ways :-D"

    I am clear now.

    Thank! Much Appreciated..

    I think , it was the word "Always" accountable for the confusion , if inferred as a general understanding.... GO is actually not required to create a stored procedure .. it is just used as a batch separater.. and hence the error as stated by Vinay..

    ~ demonfox
    ___________________________________________________________________
    Wondering what I would do next , when I am done with this one :ermm:

  • Lokesh Vij (3/7/2013)


    Thank you Danny! I know how GO is used as a batch separator πŸ™‚

    Actually the confusion was this statement

    "every create store procedure and create function, always contains a "GO" statement before the definition."

    Reading this, I thought that "GO" is embedded inside create procedure and create function statements automatically........."human craziness, keeps them thinking in all wacky ways :-D"

    I am clear now.

    Thank! Much Appreciated..

    Your welcome !!! πŸ™‚

    Thanks
    Vinay Kumar
    -----------------------------------------------------------------
    Keep Learning - Keep Growing !!!

  • Nice and easy for me thank you.

    β€œWhen I hear somebody sigh, β€˜Life is hard,’ I am always tempted to ask, β€˜Compared to what?’” - Sydney Harris

  • This was removed by the editor as SPAM

  • Good question.

    This "create proc (or create function) must be the first thing in the batch" nonsense has long been one of my pet hates. Another is that proc bodies are not delimited, the only thing that can define the end of a proc definition is the end of the batch. These two features are, I believe, the antithesis of sensible language design.

    Tom

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

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