I'm trying to clean up a database table and I'd really like to drop two columns as they should no longer be being used.
'Property' table
- Unable to modify table.
The index '_dta_index_Property_7_669245439__K1_K9_K8_K24_K4_2_5_6_13_22_25_26_29' is dependent on column 'AveragePriceMta'.
The index '_dta_index_Property_7_669245439__K1_K9_K8_K24_2_4_5_6_7_13_22_25_26_29' is dependent on column 'AveragePriceMta'.
ALTER TABLE DROP COLUMN AveragePriceMta failed because one or more objects access this column.
I've gone and looked at the indexes for this table found the particular columns I want to delete in a greyed out field of "Included columns". Obviously I don't want to just drop these indexes - but is there a way to refresh the index so that I can remove the columns in question out of the non-editable included columns field?
Using SQL Server 2008 but database is 2005.. in case that matters.
Thanks for your help! :)
You can't add or remove columns to an Index. You will have to drop the index and the re-create it.
You can you use Create Index along with the Drop_Existing clause to do this.
MS Help on Create Index
Related
I need to drop all columns in an existing table in SQL Server. It is possible to delete columns by specifying each column name. I want to delete every column without specifying column name. I am looking for something like
ALTER TABLE tblUser DROP COLUMN *;
Is there any known way to do this?
Answering your question literally, no you can't. If you try to remove the last column, SQL Server will throw the following error:
Msg 4923, Level 16, State 1, Line 12
ALTER TABLE DROP COLUMN failed because 'Id' is the only data column in table 'NoColumns'. A table must have at least one data column.
As such, if you actually want to solve your problem, whatever it is, it would be best to voice the initial problem and not the solution you decided to pursue.
Instead remove all the columns, you could drop the table
DROP TABLE TABLENAME
Or You can mention all the column names in Alter query
ALETR TABLE TableName DROP COLUMN Column1, Column2, Column3....ColumnN
is that possible to do a computed column without affecting existing data?
I have a unique index column named
"BookingNo"
For newly insert Column I want it to be following this format
(format([CreationTime],'yyMMddHHmmssff'))
I tried used Computed Column but it modified all my existing data.
my existing BookingNo format was
201800123
Is there anyway to generate via database? Or we have insert via code?
You could just add a default constraint:
ALTER TABLE TableName
ADD CONSTRAINT DF_BookingNo DEFAULT(format(SYSDATETIME(),'yyMMddHHmmssff'))
FOR BookingNo
This way you will get the value only for newly created rows.
Please note that if this column has a unique constraint on it some inserts might fail if they are executed at the same time.
I just have a table that has two rows of data and want to delete the rows and SQL Server Management Studio is choking on my delete big time. Here is the error pop up.
All I'm doing is highlighting the row and clicking the delete key. This error doesn't make any sense to me at all! The only way I've been able to get rid of the rows is to drop and recreate the tables but that isn't acceptable down the road when I only want to remove one or two rows.
You have to add Primary Key to your table. Run this two queries
First add a candidate column:
ALTER TABLE dbo.tbl_admin ADD TempID int IDENTITY(1, 1);
Then make this column the table primary key
ALTER TABLE dbo.tbl_admin
ADD CONSTRAINT pk_tbl_admin
PRIMARY KEY (TempId);
What I'm trying to do is make a copy of a table using a SELECT INTO statement.
After the table is created I want to duplicate the indexes as well.
So the code I'm using is as follows:
SELECT * INTO TableCopy FROM Table
Then:
ALTER TABLE TableCopy ADD CONSTRAINT pkGUID PRIMARY KEY ([GUID])
CREATE INDEX ixIndexName ON TableCopy (CountryCode)
When I execute the index SQL, I get an error that the indexes already exist in the catalog. I didn't think index names had to be unique, I thought they could be duplicated across different tables.
And lo and behold if I create the indexes through management studio, it accepts the index names.
What am I missing here?
Thanks.
I didn't think index names had to be unique, I thought they could be duplicated across different tables.
No. They do have to be unique within a table/view.
When you execute within SSMS, it drops the existing index and creates a new one.
From CREATE INDEX (Transact-SQL) on MSDN:
index_name - Is the name of the index. Index names must be unique within a table or view but do not have to be unique within a database.
(emphasis mine)
However, pkGUID is not an index - it is a constraint, and these do have to be unique within a database.
I'm attempting to align an index with a partitioned table, but the partition key column is nullable in the table definition.
In order to align the indexes I have to make this field not null, but when I attempt to alter the column on the table I receive an error saying that the table depends on that field.
You will need to remove the partition scheme from that table, correct the data issue, then repartition.
Or, create a new, properly partitioned version of the table with the right constraints and put your data into it from the old table.