views:

58

answers:

1

I have a script with an Alter Column statement that's changing the data type from a Bigint to a Varchar. Immediately afterwards, I need to call Substring() on that column.

Unfortunately, the syntax checker of SQL Server won't let me do this. I need to add a 'GO' statement to split the commands. The problem with this is, I also need to include IF-conditions in my script, and adding a GO statement will break the IF block so it no longer exists in the same scope and breaks the logic of my code.

Is there any way to do this:

Alter Table MyTable
Alter Column MyColumn varchar(25)

GO

Update MyTable
Set MyColumn = Substring(MyColumn, 0, 5)

Without using the word GO?

+2  A: 

Like this:

Alter Table MyTable
Alter Column MyColumn varchar(25)

EXEC('Update MyTable
Set MyColumn = Substring(MyColumn, 0, 5)');
RBarryYoung