Re: ALTER COLUMN

From: Louis Davidson (dr_dontspamme_sql_at_hotmail.com)
Date: 01/26/05


Date: Wed, 26 Jan 2005 09:43:19 -0600

If you want to make sure that both alters work, or neither, you can use a
transaction:

drop table test
go
create table test
(
 column1 varchar(10) not null,
 column2 varchar(10) not null
)
go

insert into test
values (1,10)
insert into test
values (1,'FRED')
go
select * from test
go
/*
returns
column1 column2
---------- ----------
1 10
1 FRED
*/
go

begin transaction
alter table test
 alter column column1 int not null
alter table test
 alter column column2 int
commit transaction
go
/* error returned:

Server: Msg 245, Level 16, State 1, Line 1
Syntax error converting the varchar value 'FRED' to a column of data type
int.
The statement has been terminated.
*/
go
select * from test --doesn't change

-- 
----------------------------------------------------------------------------
Louis Davidson - drsql@hotmail.com
SQL Server MVP
Compass Technology Management - www.compass.net
Pro SQL Server 2000 Database Design - 
http://www.apress.com/book/bookDisplay.html?bID=266
Note: Please reply to the newsgroups only unless you are interested in 
consulting services.  All other replies may be ignored :)
"Hugo Kornelis" <hugo@pe_NO_rFact.in_SPAM_fo> wrote in message 
news:1mdfv0ll9tnrvh32qt4oejgrvpobsg3ah7@4ax.com...
> On Wed, 26 Jan 2005 10:20:15 -0500, Vik wrote:
>
>>What is correct syntax to alter a few columns in one query like this:
>>
>>ALTER TABLE dbo.Inventory
>>ALTER COLUMN
>> Fld1 varchar(50) NULL,
>> Fld2 int NULL
>>
>>I get "Incorrect syntax near ',' " error message on this query.
>
> Hi Vik,
>
> You can alter only one caolumn at a time.
>
> ALTER TABLE dbo.Inventory
> ALTER COLUMN
> Fld1 varchar(50) NULL
> ALTER TABLE dbo.Inventory
> ALTER COLUMN
> Fld2 int NULL
>
> Best, Hugo
> -- 
>
> (Remove _NO_ and _SPAM_ to get my e-mail address) 


Relevant Pages

  • Re: "Invalid Cursor Error"
    ... ALTER TABLE is fine as long ... >> problem is and spending money on this seems absurd. ... Just plain simple Enterprise Manager. ... > SET TRANSACTION ISOLATION LEVEL SERIALIZABLE ...
    (microsoft.public.sqlserver.server)
  • Re: UPDATE and ALTER in transactions do not mix
    ... > I am trying to update a system by issuing an UPDATE and then an ALTER ... > a transaction I always get that the table is in use by another user. ... sensible to me that you can't ALTER a table that has a lock on it. ...
    (microsoft.public.access.tablesdbdesign)
  • Re: UPDATE and ALTER in transactions do not mix
    ... > Jonathan Scott via AccessMonster.com wrote: ... >> be touched once inside of a single transaction? ... > What locking strategy are you using? ... Of course it works the other way round: the ALTER doesn't take out any ...
    (microsoft.public.access.tablesdbdesign)
  • Re: How to Halt a Schema Change
    ... if I use ALTER TABLE it will run in a flash? ... The table is probably huge,and by doing it via SSMS has an impact as SQL ... It's now rolling back, according to Activity Monitor. ... If you stop a transaction it will probably take as long to roll back as ...
    (microsoft.public.sqlserver.server)
  • Re: Add a coulm in a table, after a specific column
    ... should be one big transaction. ... That or just restore a backup if there ... o Remove all "go" in the script, and wrap most statements in EXEC. ... before the ALTER TABLE statement put always some ...
    (comp.databases.ms-sqlserver)