Cannot create non existing column in SQL database - sql

I'm executing the following query on my db:
alter table Client drop column "IsVersionValid"
GO
alter table Client add "IsVersionValid" bit NULL
with the following result:
Msg 4924, Level 16, State 1, Line 1
ALTER TABLE DROP COLUMN failed because column 'IsVersionValid' does not exist in table 'Client'.
Msg 2705, Level 16, State 6, Line 3
Column names in each table must be unique. Column name 'IsVersionValid' in table 'Client' is specified more than once.
How is this possible? I'm trying to add a column that is not existing in my table. I tried doing this manually via the table designer of ssms. But this gives te same result.

This worked for me like a dream (also with double quotes), drop column if exists, and then recreate it!
ALTER TABLE Client
DROP COLUMN IF EXISTS IsVersionValid
GO
ALTER TABLE Client ADD IsVersionValid bit NULL

I solved my issue by creating a new table with the SELECT INTO statement. I then dropped the original table and renamed the new table. I think something was corrupted in this specific database. Other customers did not have this problem.

Related

Ambiguous column name on update of new column used in view

Scenario
Adding a column to a table and then updating that column
alter sometable add example_column_name varchar(255);
update sometable set example_column_name = '';
(real update is a bit more complex, but this is a boiled down version we used trying to find the problem)
Problem
The update query gives 'Ambiguous column name example_column_name.'
This works in all databases except one.
It is only for exactly one specific column name it happens, adding a column with different name and updating that column works
The column name in question works in other databases, and it already exists in other tables in the same db
Question
Does anyone know what's going on, how can we get past this problem?
Update
The problem was an indexed view that used the column name of the new column in an already existing query. See comments and accepted answer for details.
This error can't happen purely from the code shown.
There must be a trigger or indexed view in play. You have ruled out triggers so an example demonstrating the indexed view scenario is below
CREATE TABLE T1(X INT, Y INT)
CREATE TABLE T2(X INT, Z INT)
GO
CREATE VIEW V1
WITH SCHEMABINDING
AS
SELECT T1.X,
T1.Y,
Z
FROM dbo.T1
JOIN dbo.T2
ON T1.X = T2.X
GO
CREATE UNIQUE CLUSTERED INDEX IX
ON V1(X)
GO
ALTER TABLE T1
ADD Z INT;
GO
UPDATE T1
SET Z = 0
When the view is initially created the only table containing a column Z is T2 so it is not ambiguous. After adding column Z to T1 the view definition becomes ambiguous. The UPDATE to the table tries to automatically maintain the view and the error is thrown.
Msg 209, Level 16, State 1, Procedure V1, Line 5 [Batch Start Line 23]
Ambiguous column name 'Z'. Msg 4413, Level 16, State 1, Line 25 Could
not use view or function 'V1' because of binding errors.
It is best practice to always use two part naming where your query references more than one table to avoid this type of error.

How do I change a column data type from varchar(255) to date?

