Solved

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

Posted on 2011-03-02
3
750 Views
Last Modified: 2012-08-14
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
Comment
Question by:sybe
3 Comments
 
LVL 16

Expert Comment

by:vdr1620
Comment Utility
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
 
LVL 15

Expert Comment

by:derekkromm
Comment Utility
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
 
LVL 10

Accepted Solution

by:
John Claes earned 500 total points
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now