Alter column Datatype - sql-server

I have a table dbo.ExceptionMessage and now I want to change the column datatype nvarchar(100) to nvarchar(MAX). I used alter query for changing this
ALTER TABLE dbo.ExceptionMessage ALTER COLUMN Address nvarchar(MAX)
and while excecuting this query it shows some error like.
The object 'DF_ExceptionMessage_Address' is dependent on column 'Address'.
ALTER TABLE ALTER COLUMN Address failed because one or more objects access this column.
How can we solve this...

First Delete all Constraint Like this
ALTER TABLE TableName DROP CONSTRAINT [DF__TableName__ColumnName__FieldName]
and then perform change
ALTER TABLE dbo.ExceptionMessage ALTER COLUMN Address nvarchar(MAX)
then re enter the constraints

You have to find out which type of constraint DF_ExceptionMessage_Address is, drop it, alter the column type and then re-create the constraint if you need it.

you try first:
ALTER TABLE <tablename> DROP CONSTRAINT <Con_Name>;
And Then Do your Alter
ALTER TABLE dbo.ExceptionMessage ALTER COLUMN Address nvarchar(MAX)
Again Add Constraint

thiz help you to alter
alter table TableName
alter column ColumnName nvarchar(200);

Related

How do I remove a DEFAULT constraint?

This is what I tried. I simply want to remove the constraint I just did there
ALTER TABLE product
ADD DEFAULT NULL FOR sale_id
ALTER TABLE product
ALTER COLUMN sale_id DROP DEFAULT
ALTER TABLE TABLE_NAME
DROP CONSTRAINT <constraint_name>
Eg:
ALTER TABLE students
ALTER COLUMN ph
DROP NOT NULL;

Why does "DEFAULT" is causing syntax problem

I'm new in using SQL Server and right now I'm trying to change "Id" column from default ID to GUID.
When using this code
ALTER TABLE dbo.Bookings ALTER COLUMN Id UNIQUEIDENTIFIER DEFAULT NEWID();
It gets me an error
Incorrect syntax near 'DEFAULT'
And I don't understand where is the syntax problem.
Can anyone point it out what is causing this error?
You should use the add constraint syntax:
ALTER TABLE dbo.Bookings ADD CONSTRAINT DF_dbo_Bookings_Id DEFAULT(NEWID()) FOR Id;
Also, if your table have the primary key clustered on that Id column, you could use the NEWSEQUENTIALID for creating unique identifiers that will have less impact on your writes:
ALTER TABLE dbo.Bookings ADD CONSTRAINT DF_dbo_Bookings_Id DEFAULT(NEWSEQUENTIALID()) FOR Id;
Example, on dbfiddle
ALTER TABLE dbo.Bookings
ALTER COLUMN id UNIQUEIDENTIFIER ;
ALTER TABLE dbo.Bookings
ALTER id SET DEFAULT NEWID();
Its possible using the alters separately.
Its the best to backup current table to avoid potential data loss.

SQL Server: Can't alter nvarchar column length because of "dependent on column" error

I'm trying to shorten the length of a nvarchar column. None of the existing data in the nvarchar exceeds the new length.
ALTER TABLE [MyObject]
ALTER COLUMN [Alias] NVARCHAR(64) NOT NULL
Error:
The object 'MyObject_OppID_70e6d249_uniq' is dependent on column 'Alias'
I don't understand how another column is dependent on a non-foreign-key nvarchar column.
You should first drop the constraint like
ALTER TABLE [MyObject] DROP CONSTRAINT MyObject_OppID_70e6d249_uniq;
Similarly you might required to drop other dependent constraints on the column.
After that you should be able to alter the column.
ALTER TABLE [MyObject] ALTER COLUMN [Alias] nvarchar(64) NOT NULL
Once done, you can re-create the required constraints by altering the table.

Tsql add column using synonyms

I have to add two columns to a table
and I have a synonym associated with that table
For example:
My table name is table_abc and the synonyms is table_1
When i try to add column to the table in normal way,
ALTER TABLE [dbo].[table_1]
ADD test_1 varbinary(MAX),
test_2 varbinary(MAX);
Following error occurs :
Cannot alter 'dbo.table_1' because it is not a table.
Is there a correct way using synonyms to add a column to the table? or is it just a wrong way to add a column?
Thanks a lot!
You cannot alter table with alias name and you have to add column to the table
in this way
ALTER TABLE [dbo].[table_abc]
ADD test_1 varbinary(MAX)
and
ALTER TABLE [dbo].[table_abc]
ADD test_2 varbinary(MAX)
We cannot alter Synonyms created on table in Sql Server.
We can only do
CREATE SYNONYM (Transact-SQL)
DROP SYNONYM (Transact-SQL)
If we want to alter main table
alter table table_abc
add test_1 varbinary(MAX),
test_2 varbinary(MAX);
**To get the table name from the synonym name **
SELECT base_object_name,* FROM sys.synonyms where name like'%Synonym name%'

ALTER TABLE constraint problem

I'm trying to run the following SQL statement:
IF OBJECT_ID('MyTable') IS NOT NULL
DROP TABLE MyTable
SELECT
a.UserId
INTO
MyTable
FROM
UsersTable a
WHERE
a.UserId='12359670-1DC9-4A0A-8AE5-29B664C1A57E'
ALTER TABLE MyTable ALTER COLUMN UserId UNIQUEIDENTIFIER NOT NULL
ALTER TABLE MyTable ADD PRIMARY KEY(UserId)
However, I get the following error:
Cannot define PRIMARY KEY constraint on nullable column in table 'MyTable'.
Any ideas?
This assumes SQL Server based on UNIQUEIDENTIFIER
Put a GO between (or relevant batch separator if not SQL Server)
....
ALTER TABLE MyTable ALTER COLUMN UserId UNIQUEIDENTIFIER NOT NULL
GO
ALTER TABLE MyTable ADD PRIMARY KEY(UserId)
At batch compile time, the column is nullable. So break up the batches.
SQL isn't a line by line procedural language
You'll have to do this in a stored procedure
ALTER TABLE MyTable ALTER COLUMN UserId UNIQUEIDENTIFIER NOT NULL
EXEC('ALTER TABLE MyTable ADD PRIMARY KEY(UserId)')
Found a solution.
I'm using the following statement:
EXEC sp_ExecuteSQL N'ALTER TABLE MyTable ALTER COLUMN UserId UNIQUEIDENTIFIER NOT NULL'
EXEC sp_ExecuteSQL N'ALTER TABLE MyTable ADD PRIMARY KEY(UserId)'
You don't need dynamic SQL. Just create the table and the key first.
CREATE TABLE MyTable (UserId UNIQUEIDENTIFIER NOT NULL PRIMARY KEY);
INSERT INTO MyTable (UserId)
SELECT UserId
FROM UsersTable
WHERE UserId='12359670-1DC9-4A0A-8AE5-29B664C1A57E';

Resources