tsql - Statements after END in stored procedure -


i came across interesting problem today. altering stored procedure , put select statement @ end. meant temporary , working data. surprised find out later statement got saved , executing whenever sp ran.

set ansi_nulls on go  -- comments go here , saved part of sp alter procedure [dbo].[mysp]     @param int begin     --your normal sql statements here end  --you can add sql statements here select * largetable  --you have access params select @param 

it makes sense saved, not inside begin/end, otherwise comments , set ansi_nulls, etc. disappear. i'm little confused starts where, have few questions:

  1. set ansi_nulls gets saved part of sp. have confirmed each sp has own value. how sql server know save part of sp since it's not referenced before? full scan of current environment state, when alter procedure runs saves state (possibly non-default values)?
  2. apparently begin/end optional , have no intrinsic meaning. why included then? give false sense of scope doesn't exist. seems me no begin/end , go @ end make sense.

ansi nulls , quoted identifiers stored metadata attributes of stored procedure code. can review these settings via

select * sys.sql_modules  

when procedure saved, these attributes set whatever connection through procedure being saved. can lead irritating inconsistancies, wary.

as begin/end, it's @bobs says -- denote code blocks, not denote start , end of stored procedure code. (functions, yes, procedures, no.) say, no begin/end , go @ end make sense way i've been doing years.

technically, sql (attempt to) save in batch part of stored procedure -- is, text submit, broken go statements (if any). if stuck return statement right before ad hoc queries, they'd included in code never run.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -