[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 769
  • Last Modified:

TSQL: how to execute multiple sql statements on a single condition, where later statements depend on execution of the first

see sample code. That does not work, because the statements are executed as a block and the second statement creates an error
-> 'Invalid column name 'person_defaultavailable''

The field isn't yet there. If first the field is created, then I can not use the same condition anymore to check if the other statements need to be executed.




IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.Columns WHERE table_name='person' AND column_name='person_defaultavailable')
    BEGIN
        ALTER TABLE person ADD person_defaultavailable bit null
        UPDATE person SET person_defaultavailable = 1
        ALTER TABLE person ALTER COLUMN person_defaultavailable bit not null
    END
GO

Open in new window

0
sybe
Asked:
sybe
1 Solution
 
vdr1620Commented:
TRY this


IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.Columns WHERE table_name='person' AND column_name='person_defaultavailable')
    BEGIN
        ALTER TABLE person ADD person_defaultavailable bit null
    END    
 GO
       
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.Columns WHERE table_name='person' AND column_name='person_defaultavailable')
   BEGIN
        UPDATE person SET person_defaultavailable = 1
        ALTER TABLE person ALTER COLUMN person_defaultavailable bit not null
   END
       
       
    
GO

Open in new window

0
 
derekkrommCommented:
IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.Columns WHERE table_name='person' AND column_name='person_defaultavailable')
    BEGIN
        ALTER TABLE person ADD person_defaultavailable bit null
    END    
 GO
       
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.Columns WHERE table_name='person' AND column_name='person_defaultavailable')
   BEGIN
		declare @SQL nvarchar(max)
		select @SQL = 'UPDATE person SET person_defaultavailable = 1;
        ALTER TABLE person ALTER COLUMN person_defaultavailable bit not null'
		exec sp_executesql @SQL
   END
       
       
    
GO

Open in new window

0
 
John ClaesCommented:
in what version of SQL are you working?

On my side with 2005/2008 it's working fine.

I'm getting the 1 in the correct column without any errors

see example below

 
create table person
(
	drft int
)
GO
insert into person select 9
insert into person select 8
insert into person select 7
insert into person select 6

IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.Columns WHERE table_name='person' AND column_name='person_defaultavailable')
    BEGIN
        ALTER TABLE person ADD person_defaultavailable bit null
        UPDATE person SET person_defaultavailable = 1
        ALTER TABLE person ALTER COLUMN person_defaultavailable bit not null
    END
GO 

select * from person
drop table person

Open in new window

0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now