This is a really conviluted piece of SQL, I know, but basically one of the data entry monkeys where I work screwed up big-time, and the copy has gotten deep into our system.
There is a rather large number of tables that need to have a value changed from VAL1 to VAL2, if another value is equal to VAL3. The problem is that I don't know all of the tables where this is the case, and there is strict column naming policy that means that all tables have unique column names.
I wrote the following SQL to attempt to do this update, but it doesn't work:
UPDATE
(SELECT DISTINCT TABLE_NAME AS tbTableName
FROM ALL_TAB_COLUMNS
WHERE COLUMN_NAME LIKE '%MAJR%')
SET
(SELECT COLUMN_NAME AS tbColumnName
FROM tbTableName
WHERE COLUMN_NAME LIKE '%MAJR%') = 'VAL2'
WHERE
(SELECT COLUMN_NAME AS tbColumnNameWhere
FROM tbTableName
WHERE COLUMN_NAME LIKE '%PROGRAM%') = 'VAL3'
AND tbColumnName = 'VAL1';
But yeah, this falls over with the error: invalid user.table.column, table.column, or column specification
01747. 00000 - "invalid user.table.column, table.column, or column specification"
Any help would be appreciated.
You can't do subqueries like this, unfortunately. Your best bet is to use PHP or whatever your primary scripting language is and query the database to build all the SQL statements for you.
Related
While inserting data into a table which has many columns:
INSERT INTO MyTable ("name", ..100+ columns)
VALUES ('Michel', ... 100+ values)
I made an error creating a specific value so PostgreSQL tells us:
ERROR: value too long for type character varying(2)
I would like to avoid going through the whole table schema to guess which column is failing.
Is there a way to configure PostgreSQL so it tells us which column is causing the error?
One quick way might be to query the information schema table for your database and look for character columns having a maximum width of 2 (to which your error is alluding):
SELECT column_name
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'MyTable' AND
character_maximum_length = 2;
select COLUMN_NAME
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME='Submenu';
The above query gave all column names in Submenu table but I want only three column names of Submenu table.
Is there any way to get column names?
I assumed this is SQL Server, so the below queries might not work on other RDBMS's).
If your question is how to find only the column names you need (presuming you know in advance which ones those are), you would have to do something like this:
SELECT
COLUMN_NAME
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME='Submenu'
AND COLUMN_NAME IN ('Column1', 'Column2', 'Column3')
At this moment, you basically are requesting a list of any column within your table, without any restrictions whatsoever.
Alternatively, if you're looking only for the first three column names, this would work:
SELECT TOP 3
COLUMN_NAME
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME='Submenu'
ORDER BY
ORDERINAL_POSITION
In the latter case, you will have to determine how you want to sort the column names though (either by using something like ORDER BY COLUMN_NAME, in case you want them listed alphabetically, or ORDER BY ORDERINAL_POSITION in case you're trying to get them in the order they appear in the table).
If this is not what you meant, please elaborate on what you are trying to achieve.
SELECT TOP 3 COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Submenu';
As easy as that!
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.
I had created the table with 200 columns and i had inserted data
Now i need to check that specific 100 columns in one row are filled or not,how can we check this using mysql query .the primary key is defined .please help me out how to resolve this.
select * from tablename where column1 != null or column2 != null ......
That is a lot of columns so at the risk of being mysql server version specific you can use the information schema to get the column names and then write a SQL procedure or something in your chosen shell / language that iterates over them performing a test.
select distinct COLUMN_NAME as 'Field', IS_NULLABLE from information_schema.columns where TABLE_SCHEMA="YourDatabase" and TABLE_NAME="YourTableName" and TABLE_NAME not like "%view%" escape '!' ;
The example above will tell you the column name as "Field" and tell you if it can hold a NULL. Having the field name may give you a better way of automating a field name specific test.
I want to create a backwards compatible query on SYS.ALL_ARGUMENTS. In Oracle 11g, the useful ALL_ARGUMENTS.DEFAULTED column was added. Now if I run this query against Oracle 10g:
SELECT defaulted FROM all_arguments
I get an error, of course.
ORA-00904: "SYS"."ALL_ARGUMENTS"."DEFAULTED": invalid identifier
What I'd like to do is this:
SELECT CASE WHEN column_exists("defaulted")
THEN defaulted
ELSE 'N'
END
FROM all_arguments
Or even better
SELECT evaluate_column_on_current_row(column_name => "defaulted",
default_if_not_exists => 'N')
FROM all_arguments
Is there some way to do that in a single SQL query, without resorting to PL/SQL? Or should I check the Oracle version first like this:
SELECT count(*)
FROM all_tab_cols
WHERE owner = 'SYS'
AND table_name = 'ALL_ARGUMENTS'
AND column_name = 'DEFAULTED'
A query that references a column that doesn't exist can't generate a valid plan.
You need to choose an approach where the queries submitted are always valid. Be that dynamically generating/executing them, or some other approach.
But if you submit a query to be parsed, and it contains a non existant field on an existant table, the parser will throw it back at you.