SQL Server stored procedure with empty body -


create procedure syntax:

create { proc | procedure } [schema_name.] procedure_name [ ; number ]        [ { @parameter [ type_schema_name. ] data_type }           [ varying ] [ = default ] [ out | output | [readonly]       ] [ ,...n ]    [ <procedure_option> [ ,...n ] ]   [ replication ]    { [ begin ] sql_statement [;] [ ...n ] [ end ] }   [;]     <sql_statement> ::=    { [ begin ] statements [ end ] }   

[ ] (brackets) optional syntax items. not type brackets.

{ } (braces) required syntax items. not type braces.

and human readable form:

enter image description here

let's try write stored procedure empty body:

create proc my_proc  -- please treat separate call, example different session exec my_proc 

is perfect valid syntax.

livedemo

so looks sql_statement empty.

now let's try same time begin/end block:

create proc my_proc begin end -- incorrect syntax near 'end'. 

livedemo2

why first example valid? if sql_statement allows nothing second example should work or doc inaccurate.

edit

well, that's because in first example isn't empty body, sp be: exec my_proc

the case show call sp. add go or use exec:

create proc my_proc go  exec my_proc 

or

exec('create proc my_proc as')  exec my_proc 

livedemo3

the syntax error not related proper syntax stored procs. proper syntax "begin/end". begin/end requires sql inside of valid. documentation begin/end shows this:

https://msdn.microsoft.com/en-us/library/ms190487.aspx

begin        {        sql_statement | statement_block         }    end   

the grammar in create proc documentation indeed not correct, says sql_statement required "create proc", when not required.


Popular posts from this blog

Apache NiFi ExecuteScript: Groovy script to replace Json values via a mapping file -

node.js - How do I prevent MongoDB replica set from querying the primary? -

python 3.x - PyQt5 - Signal : pyqtSignal no method connect -