Syntax error while using multiple rename RENAME expressions postgresql - sql

I'm trying to write a query that RENAMEs multiple table columns at once. According to the documentation, the syntax is:
ALTER TABLE table_name
RENAME old_col_a AS new_col_a
, RENAME old_col_b AS new_col_b...;
However, in doing so I get a syntax error located on the comma after the first RENAME clause:
ERROR: syntax error at or near ","
LINE 3: , RENAME
^
SQL state: 42601
Character: 1
The query works for multiple DROP/ALTER/ADD columns and for single RENAMEs. I just can't for the life of me figure out why this error is occurring.

You need to use multiple ALTER statements:
ALTER TABLE table_name
RENAME COLUMN old_col_a TO new_col_a;
ALTER TABLE table_name
RENAME COLUMN old_col_b TO new_col_b;
ALTER TABLE
All the forms of ALTER TABLE that act on a single table, except RENAME, SET SCHEMA, ATTACH PARTITION, and DETACH PARTITION can be combined into a list of multiple alterations to be applied together. For example, it is possible to add several columns and/or alter the type of several columns in a single command. This is particularly useful with large tables, since only one pass over the table need be made.

Related

Create a table in dbshell with django

here is my code :
frontgreat=> CREATE TABLE contact_titlemessagesuggestion;
ERROR: syntax error at or near ";"
LINE 1: CREATE TABLE contact_titlemessagesuggestion;
i don't understand why it's not working and why it's an syntax error.
frontgreat=> DROP TABLE contact_titlemessagesuggestion;
ERROR: table "contact_titlemessagesuggestion" does not exist
have no syntax error and work fine.
Regards
You can normally not create a table without any columns. Therefore one often either has to list the columns, or use for example a query that provides both data (but meta-data as well) to construct the columns in the table.
For example:
CREATE TABLE contact_titlemessagesuggestion (
pk INT
);

drop multiple tables in proc sql (via teradata)

I am trying to drop multiple tables, using the following code, but it raises an error.
the code:
PROC SQL ;
CONNECT TO teradata AS TERADATA (server=dbc mode=teradata) ;
EXECUTE (drop table TABLE_NAME1, TABLE_NAME2, TABLE_NAME3 ) BY teradata ;
DISCONNECT FROM teradata ;
QUIT ;
the error:
syntax error: expecting something between NAME1 and TABLE
If you look at the documentation for drop in Teradata, you will see that it operates on only one table:
Drops the definition for the specified table from the data dictionary and drops the object from its containing database or user, depending on the keyword specified.
This is how most databases work. You need to do three drops:
drop table TABLE_NAME1;
drop table TABLE_NAME2;
drop table TABLE_NAME3;
You can delete all objects in the database with one command:
DELETE DATABASE name_database;
but also views, triggers, stored procedures, user-defined functions, and macros will be deleted.

I want to add two columns names to an existing table in Impala Query

I am writing the following query to add a column at a specified position but getting the below error:
alter table quantum_raw_dev.rpt_backup_allocation
change upt_type upt_type STRING after tray_size;
You can add one or more columns to the end of the column list using:
ALTER TABLE <table_name> ADD COLUMNS (col_name col_type, ...);
[note: there is NO comma between column name and type]
Adding or Removing Columns
You can add one or more columns to the end of the column list using ADD COLUMNS,
or (with Impala only) you can delete columns using DROP COLUMN.
The general syntax is
ALTER TABLE tablename ADD COLUMNS (col1 TYPE1,col2 TYPE2,… );
ALTER TABLE tablename DROP COLUMN colname;
For example, you can add a bonus integer column to the employees table:
ALTER TABLE employees ADD COLUMNS (bonus INT);
Or you can drop the office_id column from the employees table:
ALTER TABLE employees DROP COLUMN office_id;
Notes
DROP COLUMN is not available in Hive, only in Impala. However, see “Replacing All Columns” below.
You can only drop one column at a time.
To drop multiple columns, use multiple statements or use the method to replace columns (see below).
You cannot add a column in the middle of the list rather than at the end.
You can, however, add the column then change the order (see above) or use the method to replace columns (see below).
As with changing the column order, these do not change the data files.
If the table definition agrees with the data files before you drop any column other than the last one,
you will need to recreate the data files without the dropped column's values.
If you drop the last column, the data will still exist but it will be ignored when a query is issued.
If you add columns for which no data exists, those columns will be NULL in each row.
Replacing All Columns
You can also completely replace all the columns with a new column list.
This is helpful for dropping multiple columns,
<h1>or if you need to add columns in the middle of the list<h1>
<h2>(like your use case)<h2>
The general syntax is
ALTER TABLE tablename REPLACE COLUMNS (col1 TYPE1,col2 TYPE2,… );
This completely removes the existing list of columns and replaces it with the new list.
Only the columns you specify in the ALTER TABLE statement will exist, and they will be in the order you provide.
Note
Again, this does not change the data files, only the metadata for the table,
so you'll either want the new list to match the data files or need to recreate the data files to match the new list.
I do not think you can add columns in between columns in Impala like above.
You can backup the data, drop the and recreate with new structure, and load the table from backup. Also if you have HIVE in your system you can try to do below steps -
Add column first and then use below commands to move columns around.
ALTER TABLE tab ADD COLUMNS (id BIGINT);
This moves id column to the beginning.
ALTER TABLE tab CHANGE COLUMN id id BIGINT first;
This moves existing_col after id.
ALTER TABLE tab CHANGE COLUMN existing_col existing_col string AFTER id;
Please refresh/invalidate metadata after applying all DDLs.
You cannot add column in between. Best way is to archive the data in another table. Drop the impala old table and create a fresh table with new columns as per the desired location and then reinsert the data.

Alter SQL statement for queries in MS ACCESS

I'm familiar that you can change tables using the ALTER TABLE statement. Is there a way to do the same for queries e.g. ALTER QUERY? I'm trying to change a field name from Expr1 to "MeaningfulFooNameField".
Something along the lines of:
"ALTER QUERY qryFoo RENAME COLUMN Expr1 to MeaningfulFooNameField;"
I can't do this manually as there are hundreds of tables to go through to keep the data definition consistent.

Oracle SQL. How to change table field datatype CLOB-->VARCHAR2

I have database and some text fields are CLOB type, I need to change most of these into VARCHAR2.
Have tried to do that using SQL Developer tool, by clicking edit on table, but get error like this one:
The following SQL statement failed:
ALTER TABLE TBL_PEOPLE MODIFY (PERSON VARCHAR2(150) )
Want to ask, how can this change be done
You can't, directly. The code you tried will have got an `ORA-22859, presumably. (It's helpful to show the actual errors you get, of course).
You'll need to add a new varchar2 column; copy the data across - or a substring of it if it might be larger than the new column you're creating; drop the clob column. You can rename the columns so it looks fairly transparent.
As in this SQL Fiddle:
alter table tbl_people rename column person to clob_person;
alter table tbl_people add (person varchar2(150));
update tbl_people set person = clob_person;
alter table tbl_people drop column clob_person;
Obviously don't drop the old column until you're sure the data has copied without errors. Also take into account any constraints, indexes, etc. that might exist in the old column; they will need to be recreated. And anything that references the old column will have been invalidated - generally procedures will recompile themselves on next use.
So be careful, test it first, and plan some down time.
ALTER TABLE tablename ADD (FIELD_LIST_TEMP VARCHAR2);
UPDATE tablename SET FIELD_LIST_TEMP = FIELD_LIST;
ALTER TABLE tablename DROP COLUMN FIELD_LIST;
ALTER TABLE tablename RENAME COLUMN FIELD_LIST_TEMP TO FIELD_LIST;
Here FIELD_LIST existing column which is defined it as CLOB. With above query it will change from CLOB to VARCHAR2.