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
759 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 16

Expert Comment

by:vdr1620
ID: 35017675
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
ID: 35017719
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
ID: 35017735
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

738 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