alter-column

When increasing the size of VARCHAR column on a large table could there be any problems?

放肆的年华 提交于 2019-12-03 04:08:07
I'm using SQL Server 2008 and I need to make a VARCHAR field bigger, from (200 to 1200) on a table with about 500k rows. What I need to know is if there are any issues I have not considered. I will be using this TSQL statement: ALTER TABLE MyTable ALTER COLUMN [MyColumn] VARCHAR(1200) I've already tried it on a copy of the data and this statement had no ill effects that I could see. So are there any possible problems from doing this that I may not have considered? By the way, the column is not indexed. This is a metadata change only: it is quick. An observation: specify NULL or NOT NULL

Change a Nullable column to NOT NULL with Default Value

孤街浪徒 提交于 2019-12-02 19:55:07
I came across an old table today with a datetime column called 'Created' which allows nulls. Now, I'd want to change this so that it is NOT NULL, and also include a constraint to add in a default value (getdate()). So far I've got the following script, which works fine provided that i've cleaned up all the nulls beforehand: ALTER TABLE dbo.MyTable ALTER COLUMN Created DATETIME NOT NULL Is there any way to also specify the default value as well on the ALTER statement? I think you will need to do this as three separate statements. I've been looking around and everything i've seen seems to

ALTER TABLE on dependent column

故事扮演 提交于 2019-12-02 17:51:54
I am trying to alter column datatype of a primary key to tinyint from int.This column is a foreign key in other tables.So,I get the following error: Msg 5074, Level 16, State 1, Line 1 The object 'PK_User_tbl' is dependent on column 'appId'. Msg 5074, Level 16, State 1, Line 1 The object 'FK_Details_tbl_User_tbl' is dependent on column 'appId'. Msg 5074, Level 16, State 1, Line 1 The object 'FK_Log_tbl_User_tbl' is dependent on column 'appId'. Msg 4922, Level 16, State 9, Line 1 ALTER TABLE ALTER COLUMN appId failed because one or more objects access this column. Is there any other way other

Alter column, add default constraint

亡梦爱人 提交于 2019-11-28 16:15:57
I have a table and one of the columns is "Date" of type datetime. We decided to add a default constraint to that column Alter table TableName alter column dbo.TableName.Date default getutcdate() but this gives me error: Incorrect syntax near '.' Does anyone see anything obviously wrong here, which I am missing (other than having a better name for the column) Try this alter table TableName add constraint df_ConstraintNAme default getutcdate() for [Date] example create table bla (id int) alter table bla add constraint dt_bla default 1 for id insert bla default values select * from bla also make

How to ALTER multiple columns at once in SQL Server

为君一笑 提交于 2019-11-28 04:47:54
I need to ALTER the data types of several columns in a table. For a single column, the following works fine: ALTER TABLE tblcommodityOHLC ALTER COLUMN CC_CommodityContractID NUMERIC(18,0) But how do I alter multiple columns in one statement? The following does not work: ALTER TABLE tblcommodityOHLC ALTER COLUMN CC_CommodityContractID NUMERIC(18,0), CM_CommodityID NUMERIC(18,0) Neil Knight This is not possible. You will need to do this one by one. You could create a Temporary Table with your modified columns in, copy the data across, drop your original table and rename your Temporary Table to

How to ALTER multiple columns at once in SQL Server

ε祈祈猫儿з 提交于 2019-11-27 04:17:03
问题 I need to ALTER the data types of several columns in a table. For a single column, the following works fine: ALTER TABLE tblcommodityOHLC ALTER COLUMN CC_CommodityContractID NUMERIC(18,0) But how do I alter multiple columns in one statement? The following does not work: ALTER TABLE tblcommodityOHLC ALTER COLUMN CC_CommodityContractID NUMERIC(18,0), CM_CommodityID NUMERIC(18,0) 回答1: This is not possible. You will need to do this one by one. You could create a Temporary Table with your modified

Alter column, add default constraint

徘徊边缘 提交于 2019-11-27 00:07:23
问题 I have a table and one of the columns is "Date" of type datetime. We decided to add a default constraint to that column Alter table TableName alter column dbo.TableName.Date default getutcdate() but this gives me error: Incorrect syntax near '.' Does anyone see anything obviously wrong here, which I am missing (other than having a better name for the column) 回答1: Try this alter table TableName add constraint df_ConstraintNAme default getutcdate() for [Date] example create table bla (id int)

Altering a column: null to not null

自古美人都是妖i 提交于 2019-11-26 13:52:55
I have a table that has several nullable integer columns. This is undesirable for several reasons, so I am looking to update all nulls to 0 and then set these columns to NOT NULL . Aside from changing nulls to 0 , data must be preserved. I am looking for the specific SQL syntax to alter a column (call it ColumnA ) to " not null ". Assume the data has been updated to not contain nulls. Using SQL server 2000 . mdb First, make all current NULL values disappear: UPDATE [Table] SET [Column]=0 WHERE [Column] IS NULL Then, update the table definition to disallow NULLs: ALTER TABLE [Table] ALTER

Altering a column: null to not null

荒凉一梦 提交于 2019-11-26 02:59:41
问题 I have a table that has several nullable integer columns. This is undesirable for several reasons, so I am looking to update all nulls to 0 and then set these columns to NOT NULL . Aside from changing nulls to 0 , data must be preserved. I am looking for the specific SQL syntax to alter a column (call it ColumnA ) to \" not null \". Assume the data has been updated to not contain nulls. Using SQL server 2000 . 回答1: First, make all current NULL values disappear: UPDATE [Table] SET [Column]=0