SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


CREATE statement


CREATE statement

Author
Message
chgn01
chgn01
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2229 Visits: 1950
Comments posted to this topic are about the item CREATE statement

--------------------------------------
;-)“Everything has beauty, but not everyone sees it.” ― Confucius
Danny Ocean
Danny Ocean
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2752 Visits: 1549
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 !!!
www.GrowWithSql.com
Lokesh Vij
Lokesh Vij
SSCarpal Tunnel
SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4392 Visits: 1599
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
Guidelines for answers on Performance questions

Link to my Blog Post --> www.SQLPathy.com

Follow me @Twitter


Lokesh Vij
Lokesh Vij
SSCarpal Tunnel
SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4392 Visits: 1599
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
Guidelines for answers on Performance questions

Link to my Blog Post --> www.SQLPathy.com

Follow me @Twitter


demonfox
demonfox
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2753 Visits: 1192
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
demonfox
demonfox
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2753 Visits: 1192
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 BigGrin , With IF condition :-P

~ demonfox
___________________________________________________________________
Wondering what I would do next , when I am done with this one Ermm
Lokesh Vij
Lokesh Vij
SSCarpal Tunnel
SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4392 Visits: 1599
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
Guidelines for answers on Performance questions

Link to my Blog Post --> www.SQLPathy.com

Follow me @Twitter


Lokesh Vij
Lokesh Vij
SSCarpal Tunnel
SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4392 Visits: 1599
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 BigGrin , With IF condition :-P


+1
Drop statement can be also be used :-)

~ Lokesh Vij

Guidelines for quicker answers on T-SQL question
Guidelines for answers on Performance questions

Link to my Blog Post --> www.SQLPathy.com

Follow me @Twitter


Danny Ocean
Danny Ocean
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2752 Visits: 1549
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 !!!
www.GrowWithSql.com
Lokesh Vij
Lokesh Vij
SSCarpal Tunnel
SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4392 Visits: 1599
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
Guidelines for answers on Performance questions

Link to my Blog Post --> www.SQLPathy.com

Follow me @Twitter


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search