can anyone please help me understand this query? - sql

Can anyone help me understand this query? The mktpgmvhclxref.mktpgmvhclxrefId.mktgPgm part mostly. Generally it is table_name.columnName but here the format seems to be different
SELECT mktpgmvhclxref.mktpgmvhclxrefid.mktgpgm,
mktpgm.mktgpgm,
mktpgmvhclxref.cmpgn,
campaign.cmpgndescription,
mktpgmvhclxref.mktpgmvhclxrefid.vhcl,
vhclhierarchy.modname,
mktpgmvhclxref.mktpgmvhclxrefid.modyr,
mktpgmvhclxref.userinsrt,
mktpgmvhclxref.rowinsrt
FROM mktpgmvhclxref mktpgmvhclxref,
mktpgm mktpgm,
vhclhierarchy vhclHierarchy,
campaign campaign
WHERE mktpgmvhclxref.mktpgmvhclxrefid.mktgpgm = mktpgm.idmktgpgm
AND mktpgmvhclxref.cmpgn = campaign.campaignid
AND mktpgmvhclxref.mktpgmvhclxrefid.vhcl = vhclhierarchy.vhcl

Often, the first identifier is the schema / database name, depending on your database:
[schema].[table].[column]
In most databases, [schema] and [table] qualifiers are optional, if the [column] name is unambiguous.
In your case, however, I doubt that this is the actual case as mktpgmvhclxref is a table in your FROM clause. Oracle for instance, also knows user-defined types (UDTs, OBJECT types). So I'm guessing that:
mktpgmvhclxref.mktpgmvhclxrefid.mktgpgm
Corresponds to
mktpgmvhclxref = table
mktpgmvhclxrefid = column
mktgpgm = UDT attribute
If you're using Oracle, you can probably find your UDT as such:
select *
from all_type_attrs
where (owner, type_name) = ((
select data_type_owner, data_type
from all_tab_cols
where table_name = 'MKTPGMVHCLXREF'
and column_name = 'MKTPGMVHCLXREFID'
))
order by attr_no

mktpgmvhclxref.mktpgmvhclxrefId.mktgPgm
[--Schema----].[----table-----].[Column]

i got the anwer to my question .
its related to hibernate mapping.
The second field is actually for composite key of a table .

Related

How can I Retrieve a List of All Table Names from a SQL Server CE database?

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%';

SQL statement for a join in dB2

The following is my sql statement for a join in dB2.
select name, address, bloodgroup
from user_tb, health_tb
where user_tb.id = health_tb.id;
I am getting the following error:
"health_tb.id" is not valid in the context where it is used..
SQLCODE=-206, SQLSTATE=42703, DRIVER=4.12.79
I understand that one reason why I could be getting this error is because id may not exist in health_tb, but that is not the case. I hope someone can advise. Thank you.
First, you should learn to use modern join syntax, although this has nothing to do with your problem:
select name, address, bloodgroup
from user_tb join
health_tb
on user_tb.id = health_tb.id;
A simple search on Google pointed me to the documentation for this error. One of the first things it mentions is:
Possible reasons for this error include:
The specified column is not a column of any of the source or target
tables or views of the statement.
In a SELECT or DELETE statement, the specified column is not a column of any of the tables or views that are identified in a FROM
clause in the statement.
A column list of an SQL data change statement specified the name of a column of the target table or view of the statement.
I suspect that the id column is really called something like user_id. The working query might look like:
select name, address, bloodgroup
from user_tb join
health_tb
on user_tb.id = health_tb.user_id;
1) check if the id column in both tables have the same data type
2) check if there is any trailing space in the column name
select '<' || column_name || '>' from user_tab_columns
where tbname = 'health_tb'
If the id columns are defined as different types, that could be a problem.

Get columns of index on DB2

How can I get the columns, which an index of a table uses, in DB2?
I tried:
DESCRIBE INDEXES FOR TABLE 'MYTABLE' SHOW DETAIL;
But I get the error message
ILLEGAL SYMBOL "INDEXES". SOME SYMBOLS THAT MIGHT BE LEGAL ARE: PROCEDURE PROC. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.16.53
Ideally I want information of all indexes a table uses with their corresponding columns.
I am using DB2 for z/OS V9.1
You can use this query to show the indexes and their columns of your tables:
SELECT IX.tbname,
KEY.ixname,
KEY.colname
FROM sysibm.syskeys KEY
JOIN sysibm.sysindexes IX
ON KEY.ixname = IX.name
WHERE IX.tbname IN ( 'SOMETABLE', 'ANOTHERTABLE' )
ORDER BY IX.tbname,
KEY.ixname,
KEY.colname;
SELECT * FROM SYSIBM.SYSKEYS WHERE IXNAME IN
(SELECT NAME FROM SYSIBM.SYSINDEXES WHERE TBNAME = 'your_table_name')
I have tested it, it is giving us all the columns which are used in indexes.
You can use below query also. it works fine if syskeys table is missing
SELECT * FROM SYSIBM.SYSINDEXCOLUSE where INDNAME IN (SELECT NAME FROM SYSIBM.SYSINDEXES si where si.TBNAME ='your_table_Name' ) ORDER BY INDNAME, COLSEQ
I had an issue with using "KEY" as a table alias. Also, if you have multiple schemas with the same table name, use the following:
SELECT IX.TABLE_SCHEMA, IX.TABLE_NAME, IX.INDEX_NAME, KY.ORDINAL_POSITION, KY.COLUMN_NAME
FROM SYSKEYS KY
JOIN SYSINDEXES IX ON (KY.INDEX_NAME = IX.INDEX_NAME AND KY.INDEX_SCHEMA = IX.INDEX_SCHEMA)
WHERE IX.TBNAME = 'table-name' AND IX.TABLE_SCHEMA = 'table-schema'
ORDER BY IX.TABLE_SCHEMA, IX.TABLE_NAME, IX.INDEX_NAME, KY.ORDINAL_POSITION
FOR READ ONLY WITH UR

mysql select all table names except

Alright, I know you can do the following to get all column names.
SHOW COLUMNS FROM person;
However, it does not allow where statements.
Is there any way to get the full list and just say not this one?
So I am basically looking for something like this:
select (show columns from person where Field <> 'id') from person
USe SHOW [FULL] COLUMNS {FROM | IN} tbl_name [{FROM | IN} db_name] [LIKE 'pattern' | WHERE expr]
Example:
SHOW CHARACTER SET WHERE `Default collation` LIKE '%japanese%';
More about show here
Try something like SHOW COLUMNS FROM person WHERE column != 'value';
Or you can use proper a SQL query against information_schema
select * from information_schema.columns
where table_schema = 'test' # your db name
and table_name = 'person '
and column_name != 'id'
For MySQL 4, you can use LIKE directly instead of WHERE http://dev.mysql.com/doc/refman/4.1/en/show-columns.html
SHOW COLUMNS FROM 'Person' LIKE '%x%'
However unfortunately, that does not support a NOT clause.
If I may suggest
If you need this information from
MySQL console/phpAdmin/equivalent,
just eyeball it..
If you need this
from a front-end program like php, do
it from that environment.
MySQL documentation says that WHERE clause is allowed. Read the docs.
You cannot perform a true SHOW COLUMNS WHERE in MySQL 4. The docs show only an optional LIKE parameter for that version: http://dev.mysql.com/doc/refman/4.1/en/show-columns.html
However, you can use the metadatabase information_schema.
USE information_schema;
SELECT * FROM `COLUMNS` WHERE `TABLE_NAME` = 'person' AND `COLUMN_NAME` != 'value';

How can I find all indexes available on a table in DB2

How to find all indexes available on table in db2?
db2 "select * from syscat.indexes where tabname = 'your table name here' \
and tabschema = 'your schema name here'"
You can also execute:
DESCRIBE INDEXES FOR TABLE SCHEMA.TABLE SHOW DETAIL
You can get the details of indexes with the below command.
describe indexes for table schemaname.tablename show detail
To see all indexes :-
select * from user_objects
where object_type='INDEX'
To see index and its columns on table :
select * from USER_IND_COLUMNS where TABLE_NAME='my_table'
This depends upon which version of DB2 you are using.
We have v7r1m0 and the following query works quite well.
WITH IndexCTE (Schema, Table, Unique, Name, Type, Columns) AS
(SELECT i.table_schema, i.Table_Name, i.Is_Unique,
s.Index_Name, s.Index_Type, s.column_names
FROM qsys2.SysIndexes i
INNER JOIN qsys2.SysTableIndexStat s
ON i.table_schema = s.table_schema
and i.table_name = s.table_name
and i.index_name = s.index_name)
SELECT *
FROM IndexCTE
WHERE schema = 'LIBDEK'
AND table = 'ECOMROUT'
If you're not familiar with CTE's they are worth getting to know. Our AS400 naming conventions are awful so I've been using CTE's to normalize field names. I ended up making a library of CTE's and have it automatically append to the top of all my queries.
For checking the indexes of a table on IBM Db2 on Cloud (previously DashDb) the following query should do it:
SELECT * FROM SYSCAT.INDEXES WHERE TABNAME = 'my_tablename' AND TABSCHEMA = 'my_table_schema'
You can use also check by index name:
SELECT COUNT(*) FROM SYSCAT.INDEXES WHERE TABNAME = 'my_tablename' AND TABSCHEMA = 'my_table_schema' AND INDNAME='index_name'
The same result can be achieved by using SYSIBM.SYSINDEXES. However, this table is not referenced directly on the product documentation page.
SELECT COUNT(*) FROM SYSIBM.SYSINDEXES WHERE TBNAME = 'my_tablename' AND TBCREATOR = 'my_table_schema' AND NAME='my_index_name'
See SYSCAT.INDEXES catalog view.
One more way is to generate the DDL of the table.
It will give you the complete description of table including index on it.
Just right click on table and click on generate DDL/Scripts.
Works on most of the database.