Invalid Identifier/Ambiguously Defined - sql

I have a SQL statement which ends in:
where <table_name>.<column_name> = '<column_value>'
I get the following error:
ORA-00904: "table_name"."column_name": invalid identifier
However, I know that the column is valid for sure. I also tried:
where <schema><table_name>.<column_name> = '<column_value>'
but got the same error. Lastly I tried without the identifiers:
where <column_name> = '<column_value>'
but that results in an column is ambiguously defined error.
What am I missing here?
Whole Query:
SELECT r.<COLUMN_NAME_1>, r.<COLUMN_NAME_2>, etc, t_append.*
FROM (
SELECT <COLUMN_NAME_1>, r.<COLUMN_NAME_2>, etc..
FROM <TABLE_NAME> ) r
inner join <TABLE_NAME> t_append on
t_append.<COLUMN_NAME_1> = r.<COLUMN_NAME_1>
AND t_append.<COLUMN_NAME_2> = r.<COLUMN_NAME_2>
AND etc...
WHERE <TABLE_NAME>.<COLUMN_NAME_1> = '<COLUMN_VALUE1>'
AND <TABLE_NAME>.<COLUMN_NAME_2> = '<COLUMN_VALUE2>'
This query takes composite key columns and value and then returns the composite key values followed by the row data which the key represents.

Apart from the above mentioned suggesstions,there may two possibilities according to me. You may get
ORA-00904: "table_name"."column_name": invalid identifier
1) if you don't have necessary permissions on the accessing objects. (confirm your permission on the object)
2) if your column was defined with double quotes like below
create table test("CheckMyColumn" number));
then it will be case sensitive. (Refer the table definition and try with same case)

The reason of a column ambiguously is because oracle doesn't know which column you are referring , it seems in your query you have specify 2 tables( from tab1 , tab2 ).
As for "table_name"."column_name": invalid identifier it means for sure column_name column for table table name doesn't exists, can you provide the ddl of the table.

Related

How do I change column data type in Redshift?

I have tried changing the column data type in Redshift via SQL. Keep getting an error:
[Amazon][Amazon Redshift] (30) Error occurred while trying to execute a query: [SQLState 42601] ERROR: syntax error at or near "TABLE" LINE 17: ALTER TABLE bmd_disruption_fv ^
Unable to connect to the Amazon Redshift server 'eceim.master.datamart.eceim.sin.auto.prod.c0.sq.com.sg'. Check that the server is running and that you have access privileges to the requested database
The first sql query works. I have tried writing the Alter Table script before the Select lines but it did not work too.
`
*Extract selected columns and renaming them for easier reference
*/
select ID, Completion_Time AS Date_Reported, Name2 AS Name, Contact_Info_for_updates AS Contact_Info,
Your_operation_line AS Operation_Line, Aircraft_Registration_SMU_SMT_etc AS Aircraft_Reg,
Designation_trade_B1_B2_ACT_AST_AAT AS Trade, Choose_your_Issue AS Issue, Manpower, Material, Equipment_GES,
Information, Tools, State_details_here_SVO_number_too AS Issue_Details, Time_wasted_on_due_to_issue AS Time_Wasted,
State_additional_comments_suggestions AS Additional_Comments, Stakeholders, Status
from bmdm.bmd_disruption_fv
/*Change colum data type
*/
ALTER TABLE bmd_disruption_fv
{
ALTER COLUMN ID TYPE INT
}
`
Several things are causing issues here. First the curly brackets '{}' should not be in the alter table statement. Like this:
alter table event alter column eventname type varchar(300);
Second, and likely more importantly, you can only change the length of varchar columns. So changing a column type to INT is not possible. You will need to perform a multistep process to make this change to the table.

ERROR: syntax error at or near "." LINE 4: ON like.takerId = frame.likeId;

i have a table whose name is like. But whenever i have to select data from like, i was getting this error, i figured it out public.like..but when i try to join two tables
SELECT *
FROM frame
INNER JOIN public.like
ON like.takerId = frame.likeId;
i get this error
ERROR: syntax error at or near "."
LINE 4: ON like.takerId = frame.likeId;
i also use public prefix but it throws
ERROR: column like.takerid does not exist
LINE 4: ON public.like.takerId = frame.likeId;
^
HINT: Perhaps you meant to reference the column "like.takerId".
even if it is saying column like.takerid does not exist , then why it gives me HINT: Perhaps you meant to reference the column "like.takerId". I dont know, i think it is problem with like table name, like is a sql syntax, and it assumes like and a sql syntax and throwing me error. Should I change my table name? Or is there any way to make sql case sensetive or how can i tell sql to ignore like. public.like is not working for joining table.
As like is a reserved keyword, you need to use double quotes for each occurance of it (unless it's prefixed with the schema name as you found out)
SELECT *
FROM frame
JOIN public.like ON "like".takerId = frame.likeId;
Or
SELECT *
FROM frame
JOIN "like" ON "like".takerId = frame.likeId;
Or use an alias
SELECT *
FROM frame f
JOIN "like" l ON l.takerId = f.likeId;
But in the long run you should find a different name for the table that does not require quoting.
You should definitely chose another name for your table. LIKE is a reserved command, and it is considered a bad practice to use it, although possible by using ", e.g.
CREATE TABLE public."like" (id int);
INSERT INTO public."like" VALUES (42);
SELECT * FROM "public.like"
EDIT: As pointed out by #a_horse_with_no_name, specifying a schema in temporary tables won't work (check db<>fiddle), so only the table name should be between double quotes as corrected in the snippet above. For temporary tables just omit the schema:
CREATE TEMPORARY TABLE "like" (id int);
INSERT INTO "like" VALUES (42);
SELECT * FROM "like"
Demo: db<>fiddle

psql column doesn't exist but it does

I am trying to select a single column in my data table using raw SQL in a postgresql database from the psql command line. I am getting an error message that says the column does not exist. Then it gives me a hint to use the exact column that I referenced in the select statement. Here is the query:
SELECT insider_app_ownershipdocument.transactionDate FROM insider_app_ownershipdocument;
Here is the error message:
ERROR: column insider_app_ownershipdocument.transactiondate does not exist
SELECT insider_app_ownershipdocument.transactionDate FROM in...
HINT: Perhaps you meant to reference the column "insider_app_ownershipdocument.transactionDate".
I have no idea why this is not working.
(Postgres) SQL converts names automatically to lower case although it support case-sensitive names. So
SELECT insider_app_ownershipdocument.transactionDate FROM insider_app_ownershipdocument;
will be aquivalent to:
SELECT insider_app_ownershipdocument.transactiondate FROM insider_app_ownershipdocument;
You should protect the column name with double quotes to avoid this effect:
SELECT insider_app_ownershipdocument."transactionDate" FROM insider_app_ownershipdocument;

How To Reference a Column that has been concatenated in SQL

How do you reference a column in a table that has been concatenated? I am trying to reference the 'UniqueID' column in a join, but all the ways that I have tried it throw the ORA-00904 error saying "T2.UNIQUE ID:Invalid identifier".
create table cdm_user.uniquesubjectIDDEW as (
select distinct concat (site,screening_no) "UniqueID" , visit, site, Screening_no
from databrowser.v_data_entry_workflow
where study = '3508'
);
commit;
Select *
from cdm_user.uniquesubjectIDDEW t1
left join cdm_user.uniquesubjectIDDEW t2
on t1.UniqueID = t2.UniqueID
and t2.visit = 'Screening'
Where t1.visit = 'Week_52'
and t2.visit is null
Any help is much appreciated as I am new to SQL.
Unless quoted, identifiers such as table and columns names will be mapped to upper case. So your select will be interpreted as needing a column name UNIQUEID but you created the column name as "UniqueId" with quotes so it doesn't match.
You'll need to either unquote the name when you create the table or quote it in all queries.
Generally it is better not to use quoted, case-sensitive column names, which is why a lot of databases use underscores in table/column names as word separators rather than some variant of camel case.
Use quoted column name in createing command:
alter table cdm_user.uniquesubjectIDDEW add primary key ("UniqueID");

In sqlite How to add column in table if same column is not exists in table

How can I add a column in an SQLite table if and only if the same column does not exist in the table?
Using ALTER TABLE I am able to create a new column but want to know how to check whether that column already exists in the table or not?
SQLite returns an error like "no such column: foo" if the table doesn't contain the column:
select foo from yourTable limit 1
Also you can get the create-table statement:
select sql from sqlite_master where tbl_name = 'YourTableName'
and then parse the result, looking for the column-name. I don't know of an elegant way to query the list of columns for a specified table, though one may exist.
Also if you attempt to do this:
alter table YourTable add column foo {column-def whatever it is}
you get an error from SQLite if the column already exists. You could trap that error too.
Finally you could do this:
select sql from sqlite_master
where tbl_name = 'YOURTABLE' and sql like '%"foo" CHAR%'; -- or whatever type
and if the specified table contains the column which is surrounded by double-quotes in the query, and with the type you have specified, you will get a result, otherwise an empty set. Specifying the datatype ensures that your LIKE substring match occurs on a column-name.
There's no way (that I know of) to do it all in a single SQLite query. You must use application code to manage the If/Elseness.
Check if table exists or not:
select count(*) from sqlite_master where type = 'table' and name = MyTable';
Check if column exists in table or now
pragma table_info(thumbnail);
However, a better approach may be explicit database schema updates based on schema versions your application maintains (e.g. specific alter table statement to go from schema version 1 to 2):
pragma user_version;
It seems like that it is impossible to do checking if the column not exists and addindg the new column in one command, because Sqlite don't support "IF NOT EXISTS" for column. "IF NOT EXISTS" works only on table.
Here is what I will do:
rev = ExecuteStatement("SELECT columnNamexx FROM tableNamexx limit 1;");
if(rev != SQLITE_OK){ // add col to table
ExecuteStatement("ALTER TABLE tableNamexx ADD COLUMN columnNamexx INTEGER DEFAULT 0;");
}
You can view the table columns by using '.schema tableName'