Tsql add column using synonyms - sql-server

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%'

Related

Alter data type of columns presents in manies tables with sql server script code

I'm tring to simplify my task. I'm want to use one script to change data type of all columns name like xxx_ID or Datexxx present in any table.
Example of column: Employee_ID, Customer_ID, Periode_ID, Contry_ID, DateInsert, DateUpdate,...
eg: ALTER all TABLE like dbo.tbl_* and all COLUMN like *_ID
///something like this
ALTER TABLE dbo.tbl_*
ALTER COLUMN *_ID varchar(15) NOT NULL
ALTER TABLE dbo.tbl_*
ALTER COLUMN Date* date NOT NULL
////SQL SERVER 2019
Can someone have idea?

Alter column Datatype

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);

How to change existing column type of a table in Sybase?

I searched for a while and can't get an answer.
Why this doesn't work?
ALTER TABLE mytable
ALTER COLUMN price DOUBLE
The syntax is incorrect and there is no DOUBLE datatype in Sybase.
So, you may try it like this:
ALTER TABLE mytable MODIFY price float
To alter any table in order to change the datatype of some field :
ALTER TABLE <table_name> MODIFY <column_name> <new_datatype>
For example:
In order to change the datatype of any column 'emp_id' from int to varchar, you need to :
ALTER TABLE employee MODIFY emp_id varchar(10)
Isn't it simple ?

How to change a normal column to "computed" column

I have a table in MSSQL server 2008. I would like to change one of the column in that table to computed column. Could somebody tell me how do I do that ?
Preserve the old data:
EXEC sp_rename 'MyTable.OldCol', 'RenamedOldCol', 'COLUMN';
Add computed column
ALTER TABLE MyTable ADD ComputedCol AS (some expression);
Then, when you're happy
ALTER TABLE MyTable DROP COLUMN RenamedOldCol;

How to change the data type of a column without dropping the column with query?

I have a column which has a datatype : datetime. But now i want to convert it to datatype varchar. Can i alter the datatype without droppping the column? If yes, then please explain how?
MSDN says
ALTER TABLE mytable ALTER COLUMN mycolumn newtype
Beware of the limitations of the ALTER COLUMN clause listed in the article
If ALTER COLUMN doesn't work.
It is not unusual for alter column to fail because it cannot make the transformation you desire. In this case, the solution is to create a dummy table TableName_tmp, copy the data over with your specialized transformation in the bulk Insert command, drop the original table, and rename the tmp table to the original table's name. You'll have to drop and recreate the Foreign key constraints and, for performance, you'll probably want to create keys after filling the tmp table.
Sound like a lot of work? Actually, it isn't.
If you are using SQL Server, you can make the SQL Server Management Studio do the work for you!
Bring up your table structure (right-click on the table column and select "Modify")
Make all of your changes (if the column transformation is illegal, just add your new column - you'll patch it up in a moment).
Right-click on the background of the Modify window and select "Generate Change Script." In the window that appears, you can copy the change script to the clipboard.
Cancel the Modify (you'll want to test your script, after all) and then paste the script into a new query window.
Modify as necessary (e.g. add your transformation while removing the field from the tmp table declaration) and you now have the script necessary to make your transformation.
ALTER TABLE [table name] MODIFY COLUMN [column name] datatype
ALTER TABLE YourTableNameHere ALTER COLUMN YourColumnNameHere VARCHAR(20)
Type the below query:
alter table table_Name alter column column_name datatype
e.g.
alter table Message alter column message nvarchar(1024);
ALTER TABLE YourTableNameHere ALTER COLUMN YourColumnNameHere VARCHAR(20)
With SQL server 2008 and more, using this query:
ALTER TABLE [RecipeInventorys] ALTER COLUMN [RecipeName] varchar(550)
This work for postgresql 9.0.3
alter table [table name] ALTER COLUMN [column name] TYPE [character varying];
http://www.postgresql.org/docs/8.0/static/sql-altertable.html
ALTER TABLE [table_name] ALTER COLUMN [column_name] varchar(150)
ALTER TABLE YourTableNameHere ALTER COLUMN YourColumnNameHere VARCHAR(20) this is perfect for change to datatype
ORACLE - Alter table table_name modify(column_name new_DataType);
ALTER TABLE yourtable MODIFY COLUMN yourcolumn datatype
ALTER TABLE table_name
MODIFY (column_name data_type);
ALTER tablename MODIFY columnName newColumnType
I'm not sure how it will handle the change from datetime to varchar though, so you may need to rename the column, add a new one with the old name and the correct data type (varchar) and then write an update query to populate the new column from the old.
http://www.1keydata.com/sql/sql-alter-table.html
alter table [table name] remove [present column name] to [new column name.

Resources