?
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
Medium Priority
?
765 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 2000 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
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.

770 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