I'm trying this query to change attribute name - sql

alter table car
rename column office-id to office_id
Why is it giving out "missing to keyword" error?

This seems to be a potential solution:
Error trying to rename columns with space in oracle table. Error - SQL Error : ORA- 00946 : missing TO keyword
alter table car rename column "office-id" to office_id

You'll likely need to enclose the column name in quotes
alter table car rename column "office-id" to office_id
Also you don't specify the dialect (the db is it Oracle, MS SQL, Postgres?)

Related

Moving a table from one schema to another in Exasol

I am trying to move a table which resides in a certain schema to a different schema with the same table name. I have tried the following but they do not work:
rename <OLD_SCHEMA_NAME>.<TABLE_NAME> TO <NEW_SCHEMA_NAME>.<TABLE_NAME>;
The error that appears is:
SQL Error [42000]: invalid identifier chain for new name [line 1, column 100] (Session: 1722923178259251200)
and
ALTER TABLE <OLD_SCHEMA_NAME>.<TABLE_NAME> RENAME <NEW_SCHEMA_NAME>.<TABLE_NAME>;
The error that appears is:
SQL Error [42000]: syntax error, unexpected IDENTIFIER_PART_, expecting COLUMN_ or CONSTRAINT_ [line 1, column 62] (Session: 1722923178259251200)
Many Thanks!
According to Exasol documentation there is no way to move table between schemas using RENAME statement:
Schema objects cannot be shifted to another schema with the RENAME
statement. For example, 'RENAME TABLE s1.t1 TO s2.t2' is not allowed.
I would move the table this way:
create table <NEW_SCHEMA_NAME>.<TABLE_NAME>
like <OLD_SCHEMA_NAME>.<TABLE_NAME>
including defaults
including identity
including comments;
insert into <NEW_SCHEMA_NAME>.<TABLE_NAME>
select *
from <OLD_SCHEMA_NAME>.<TABLE_NAME>;
drop table <OLD_SCHEMA_NAME>.<TABLE_NAME>;

Newly Added Column is not Appearing in Synonym

I have a table named YY_ZZ_VAT_TRX_DETAILS under the XX schema.
It has an existing SYNONYM to AA schema.
I am currently logged in as AA and I wanted to add a column to YY_ZZ_VAT_TRX_DETAILS and executed the below command just fine
alter table XX.YY_ZZ_VAT_TRX_DETAILS
add (USER_DEFINED_FISC_CLASS VARCHAR2(30));
Table XX.YY_ZZ_VAT_TRX_DETAILS altered.
I tried to select the Column from the Table using the simple query below
select USER_DEFINED_FISC_CLASS
from YY_ZZ_VAT_TRX_DETAILS;
But I surprisingly got this error:
ORA-00904: "USER_DEFINED_FISC_CLASS": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action: Error at Line: 1 Column: 9
I thought I just missed altering the table, so I re-ran the ALTER script again, but got this error instead:
Error starting at line : 4 in command - alter table
XX.YY_ZZ_VAT_TRX_DETAILS add (USER_DEFINED_FISC_CLASS VARCHAR2(30))
Error report - ORA-01430: column being added already exists in table
01430. 00000 - "column being added already exists in table"
*Cause:
*Action:
I checked the Object Definition and sure enough, I found the new column there:
But when I query all the columns from the Table, I still can't see it:
select *
from YY_ZZ_VAT_TRX_DETAILS;
However, when I query using the XX schema prefix, I'm able to see the column:
select *
from XX.YY_ZZ_VAT_TRX_DETAILS;
Why isn't the synonym picking up the newly-added column?
It seems this was caused by this "Edition-Based Redefinition" thing that Oracle recently introduced for 12c.
Upon further investigation, what happened was:
The Table XX.XX_ZZ_VAT_TRX_DETAILS has a Editionable view named XX_ZZ_VAT_TRX_DETAILS# that only selects a certain number of columns.
Now, the View XX_ZZ_VAT_TRX_DETAILS# has a SYNONYM named XX_ZZ_VAT_TRX_DETAILS under the schema AA, hence the confusion on the Table having a SYNONYM of the same name.
To resolve this, i modified the view XX_ZZ_VAT_TRX_DETAILS# and added the new column and successfully recompiled it.
Once this was done, the synonym now shows the newly-added column and I was able to compile the stored procedure successfully.
I believe you could be having a table or a view called YY_ZZ_VAT_TRX_DETAILS or a private synonym that points to another table?.
In oracle the preference while querying is
1)objects_in same schema
2)private synonym
3)public synonym

Error while trying to mask a column in SSMS

I'm trying to mask some information in my table, when I use this :
ALTER TABLE Person
ALTER COLUMN DoB add masked with (Function = 'default()')
I get those two error :
Incorrect syntax near the word 'masked'
and
Incorrect syntax near the keyword 'with'. If the statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
That's literally the same code as the Microsoft documentation, but for some reason it doesn't work
If somebody could explain me what is the problem, that would help me a lot
To mask a column using the ALTER statement the datatype of the column must be included in this statement. The ADD keyword is also not necessary. I don't know what the datatype of the DoB column from your example is but it will need to be changed as below. From the name I'm guessing this is a datetime column, but if it's not the matching datatype will need to be substituted in.
ALTER TABLE Person ALTER COLUMN DoB DATETIME MASKED WITH (FUNCTION = 'default()')
Update:
This error was a result of using SQL Server 2012 when this feature was released in SQL Server 2016.

Rename column with reserved word as column name; Works on one, fails on another

I'm renaming two columns in two tables, both of which are named LEVEL, which is a reserved word.
What is confusing me greatly is that I am running an identical command on both these tables, but only one of them is failing. Here are the commands:
--this works
ALTER TABLE notificationsubscriptions RENAME COLUMN "LEVEL" TO nlevel;
--this doesn't
ALTER TABLE notifications RENAME COLUMN "LEVEL" TO nlevel;
The second statement results in the following error:
Error starting at line : 9 in command -
ALTER TABLE notifications RENAME COLUMN "LEVEL" TO nlevel
Error report -
SQL Error: ORA-00900: invalid SQL statement
00900. 00000 - "invalid SQL statement"
*Cause:
*Action:
Both these columns are of the same datatype (number). I am baffled - Why would this command work for one of these tables but not the other?
The database is running Oracle 10.2.
Try to remove any links that exist on this column from indexes and/or foreign key constraints
Drop indexes
Rename column
Create indexes with the new name of column

"syntax error at or near 'order'"" in PostgreSQL

I'm trying to add a column named order to my table. I realize that order is a reserved word in SQL. So, how do I do it?
My command:
alter table mytable add column order integer;
I've also tried:
alter table mytable add column 'order' integer;
PostgreSQL 9.1.
Use this:
alter table mytable add column "order" integer;
But, you might want to consider using a non-reserved name instead, like sort_order or something similar that reflects what the column is used for (and isn't a reserved word).
I think you don't need "column". Plus "order" is a keyword in SQL, so you should use a different name for your column. Follow this syntax:
ALTER TABLE table_name ADD column_name datatype
Source: W3Schools
ALTER TABLE table_name
ADD COLUMN "order" integer
You are using order which is a reserved keyword you should consider renaming that to something like orders. And the problem should go away.