I am using a reporting database which consists of 20 tables on SQL Server. In marketing table I have a column report_date which is currently a varchar(255). It is basically a date formatted in a way 2017-12-12. I want to change the type of this column to a date. I’m running this script but getting errors. The script is down below:
USE [reporting].[dbo].[marketing]
GO
SELECT CONVERT(date, 'report_date');
These are the errors I’m getting.
Msg 911, Level 16, State 1, Line 1
Database 'dbo' does not exist. Make sure that the name is entered correctly.
Msg 241, Level 16, State 1, Line 3
Conversion failed when converting date and/or time from character string.
How should I adjust the script?
If you want to change the column's data type (and you should) then you need to use an alter table statement. Your first error message is because of the USE directive - it should be
USE [reporting]
GO
Your second error message is because 'report_date' is a string constant, not a column name.
The select statement should be
SELECT CAST(report_date as date) -- Don't use Convert without the style argument....
FROM [dbo].[marketing]
Note that if you have even a single value that can't be converted to date you will get the second error again.
Basically I would recommend first making sure that the select statement completes without any exceptions, and only then alter the table:
USE reporting
GO
ALTER TABLE [dbo].[marketing]
ALTER COLUMN report_date DATE
GO
TRY THIS:
SELECT CONVERT(DATE, report_date) --If only for comparison
ALTER TABLE marketing ALTER COLUMN report_date DATE --If want to change in the table
The proper way to do this is like;
ALTER TABLE reportin.dbo.marketing ALTER COLUMN 'report_date' date
And you can check this How do you change the datatype of a column in MS SQL?
"Convert" is used for conversion from one datatype to other in select queries, you need to use alter statement for altering database columns and also
USE [databasename] is enough, so rewriting your query here :
USE [reporting]
GO
ALTER TABLE marketing ALTER COLUMN ReportDate DATE
Slow, but safe way is to:
Create a new column (ALTER TABLE dbo.MyTable ADD MyNewColumn DATE NULL;)
Update the new column using the old one (UPDATE dbo.MyTable SET MyNewColumn = CONVERT(DATE, MyColumn);)
Drop the old column (ALTER TABLE dbo.MyTable DROP MyColumn;) - Alternatively, you can rename this column instead and keep it as is)
Rename the new column (EXEC sp_rename 'dbo.MyTable.MyNewColumn', 'MyColumn', 'COLUMN';)
You might have to drop indexes beforehand, but this method (and it's alterations) help to prevent data loss.
If you encounter an error during casting, you should eliminate those values from the update (by for example adding a WHERE clause) and investigate them manually.
If you are using SQL Server 2012 or newer, you can use TRY_CONVERT() to ignore the values which cannot be converted to DATE. In this case you will have NULL in your new column.
Before you do anything, make sure, that all applications and code which is working with this column can handle the changes.
Notes
You might want to rebuild the table/indexes after a change like this.

Column existent but sometimes prompted as not there

First I ran
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = '[Tablename]'
to know which columns to convert into another format via
ALTER TABLE [tablename]
ALTER COLUMN [columnname] [format]
Problem: I get the following prompt for a column listed by the first command line:
Msg 4924, Level 16, State 1, Line 16
ALTER TABLE ALTER COLUMN failed because column '[Columnname]' does not exist in table '[Tablename]'.
Trying to convert the column from FLOAT to DECIMAL(19,12) doesn't work, but I did that with other columns of the same table before successfully.
Appreciate the input as always!
Figured it out funnily enough right after posting it. Had been debating about it for a while beforehand. My column name actually started with a space, was not aware that this was even possible.

Set Identity in existing column of table in SQL Azure

I am facing one critical issue in SQL Azure database, i have created this database from local sql server database and in that one table i have set isidentity property.
Today i have deleted that table and created it again through SQL Azure portal but i could not able to set identity column on that! I have tried with sql query also like below
alter table mytablename alter column id identity(1,1)
gives me error like
Msg 156, Level 15, State 1, Line 1 Incorrect syntax near the keyword
'identity'.
Please suggest me some technique by which i can alter this column and set isidentity property.
Thanks in Advance.
In this case i have use RazorSQL to alter this table and first dropped that table and recreate using sql query and it's worked

SQL server 2005 query not running

Before posting this question, I have tried so many things but that was not helpful for me.
I want to rename the column of table at sql server 2005, following query I have run at sql server2005:
1) ALTER TABLE Details RENAME COLUMN
AccountID TO UID; but it gives me the
error: Incorrect syntax near the
keyword 'COLUMN'.
2)I have added one new column in the
table by query: ALTER TABLE Details
ADD BID uniqueidentifier; and then I
want to set the coulmn property to not
null .
How can i do that?
Thanks in advance
AS
Use sp_Rename 'TableName.Column', 'NewColumnName', 'COLUMN'.
In order to do your second part of the question, you'll need to do:
ALTER TABLE myTable
ADD myColumn UNIQUEIDENTIFIER NOT NULL DEFAULT 'some default value'
If you don't want to specify a default value, you'll have to first create the column with NULL. Once the column is created, you can then populate with your desired values and then re-alter the column to NOT NULL.
1) Instead of using ALTER TABLE, why not use sp_rename? For example:
EXEC sp_rename 'Details.[AccountID]', 'title', 'UID'
2) You can use ALTER TABLE Details ALTER COLUMN BID NOT NULL, but you'll probably want to specify a default value for it also, if the table already has data in it.