sql server - Why is this T-SQL DDL not adding a column to my table -


we have type of ddl construct (but not always) fails. construct is:

if not exists (select 'b'                   sys.columns                        inner join sys.tables b                                on a.object_id = b.object_id                  a.name = 'badgeid'                        , b.name = 'allianceincentbadges')    begin        alter table dbo.allianceincentbadges          add badgeid int not null default 0;         exec sp_executesql  n'merge allianceincentbadges using incentivebadges on allianceincentbadges.badgename = incentivebadges.badgename when matched update set badgeid = incentivebadges.badgeid;'     ;  end;   go  

if run 'alter' table first , 'exec', fine. however, if run using above construct, badgeid field not added , exec throws error missing column. use 'exec' parser doesn't throw error (the parser throw error if column doesn't exist @ parse time).

any ideas why problem crops time time?

begin try     alter table dbo.allianceincentbadges add badgeid int not null default 0;     merge allianceincentbadges using incentivebadges on allianceincentbadges.badgename = incentivebadges.badgename when matched update set badgeid = incentivebadges.badgeid; end try begin catch     if error_number() <> '2705' throw; end catch 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -