I just created a table named TELE by running the following query:
CREATE TABLE TELE
(NAME2 VARCHAR(15) NOT NULL,
NAME1 VARCHAR(12) NOT NULL,
PHONE CHAR(4));
Now, I am trying to populate it with data from the DSN8A10.EMP table, by running the following query:
INSERT INTO TELE
SELECT LASTNAME, FIRSTNME, PHONENO
FROM DSN8A10.EMP
WHERE WORKDEPT = 'D21';
But I get the following error:
[42704][-204] "DSN8A10.EMP" is an undefined name.. SQLCODE=-204,
SQLSTATE=42704, DRIVER=4.23.42.
I am using IntelliJ IDEA with com.ibm.db2.jcc.DB2Driver Data Server Driver.
Can you help me with a solution, please?
Thanks in advance!
Some possibilities:
the table does not exist because you have a typo in the schema name or the table name
the table does exist but in a different database
the table exists in the database, but the name or schema has a MiXed case, in which case you must use double quotes around the schema name and table name. So "DSN8a10"."emp" is DIFFERENT from DSN8a10.EMP.
If the Db2-server runs on Linux/Unix/Windows, this query may help to show a mixed case name. It's possible the table is a view or alias or nickname.
select tabschema, tabname from syscat.tables where upper(tabschema)='DSN8A10' and upper(tabname) = 'EMP'
If the Db2-server runs on i-Series: use QSYS2.SYSTABLES instead.
select table_schema, table_name from qsys2.systables where upper(table_schema)='DSN8A10' and table_name='EMP'
If the Db2-server runs on Z/OS: use SYSIBM.SYSTABLES instead:
select creator, name from sysibm.systables where upper(creator)='DSN8A10' and upper(name) = 'EMP'
Related
I found the table name exist in my system, I used the following query;
select * from sys.tables where name = '%openpotab%';
But how can I find the database name using the table name?
thanks in advance,
Bhavesh
You could use:
select DB_NAME(DB_ID()), * from sys.tables where name LIKE '%openpotab%';
Is it possible to find out when a table was renamed? (as a primary question)
And if possible which user did it?
You can try this:
SELECT Name, modify_date, * FROM sys.all_objects
WHERE Name = 'Table name'
Is there some SQL that will either return a list of table names or (to cut to the chase) that would return a boolean as to whether a tablename with a certain pattern exists?
Specifically, I need to know if there is a table in the database named INV[Bla] such as INVclay, INVcherri, INVkelvin, INVmorgan, INVgrandFunk, INVgobbledygook, INV2468WhoDoWeAppreciate, etc. (the INV part is what I'm looking for; the remainder of the table name could be almost anything).
IOW, can "wildcards" be used in a SQL statement, such as:
SELECT * tables
FROM database
WHERE tableName = 'INV*'
or how would this be accomplished?
This should get you there:
SELECT *
FROM INFORMATION_SCHEMA.TABLES
where table_name LIKE '%INV%'
EDIT:
fixed table_name
To check for exists:
--
-- note that the sql compiler knows that it just needs to check for existence, so this is a case where "select *" is just fine
if exists
(select *
from [sys].[tables]
where upper([name]) like N'INV%')
select N'do something appropriate because there is a table based on this pattern';
You can try the following:
SELECT name FROM sys.tables where name LIKE 'INV%';
I have a list of tables, and I want to check which ones do not currently exist in the target database, I can't figure out a query to return only the tables from the list that do not exist? I am running DB2 9.7.
If the list of tables you want to check against is in a form that you can query it would be something like this. The query below will return all tables NOT in the select table query (that you'll have to provide):
select * from sysibm.systables
where owner = 'SCHEMA'
and type = 'T'
and name not in ( /* select query to return your list of tables */ );
Update post comment:
If the tables are listed in a flat file (.txt, .csv) and the number is manageable. You should be able to list them out in a coma seperated form like this (at least you can with other sql languages I'm more familiar with).
select * from sysibm.systables
where owner = 'SCHEMA'
and type = 'T'
and name not in ( 'table1', 'table2', 'table3', 'table4', 'tableA', 'tableB' );
Otherwise you might have to build a quick temp table to import all the table names into and go with the first example still.
Update post post comment:
And finally, after your most recent comment, I realize I was mis-understanding your question and had it backwards. To flip it and find the tables from the list that aren't in the SCHEMA you'd do something like this. (after importing the list into a temporary table).
select mytablename from templistoftables
where mytablename not in
(select name from sysibm.systables
where owner = 'SCHEMA'
and type = 'T');
I am doing a select * on a postgresql table, and everything looks good. But if I do:
SELECT Name from People
It says:
ERROR: column People.Name does not exist
SQL state: 42703
Character: 8
But the name column shows up during select *. I've tried:
SELECT People.Name from People
as well, with the same result. Am I missing something? It should be pretty easy to do this in any other database.
Try putting quotation marks around the column name, i.e. "Name"
PostgreSQL converts everything to lowercase.
If you asks for this:
CREATE TABLE People
{
id SERIAL,
Att1 varchar(100)
-- constraints
};
SELECT Name FROM People;
You should get all the information, because pgsql converts this to
CREATE TABLE people
{
id SERIAL,
att1 varchar(100),
-- constraints
};
SELECT name FROM people;
If you built your table with pgAdmin and created field with mixed casing, you will need to quote them to be sucessfull, like this:
SELECT "Att1" FROM people
Name is a keyword, so it might not be handled well in this case. The best thing to do would be to alias the table like this:
SELECT Name from Peoplep
and then use the p to select the column:
SELECTp.Namefrom People p