sql - Go command Error -


i have stored procedure executing. now, tried add try catch t-sql statements.

after adding

begin try  set ansi_nulls on go set quoted_identifier on set xact_abort on go  /****** add new column accountid  metainformation table ******/  if not exists (select * syscolumns name = 'accountid' , id = object_id('[gsf].[dbo].[metainformation]')) begin     alter table [gsf].[dbo].[metainformation] add accountid int  end go    if exists (select * syscolumns name = 'accountid' , id = object_id('[gsf].[dbo].[metainformation]')) begin     update [gsf].[dbo].[metainformation]       set mp.accountid = ad.accountid     [gsf].[dbo].[metainformation] mi, [gsf].[dbo].[accountdetails] ad     mi.detailid= ad.detailid end go 

i error in go statements, shows error saying incorrect syntax near go.

any pointers or alternative use ?

updated code:

use gsf go  /****** add new column accountid  metainformation table ******/ if not exists (select * **sys.columns** name = 'accountid' , id = object_id('[gsf].[dbo].[metainformation]'))     alter table [gsf].[dbo].[metainformation] add accountid uniqueidentifier   begin transaction; begin try   set ansi_nulls on set quoted_identifier on   /****** if column accountid exists, update accountid values in metainformation accountdetails ******/  if exists (select * **sys.columns** name = 'accountid' , id = object_id('[gsf].[dbo].[metainformation]')) begin     update mp       set mp.accountid = ad.accountid     [gsf].[dbo].[metainformation] mp     **inner join**       [gsf].[dbo].[accountdetails] ad     on mp.allocationdetailid = ad.allocationdetailid end  /****** drop accountid column accountdetails ******/  if exists (select * sys.columns name = 'accountid' , id = object_id('[gsf].[dbo].[accountdetails]'))     alter table [gsf].[dbo].[accountdetails] drop column accountid    /****** add 2 new pstage values [pstagetocategory] table ******/  insert [gsf].[dbo].[pstagetocategory]    (pstage, pstagetocategoryname)    values(19,1)  insert [gsf].[dbo].[pstagetocategory]    (pstage, pstagetocategoryname)    values(21,1)    /****** drop , create new viewname view remove reference of accountid ******/  use gsf  if exists ( select * sys.views name = 'viewname') drop view viewname   declare @sql_view nvarchar(max); set @sql_view = '<view definition>'; exec sp_executesql @sql_view;  commit transaction end try  begin catch select     error_number() errornumber    ,error_severity() errorseverity    ,error_state() errorstate    ,error_procedure() errorprocedure    ,error_line() errorline    ,error_message() errormessage; end catch 

i'm using script changing schema in production .so, including try catch know error , transaction rollback if there error in script(only 1 run) in run. think makes sense ?

also, have made changes script according comments. think ?

"go not transact-sql statement; command recognized sqlcmd , osql utilities , sql server management studio code editor" -- quote msdn.

the sample code below assumes executing script ssms or 1 of other utilities.

here improvements code.

1 - try/catch code blocks need in 1 batch. therefore, go needs after block.

2 - syscolumns table depreciated , removed product eventually. see jason strate's blog article on topic.

3 - not need begin/end code block if there 1 statement after if expression.

4 - should avoid ansi 92 joins since not work in sql server 2012. see mike walsh's blog article on topic.

5 - there typo in mp.accountid used instead of mi.accountid.

6 - use semicolons @ end of statements since there has buzz being requirement in future.

in summary, use try/catch blocks when write stored procedure , want return error code calling application. sample code returns information error.

sincerely

john

--  -- use correct database , set session settings --  -- switch correct database use [gsf]; go  -- change session settings set ansi_nulls on; set quoted_identifier on; set xact_abort on; go   -- -- code block --  begin try  -- add column if not exist if not exists (select * sys.columns c c.name = 'accountid' , c.object_id = object_id('dbo.metainformation'))     alter table [dbo].[metainformation] add accountid int;  -- update column regardless if exists (select * from sys.columns c c.name = 'accountid' , c.object_id = object_id('dbo.metainformation'))     update metainformation     set accountid = ad.accountid     [dbo].[metainformation] mi join [dbo].[accountdetails] ad on mi.detailid = ad.detailid;  end try   -- -- error handling --  -- error handler begin catch     select         error_number() errornumber        ,error_severity() errorseverity        ,error_state() errorstate        ,error_procedure() errorprocedure        ,error_line() errorline        ,error_message() errormessage; end catch  go 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -