Alter Text Column

Apr 19, 2006

Hi

I had a text type not null column which i wanted to change to a null column.Writing a simple alter statement gave me an eror cannot change text type column so i tried to rename the original column create a new column with the same name and allowing nulls on it and then copying the contents of the renamed column to the new column and finally deleting the renamed column.

EXEC sp_rename 'TableName.ColumnName', 'ColumnName_old', 'COLUMN'

ALTER TABLE TableName ADD ColumnName text NULL
UPDATE TableName  SET ColumnName = ColumnName_old
ALTER TABLE TableName   DROP COLUMN ColumnName_old



However when i  tried to execute these statements in query analyser on the Update statement it gave me the error that ColumnName_old does not exist.

However then I tried to execute these queries one by one I was able to do that.

Can anybody tell me whats causing the queries to not be executed all at once without giving the ColumnName_old does not exist error cause I wanted to run them on live dbs.

 

any help would be appreciated.

Himani

 

View 2 Replies


ADVERTISEMENT

TSQL - Using ALTER TABLE - ALTER COLUMN To Modify Column Type / Set Identity Column

Sep 7, 2007

Hi guys,
If I have a temporary table called #CTE
With the columns
[Account]
[Name]
[RowID Table Level]
[RowID Data Level]
and I need to change the column type for the columns:
[RowID Table Level]
[RowID Data Level]
to integer, and set the column [RowID Table Level] as Identity (index) starting from 1, incrementing 1 each time.
What will be the right syntax using SQL SERVER 2000?

I am trying to solve the question in the link below:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2093921&SiteID=1

Thanks in advance,
Aldo.

I have tried the code below, but getting syntax error...



ALTER TABLE #CTE
ALTER COLUMN
[RowID Table Level] INT IDENTITY(1,1),
[RowID Data Level] INT;


I have also tried:

ALTER TABLE #CTE
MODIFY
[RowID Table Level] INT IDENTITY(1,1),
[RowID Data Level] INT;







View 18 Replies View Related

Altered Table-changed Column From Text To Varchar And Now Having Issues With Alter Switch

Feb 14, 2008

Working on partitioning a few large tables. One of the tables included a text column and the €œTEXTIMAGE_ON [PRIMARY]€? clause which would prevent the partitioning of this table. After some research we found that the data was legacy and no longer used. We updated the column on the affected rows to NULLS and altered the column to a VARCHAR(20)
When I attempted to run the ALTER TABLE SWITCH I encountered the error
Msg 4947, Level 16, State 1, Line 1
ALTER TABLE SWITCH statement failed. There is no identical index in source table 'LocalDeltanet.dbo.testresultsjoe' for the index 'PKIDX_testSummary' in target table 'LocalDeltanet.dbo.testresults_part'.
After a lot of grief and testing I determined that the message was bogus and the real issue is that the 'sys.tables' still has €œlob_data_space_id€? with a value of 1 for this table.
I created a copy of the table with the text column altered to varchar and one with just the varchar to begin with. After copying data from the original table, I tried to run the alter switch. It failed once again for the text column altered to varchar table, but it worked for the varchar from the start.

Since it appears that this value is causing my issues, is there anyway to update the table in place. I know I can BCP the data out, but that would take too long and would defeat the advantage of using the alter switch method.

BOL States:

The allow updates option is still present in the sp_configure stored procedure, although its functionality is unavailable in Microsoft SQL Server 2005 (the setting has no effect). In SQL Server 2005, direct updates to the system tables are not supported. This means we cannot update the table manually.

Thanks

View 1 Replies View Related

Alter Table Alter Column In MSACCESS. How Can I Do It For A Decimal Field?

Jul 23, 2005

Hi people,I?m trying to alter a integer field to a decimal(12,4) field in MSACCESS 2K.Example:table : item_nota_fiscal_forn_setor_publicofield : qtd_mercadoria integer NOT NULLALTER TABLE item_nota_fiscal_forn_setor_publicoALTER COLUMN qtd_mercadoria decimal(12,4) NOT NULLBut, It doesn't work. A sintax error rises.I need to change that field in a Visual Basic aplication, dinamically.How can I do it? How can I create a decimal(12,4) field via script in MSACCESS?Thanks,Euler Almeida--Message posted via http://www.sqlmonster.com

View 1 Replies View Related

Alter Table Alter Column

Jul 20, 2005

I would like to add an Identity to an existing column in a table using astored procedure then add records to the table and then remove the identityafter the records have been added or something similar.here is a rough idea of what the stored procedure should do. (I do not knowthe syntax to accomplish this can anyone help or explain this?Thanks much,CBLCREATE proc dbo.pts_ImportJobsas/* add identity to [BarCode Part#] */alter table dbo.ItemTestalter column [BarCode Part#] [int] IDENTITY(1, 1) NOT NULL/* add records from text file here *//* remove identity from BarCode Part#] */alter table dbo.ItemTestalter column [BarCode Part#] [int] NOT NULLreturnGOSET QUOTED_IDENTIFIER OFFGOSET ANSI_NULLS ONGOhere is the original tableCREATE TABLE [ItemTest] ([BarCode Part#] [int] NOT NULL ,[File Number] [nvarchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULLCONSTRAINT [DF_ItemTest_File Number] DEFAULT (''),[Item Number] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULLCONSTRAINT [DF_ItemTest_Item Number] DEFAULT (''),[Description] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULLCONSTRAINT [DF_ItemTest_Description] DEFAULT (''),[Room Number] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULLCONSTRAINT [DF_ItemTest_Room Number] DEFAULT (''),[Quantity] [int] NULL CONSTRAINT [DF_ItemTest_Quantity] DEFAULT (0),[Label Printed Cnt] [int] NULL CONSTRAINT [DF_ItemTest_Label Printed Cnt]DEFAULT (0),[Rework] [bit] NULL CONSTRAINT [DF_ItemTest_Rework] DEFAULT (0),[Rework Cnt] [int] NULL CONSTRAINT [DF_ItemTest_Rework Cnt] DEFAULT (0),[Assembly Scan Cnt] [int] NULL CONSTRAINT [DF_ItemTest_Assembly Scan Cnt]DEFAULT (0),[BarCode Crate#] [int] NULL CONSTRAINT [DF_ItemTest_BarCode Crate#] DEFAULT(0),[Assembly Group#] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULLCONSTRAINT [DF_ItemTest_Assembly Group#] DEFAULT (''),[Assembly Name] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULLCONSTRAINT [DF_ItemTest_Assembly Name] DEFAULT (''),[Import Date] [datetime] NULL CONSTRAINT [DF_ItemTest_Import Date] DEFAULT(getdate()),CONSTRAINT [IX_ItemTest] UNIQUE NONCLUSTERED([BarCode Part#]) ON [PRIMARY]) ON [PRIMARY]GO

View 2 Replies View Related

Alter Table Alter Column...

Oct 8, 2007

I am using sql server ce.I am changing my tables sometimes.how to use 'alter table alter column...'.for example:I have table 'customers', I delete column 'name' and add column 'age'.Now I drop Table 'customers' and create again.but I read something about 'alter table alter column...'.I use thi command but not work.I thing syntax not true,that I use..plaese help me?



my code:
Alter table customers alter column age

View 7 Replies View Related

ALTER TABLE DROP COLUMN LastUpdated Failed Because One Or More Objects Access This Column.

Mar 7, 2008

Hi I’m trying to alter a table and delete a column I get the following error. The object 'DF__Morningst__LastU__19EB91BA' is dependent on column 'LastUpdated'.
ALTER TABLE DROP COLUMN LastUpdated failed because one or more objects access this column. I tried deleting the concerned constraint. But the next time I get the same error with a different constraint name. I want to find out if I can dynamically check the constraint name and delete it and then drop the column. Can anyone help.IF EXISTS(SELECT 1FROM sysobjects,syscolumnsWHERE sysobjects.id = syscolumns.idAND sysobjects.name = TablenameAND syscolumns.name = column name)BEGIN EXECUTE ('ALTER TABLE tablename DROP CONSTRAINT DF__SecurityM__DsegL__08C105B8')EXECUTE ('ALTER TABLE tablenameDrop column columnname)ENDGO
 

View 1 Replies View Related

Alter Column Results In Incorrect Syntax Near 'column'

Oct 29, 2001

I ran this query against the pubs database and it runs successfully

ALTER TABLE publishers ALTER COLUMN state CHAR(25)

I change the table & field names for my db as follows:
ALTER TABLE customquery ALTER COLUMN toclause CHAR(25)

and run against my database and I get the following error - Incorrect syntax near 'COLUMN'.

My column name is correct - I don't know why it would run fine against pubs, but not my db. I do not have quoted identifiers turned on. I have tried using [] around my column name [toclause], but that didn't change anything. Any help would be appreciated.
Thanks.

View 1 Replies View Related

Alter A Column To Be The Table Identity Column

Aug 3, 2006

i have a table
table1
column1 int not null
column2 char not nul
column3 char

i want to script a change for table1 to alter column1 to be the table identity column. not primary.

View 5 Replies View Related

Does Alter Column Do Work If Column Already The Same As Desired?

Jul 23, 2005

I have a table with a column defined thus: LOCNumber Varchar(100) in atable called Bookdata.If I execute ALTER TABLE BookData ALTER COLUMN LOCNumber varchar(100)on the table, does any work get done on the table and column?Or does SqlServer know that the column is already varchar(100) andnothingneeds to be done?If it does do some work, how do you tell it not to run this as it isalready a varchar(100) column. Right now my program just blindly doesthis ALTER TABLEstatement regardless of any conditions. Is this a potential problem?Thanks for any help.

View 2 Replies View Related

Alter Table Add Column - How Do You Add A Column After Say The Second Column

Jul 11, 2007



When you use "Alter Table add Column", it adds the column to the end of the list of fields.



How do you insert the new column to position number 2 for instance given that you may have more than 2 columns?



Create table T1 ( a varchar(20), b varchar(20), c varchar(20))



Alter table add column x varchar(20)

so that the resulting table is

T1 a varchar(20), x varchar(20), b varchar(20), c varchar(20)



Can this be done programmatically?













View 33 Replies View Related

Alter Column B/w Desired Column

Jun 14, 2006

if i have structure of table like a,b,c,d,e column then

i want to alter one more column x b/w b and c column, so that

a,b,x,c,d,e

View 4 Replies View Related

Cannot Set Default Value For Column Using Alter Column

May 16, 2006

Well here's one of those excruciatingly simple obstacles:

In SQL Server 2005 (Mgmt Studio): according to BOL, the syntax to set a default value for an existing column is:

ALTER TABLE MyCustomers ALTER COLUMN CompanyName SET DEFAULT 'A. Datum Corporation'

However, when I Check:

alter table CommissionPayment alter column Amount Set Default 0

I get the error message:

"Incorrect syntax near the keyword 'Set'."

No other combinations of this syntax work.

Help! What am I missing?

View 4 Replies View Related

Alter Column

Jul 15, 2002

Hi smart people!

I would like to know how to alter a column to have a default value. For instance I have a column AreaCode Char(3) in a table. I have data in the table and now I want to add a default value of '123' to the AreaCode column.

I tried the following but did not work.
Alter Table Phone
Alter Column AreaCode Char(3) Default '123'

Can we even do it using SQL?

Thanks

View 1 Replies View Related

Alter Column Name

Jul 12, 2001

Is there any way to alter a column's name using Transact SQL and not the GUI interface?

View 1 Replies View Related

Alter Column

Mar 15, 2006

what is the syntax for multiple column modifications ?

alter table abc
alter column x1 nvarchar(10) null

-- works


alter table abc
alter column x1 nvarchar(10) null,
alter column x2 nvarchar(10) null

-- doesn't work or do i have to use the alter table each time ?

View 1 Replies View Related

How To Alter A Column??

Nov 15, 2007

Hi,
I am new to SQL Server 2005. Please help
what is the problem with this code..?

ALTER TABLE AM_Master
ALTER COLUMN Last_Updated datetime NOT NULL DEFAULT getdate();
Go

The error is like

Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'DEFAULT'.

View 8 Replies View Related

ALTER COLUMN

Oct 31, 2007

Hey

I am using postgres, I see different ways to add/change a data type to a column - whats right?

ALTER TABLE table1 ALTER COLUMN SET....??

:)

View 12 Replies View Related

Alter Column Name From SQL?

Oct 13, 2006

How can you rename a column in a table (from c# code, preferrably from a SQL script command) without deleting the column and re-creating it with a new name?

View 5 Replies View Related

Alter Column To Identity

Aug 31, 2001

i am altering an int column to identity:
where is something wrong here?

error:

Server: Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'IDENTITY'.

statement:

ALTER TABLE Navigation Alter column [Navigation_ID] int IDENTITY(1,1)

View 1 Replies View Related

Alter An Identity Column

Feb 5, 2003

I'm attempting to remove the IDENTITY property from a column using the ALTER command but can't seem to find the right syntax. I've tried every conceivable combination I can think of. I can add the property to a column that doesn't have it.

It's easy to do in Enterprise Mgr., but I want to script the change.

I really don't want to drop the table and rebuild if I don't have to.

Sidney Ives

View 3 Replies View Related

Alter Column Set Default

Oct 11, 2006

I've done some research on setting the default value of a column and found the following code:

ALTER TABLE [table1] ALTER COLUMN [column1] SET DEFAULT 0

I am trying to set column1 within table1 to a default value of 0, column1 is an int data type. But it doesn't work within MS SQL SERVER 2000.

Can anyone tell me what's wrong?

View 2 Replies View Related

Alter Column To Add IDENTITY

May 8, 2008

Hello All,
There is a table A in SQL. One of the column of A was IDENTITY before and was getting referenced in other table B.

During migration to SQL 2005, I removed the IDENTITY constraint and imported all the data from 2000 to 2005. Now I have to deploy the IDENTITY back to the column. I am getting an error while executing the code:

ALTER TABLE TableA
ALTER COLUMN ColumnA
ADD CONSTRAINT IDENTITY(445,1)


Can anyone tell me where I am going wrong? Or if there is any other way to do it. There are 50+ tables I need to do the same.

Thanks,
-S

View 1 Replies View Related

Alter A Column To Add (not Null)

Oct 30, 2013

How do you alter a table to set a column which is currently int, to int not null?

I have already set all values to either 0 or 1.

Why can't I use:

Alter table myTable alter myCol int not null default(0)

I get a syntax error on the word default. If I remove "default(0)" then the command executes ok, but my Inserts don't work because there's no default value.

View 4 Replies View Related

ALTER TABLE - ADD Column

Sep 27, 2005

I have the table table_x:
col1 INT PRIMARY KEY
col2 VARCHAR

I want to add col3 between col1 and col2. In MySQL it's done with the following query:
ALTER TABLE table_x ADD col3 VARCHAR( 10 ) NOT NULL AFTER col1 ;

In sql server all i can do is to add the column to the end of table. Is there a way to insert a new column in the middle or to move a column to left/right?

View 5 Replies View Related

How To Alter An IDENTITY Column

Feb 27, 2008

hello experts,

Any body know what is the query for modifying the identity column in a table? i mean modifying from IDENTITY column to int colum or VICE-VERSA

Thanks
Muthu

View 3 Replies View Related

Generation Of Sql For An Alter Column Etc

Mar 24, 2006

Hi.I have a database I need to supply something (I'm assuming a t-sql script..maybe something else is better) to update customer tables with.The operations include mostly changing varchar lengths, though a couple ofcolumns were renamed.I'd like to maybe figure out how to get Enterprise Manager or Query Analyzerto generate the scripts.I can't just send alter table scripts because I'm involving all sorts ofconstraints that have to be disabled/or dropped, the alter made, then havethem enabled/ or re-created.Basically I'm hoping to get the tools to do the rather large amount of workfor me. I'm targetting sql server 2000.Can someone make a knowledgeable suggestion?RegardsJeff Kish

View 4 Replies View Related

Alter Column Type.

Jul 20, 2005

Hello,Easy one for the SQL experts.I have a simple table. For the example let's say it looks like this:CREATE TABLE doc_exb ( column_a INT, column_b VARCHAR(20) NULL)Now I want to alter this table and make the column column_a a floatinstead of an INT.How do I do that? I cannot DROP and ADD the column I would lose somevery precious data.Thanks a lot

View 1 Replies View Related

How To Alter Column To Identity(1,1)

Jun 18, 2007

I tried



Alter table table1 alter column column1 Identity(1,1);

Alter table table1 ADD CONSTRAINT column1_def Identity(1,1) FOR column1



they all can not work,

any idea about this? thanks


View 24 Replies View Related

Alter Table Column

Feb 20, 2008

I would like to alter a table column from char(10) to char(5). I issue the command as :

Alter table mytable alter column mycolumn char(5)

It returns :

Server: Msg 8152, Level 16, State 2, Line 1
String or binary data would be truncated.


How can I perform this task?
Thanks,
Joe.

View 1 Replies View Related

Alter Column To Set Default

Aug 23, 2006

I know that the correct syntax to set the default on a column in SQL Server 2005 is:

Alter Table <TableName> Add Constraint <ConstraintName> Default <DefaultValue> For <ColumnName>

But from what I can gather, the SQL-92 syntax is:

Alter Table <TableName> Alter Column <ColumnName> Set Default <DefaultValue>

This generates an error on SQL Server 2005.

Am I wrong about the standard syntax for this statement? If this is the standard, why doesn't SQL Server 2005 support it? I am trying to avoid code that will only work on certain database managers.



Thanks.

View 1 Replies View Related

Alter Column Name Question

Oct 2, 2007

is there a way to alter a column name in a table? is it possible?

View 1 Replies View Related

Alter Column With Data

Nov 8, 2006

I am trying to use T-SQL to alter a column with data already in it from char to varbinary. This is very easy to do in Enterprise Manager, but just for my own knowledge I'm trying to figure out how to do this in T-SQL. I don't mind losing the data (I'm using a temp table to bring the converted data back in), but I want to keep the column in the same place. Here's what I have so far, but I keep getting an implicit conversion error:

UPDATE UserProfile

SET PassID = CAST(PassID AS VARBINARY(128))
GO
ALTER TABLE UserProfile

ALTER COLUMN PassID VARBINARY(128)
GO

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved