March 6, 2013 at 8:34 pm
Comments posted to this topic are about the item CREATE statement
--------------------------------------
;-)βEverything has beauty, but not everyone sees it.β β Confucius
March 6, 2013 at 10:24 pm
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 !!!
March 6, 2013 at 10:35 pm
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
Link to my Blog Post --> www.SQLPathy.com[/url]
Follow me @Twitter
March 6, 2013 at 10:36 pm
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
Link to my Blog Post --> www.SQLPathy.com[/url]
Follow me @Twitter
March 6, 2013 at 11:17 pm
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:
March 6, 2013 at 11:39 pm
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:
March 6, 2013 at 11:49 pm
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
Link to my Blog Post --> www.SQLPathy.com[/url]
Follow me @Twitter
March 6, 2013 at 11:50 pm
demonfox (3/6/2013)
the explanations saysThe 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
Link to my Blog Post --> www.SQLPathy.com[/url]
Follow me @Twitter
March 7, 2013 at 12:03 am
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 !!!
March 7, 2013 at 12:17 am
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
Link to my Blog Post --> www.SQLPathy.com[/url]
Follow me @Twitter
March 7, 2013 at 12:58 am
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:
March 7, 2013 at 2:04 am
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 !!!
March 7, 2013 at 2:30 am
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
March 7, 2013 at 2:39 am
This was removed by the editor as SPAM
March 7, 2013 at 4:48 am
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