Why can't you Alter multiple columns within the same line - tsql

I was simply wondering if there was a performance or other technical reason for you to be unable to perform a ALTER TABLE ALTER COLUMN statement with multiple columns within the same line e.g.
ALTER TABLE tblGeneric ALTER COLUMN Generic1 VARCHAR(255), Generic2 VARCHAR(255);
This is exclusively a restriction of t-sql as you can in fact comma separate columns with the MODIFY statement of mysql.
I just thought it odd, especially considering the MODIFY of mysql, that you can do a same line multi ALTER TABLE ADD statement but not a same line multi ALTER. I was just wondered if there is any particular documented reason for this or at least if it's in a issues list.

SQL Server T-SQL doesn't allow multiple columns to be changed in one ALTER TABLE command (unlike some other languages where it's possible).
Please follow this MSDN link for ALTER command syntax and explanation.
However, you can do multiple ADD or multiple DROP COLUMN, but just one ALTER COLUMN.

Related

Setting multiple alter statements on a single column in one command SQL

I am trying to modify a column data type and name in one command.
Is it possible to apply multiple alter statements on one column in one code line? Is that the correct syntax?
ALTER TABLE tb_table
ALTER COLUMN colum_date
SET DATA TYPE TIMESTAMP NOT NULL
RENAME COLUMN colum_date TO colum_timestamp;
I have been looking for related bibliography but could not find much information on the web.
According to the documentation renaming a column is an action that cannot be combined with others but requires its own statement. So no, you cannot do what you want in one statement.
But you can do it in one transaction. Setting the datatype and setting the NOT NULL constraint requires two actions though. But both of that actions can be in one statement.
BEGIN TRANSACTION;
ALTER TABLE tb_table
ALTER COLUMN colum_date
SET DATA TYPE TIMESTAMP,
ALTER COLUMN colum_date
SET NOT NULL;
ALTER TABLE tb_table
RENAME COLUMN colum_date
TO colum_timestamp;
COMMIT TRANSACTION;

Alter the column type over several tables

In a PostgreSQL db I'm working on, half of the tables have one particular column, always named the same, that is of type varchar(5). The size became a bit too restricting and I want to change it to varchar(10).
The number of tables in my particular case is actually very manageable to do it by hand. But I was wondering how one could script this with a query for larger dbs. It generally should be possible in just a few steps.
Identify all the tables in the schema, then (?) filter by condition if column present.
Create ALTER TABLE statements for each table found
I have some idea about how to write a query that identifies all tables in the schema. But I wouldn't know how to filter them. And if I didn't filter them, I assume the generated alter table statements would break.
Would be great if someone could share their knowledge on this.
Thanks to Abelisto for providing some guidance. Eventually, this is how I did it.
First, I created a query that in turn creates the ALTER TABLE statements. MyDB and MyColumn need to reflect actual values.
SELECT
'ALTER TABLE '||columns.table_name||' ALTER COLUMN '||MyColumn||' TYPE varchar(20);'
FROM
information_schema.columns
WHERE
columns.table_catalog = 'MyDB' AND
columns.table_schema = 'public' AND
columns.column_name = 'MyColumn';
Then it was just a matter of executing the output as a new query. All done.

PostgreSQL - Dynamic addition of large no of columns

Assume I have a table named tracker with columns (issue_id,ingest_date,verb,priority)
I would like to add 50 columns to this table.
Columns being (string_ch_01,string_ch_02,.....,string_ch_50) of datatype varchar.
Is there any better way to add columns with single procedure rather than executing the following alter command 50 times?
ALTER TABLE tracker ADD COLUMN string_ch_01 varchar(1020);
Yes, a better way is to issue a single ALTER TABLE with all the columns at once:
ALTER TABLE tracker
ADD COLUMN string_ch_01 varchar(1020),
ADD COLUMN string_ch_02 varchar(1020),
...
ADD COLUMN string_ch_50 varchar(1020)
;
It's especially better when there are DEFAULT non-null clauses for the new columns, since each of them would rewrite the entire table, as opposed to rewriting it only once if they're grouped in a single ALTER TABLE.

postgresql: alter multiple columns

My database has severals table with some column type 'money'. I would like to alter all these columns (in different tables) in a single statement rather than change type column by column, to avoid omissions.
You'll have to repeat the altering query for every column.
You might want to create a program code to do that for you. You know, with loops.
In order for the database to alter all the tables atomically you should enclose all the altering queries in a transaction (PostgreSQL supports transactional DDL).

Sybase - Changing size of "Name" column - "Illegal Column Definition" error

We have a Companies table in our database with a "Name" varchar column and its size is currently 30 characters. Our seemingly simple task of changing its size to 50 characters has turned into a bit of an issue. When trying to change it and save the changes through Sybase Central we get the following error:
[Sybase][ODBC Driver][SQL Anywhere]Illegal column definition: Name
SQLCODE: -1046
SQLSTATE: 42000
SQL Statement: ALTER TABLE "DBA"."Companies" ALTER "Name" VARCHAR(50)
We've tried various escaping characters around the column thinking it might have something to do with the word "Name" being treated differently internally by Sybase. We have no indexes or constraints on this column and have removed the single trigger that did exist just trying to isolate any potential factors. Further perplexing us, we have a Companies_a table that keeps track of all changes to the Companies table that has nearly the exact same schema including the Name column. We are able to change that column's size without issue which seems to indicate it's not necessarily an issue with the word "Name". I've gone through all the tabs in Sybase Central for this table, and don't see anything special/different about this table or this column.
Googling this issue is difficult as the word "Name" is extremely common. We have workarounds we can do (ie. creating temp column, copying, dropping & recreating the column, copying back) but if possible I'd like to understand exactly what's happening here and why.
I believe our Companies table and this column were likely created in a previous version of ASA. The inline_max column in sys.systabcol was set to null and we could not change it.
Running the following statement set the defaults:
ALTER TABLE "DBA"."Companies" ALTER "Name" inline use default prefix use default;
I was then able to run this statement without error:
ALTER TABLE "DBA"."Companies" ALTER "Name" VARCHAR(50);
Here's where I found the general concept for the fix:
Sybase SqlAnywhere forum thread
ALTER TABLE [tableName]
ALTER [ColumnName] varchar(4000) NULL