Is there any term like 'DOT(.) notation' used in SQL joins?
if practised, pls explain how to use it.
Thanks in advance.
Yes here is how you do it
When you do your SELECT
SELECT firstname, lastname from dbo.names n -- The n becomes an alias
JOIN address a --- another alias
on a.userid = n.userid
Collated from multiple sources of official documentation.
Dot notation (sometimes called the membership operator) allows you to qualify an SQL identifier with another SQL identifier of which it is a component. You separate the identifiers with the period ( . ) symbol. For example, you can qualify a column name with any of the following SQL identifiers:
Table name: table_name.column_name
View name: view_name.column_name
Synonym name: syn_name.column_name
These forms of dot notation are called column projections.
You can also use dot notation to directly access the fields of a named or unnamed ROW column, as in the following example:
row-column name.field name
This use of dot notation is called a field projection. For example, suppose you have a column called rect with the following definition:
CREATE TABLE rectangles
(
area float,
rect ROW(x int, y int, length float, width float)
)
The following SELECT statement uses dot notation to access field length of the rect column:
SELECT rect.length FROM rectangles WHERE area = 64
Selecting Nested Fields
When the ROW type that defines a column itself contains other ROW types, the column contains nested fields. Use dot notation to access these nested fields within a column.
For example, assume that the address column of the employee table contains the fields: street, city, state, and zip. In addition, the zip field contains the nested fields: z_code and z_suffix. A query on the zip field returns values for the z_code and z_suffix fields. You can specify, however, that a query returns only specific nested fields. The following example shows how to use dot notation to construct a SELECT statement that returns rows for the z_code field of the address column only:
SELECT address.zip.z_code
FROM employee
Rules of Precedence
The database server uses the following precedence rules to interpret dot notation:
schema name_a . table name_b . column name_c . field name_d
table name_a . column name_b . field name_c . field name_d
column name_a . field name_b . field name_c . field name_d
When the meaning of an identifier is ambiguous, the database server uses precedence rules to determine which database object the identifier specifies. Consider the following two tables:
CREATE TABLE b (c ROW(d INTEGER, e CHAR(2));
CREATE TABLE c (d INTEGER);
In the following SELECT statement, the expression c.d references column d of table c (rather than field d of column c in table b) because a table identifier has a higher precedence than a column identifier:
SELECT *
FROM b,c
WHERE c.d = 10
For more information about precedence rules and how to use dot notation with ROW columns, see the IBM Informix: Guide to SQL Tutorial.
Using Dot Notation with Row-Type Expressions
Besides specifying a column of a ROW data type, you can also use dot notation with any expression that evaluates to a ROW type. In an INSERT statement, for example, you can use dot notation in a subquery that returns a single row of values. Assume that you created a ROW type named row_t:
CREATE ROW TYPE row_t (part_id INT, amt INT)
Also assume that you created a typed table named tab1 that is based on the row_t ROW type:
CREATE TABLE tab1 OF TYPE row_t
Assume also that you inserted the following values into table tab1:
INSERT INTO tab1 VALUES (ROW(1,7));
INSERT INTO tab1 VALUES (ROW(2,10));
Finally, assume that you created another table named tab2:
CREATE TABLE tab2 (colx INT)
Now you can use dot notation to insert the value from only the part_id column of table tab1 into the tab2 table:
INSERT INTO tab2
VALUES ((SELECT t FROM tab1 t
WHERE part_id = 1).part_id)
The asterisk form of dot notation is not necessary when you want to select all fields of a ROW-type column because you can specify the column name alone to select all of its fields. The asterisk form of dot notation can be quite helpful, however, when you use a subquery, as in the preceding example, or when you call a user-defined function to return ROW-type values.
Suppose that a user-defined function named new_row returns ROW-type values, and you want to call this function to insert the ROW-type values into a table. Asterisk notation makes it easy to specify that all the ROW-type values that the new_row( ) function returns are to be inserted into the table:
INSERT INTO mytab2 SELECT new_row (mycol).* FROM mytab1
References to the fields of a ROW-type column or a ROW-type expression are not allowed in fragment expressions. A fragment expression is an expression that defines a table fragment or an index fragment in SQL statements like CREATE TABLE, CREATE INDEX, and ALTER FRAGMENT.
Additional Examples of How to Specify Names With the Dot Notation
Dot notation is used for identifying record fields, object attributes, and items inside packages or other schemas. When you combine these items, you might need to use expressions with multiple levels of dots, where it is not always clear what each dot refers to. Here are some of the combinations:
Field or Attribute of a Function Return Value
func_name().field_name
func_name().attribute_name
Schema Object Owned by Another Schema
schema_name.table_name
schema_name.procedure_name()
schema_name.type_name.member_name()
Packaged Object Owned by Another User
schema_name.package_name.procedure_name()
schema_name.package_name.record_name.field_name
Record Containing an Object Type
record_name.field_name.attribute_name
record_name.field_name.member_name()
Differences in Name Resolution Between PL/SQL and SQL
The name resolution rules for PL/SQL and SQL are similar. You can avoid the few differences if you follow the capture avoidance rules. For compatibility, the SQL rules are more permissive than the PL/SQL rules. SQL rules, which are mostly context sensitive, recognize as legal more situations and DML statements than the PL/SQL rules.
PL/SQL uses the same name-resolution rules as SQL when the PL/SQL compiler processes a SQL statement, such as a DML statement. For example, for a name such as HR.JOBS, SQL matches objects in the HR schema first, then packages, types, tables, and views in the current schema.
PL/SQL uses a different order to resolve names in PL/SQL statements such as assignments and procedure calls. In the case of a name HR.JOBS, PL/SQL searches first for packages, types, tables, and views named HR in the current schema, then for objects in the HR schema.
Related
I have I table consisting of 3 columns: system, module and block. Table is filled in a procedure which accepts system, module and block and then it checks if the trio is in the table:
select count(*) into any_rows_found from logs_table llt
where system=llt.system and module=llt.module and block=llt.block;
If the table already has a row containing those three values, then don't write them into the table and if it doesn't have them, write them in. The problem is, if the table has values 'system=a module=b block=c' and I query for values 'does the table have system=a module=d block=e' it returns yes, or, to be precise, any_rows_found=1. Value 1 is only not presented when I send a trio that doesn't have one of it's values in the table, for example: 'system=g module=h and block=i'. What is the problem in my query?
Problem is in this:
where system = llt.system
Both systems are the same, it is as if you put where 1 = 1, so Oracle is kind of confused (thanks to you).
What to do? Rename procedure's parameters to e.g. par_system so that query becomes
where llt.system = par_system
Another option (worse, in my opinion) is to precede parameter's name with the procedure name. If procedure's name was e.g. p_test, then you'd have
where llt.system = p_test.system
From the documentation:
If a SQL statement references a name that belongs to both a column and either a local variable or formal parameter, then the column name takes precedence.
So when you do
where system=llt.system
that is interpreted as
where llt.system=llt.system
which is always true (unless it's null). It is common to prefix parameters and local variables (e.g. with p_ or l_) to avoid confusion.
So as #Littlefoot said, either change the procedure definition to make the parameter names different to the column names, or qualify the parameter names with the procedure name - which some people prefer but I find more cumbersome, and it's easier to forget and accidentally use the wrong reference.
Root cause is alias used for table name.
where system=llt.system and module=llt.module and block=llt.block;
Table name alias in select query and input to procedure having the same name(i.e. llt
). You should consider either renaming one of them.
I have the following code:
SELECT S~CLUSTD AS ZZCLUSTD
INTO CORRESPONDING FIELDS OF TABLE #lt_viqmel_iflos
FROM viqmel AS v
LEFT OUTER JOIN stxl AS S
ON s~tdobject = #lv_qmel
AND s~tdname = v~qmnum
Select statement generates following short dump:
Only the prefixed length field can be used to read from the LRAW field or
LCHR field S~CLUSTD.
Internal table lt_viqmel_iflos is type viqmel_iflos(DB view which contains DB table QMEL) to which I appended ZZCLUSTD type char200.
The problem is that I cannot make ZZCLUSTD type LRAW in QMEL because I get the following error:
So my only option (that I am aware of) remains to select into char200 the first 200 characters of LRAW.
Is this even possible?
Or is there another way to select LRAW data?
I found the info about the topic, but unfortunately I can't adapt it to my scenario:read LRAW data
In fact, there are two questions here.
The first one is the activation error of table QMEL:
Field ZZCLUSTD does not have a preceding length field of type INT4
A DDIC table containing a column of type LCHR and LRAW, requires that it's always immediately preceded with a column of type INT2 or INT4 (although the message says only INT4).
The second question is about how to read such a field. Both columns must always be read at the same time, and the INT2/INT4 column must be "read before" the LCHR/LRAW column. The only reference I could find to explain this restriction is in the note 302788 - LCHR/LRAW fields in logical cluster tables.
The INT2 column of STXL table being named CLUSTR, the following code works:
TYPES: BEGIN OF ty_viqmel_iflos,
clustr TYPE stxl-clustr, "INT2
zzclustd TYPE stxl-clustd, "LCHR
END OF ty_viqmel_iflos.
DATA lt_viqmel_iflos TYPE TABLE OF ty_viqmel_iflos.
SELECT S~CLUSTR, S~CLUSTD AS ZZCLUSTD
INTO CORRESPONDING FIELDS OF TABLE #lt_viqmel_iflos
FROM viqmel AS v
INNER JOIN stxl AS S
ON s~tdname = v~qmnum
UP TO 100 ROWS.
NB: there is a confusion in your question, where you refer to both CLUSTD from STXL and ZZCLUSTD from QMEL. I don't understand what you are trying to achieve exactly.
NB: if you want to read the texts from the table STXL, there's another solution by calling the function module READ_TEXT_TABLE, or READ_MULTIPLE_TEXTS if you prefer. They were made available by the note 2261311. In case you don't have or can't install these function modules, you may try this gist which does the same thing. It also contains a reference to another discussion.
NB: for information, to be more precise, LRAW contains bytes, not characters, and for data clusters (case of STXL), these bytes correspond to any values (characters in the case of STXL) zipped with the statement EXPORT and are to be unzipped with IMPORT`.
We have Oracle table having default keyword(i.e in as field name) field name.Now i am querying table but unable to extract specific field data.
select a.filename,a.in from table a
Following error appears "invalid field name.
Try using double quotes.
select a."IN" from table a
You can use default (oracle reserved) keywords as the name of the columns but yes it is not advisable to use it.
Anyway, If you want to use oracle reserved keywords then you must have to enclose them in the double-quotes.
Note that oracle is case insensitive in terms of its object names until and unless it is wrapped in the double-quotes. it means if you enclose any object name in double-quotes then you must have to use them anywhere in the entire DB as case sensitive manner.
So if your table definition is:
CREATE TABLE YOUR_TABLE ("IN" NUMBER);
Then you need to use "IN" wherever you want to refer the column but if your table definition is:
CREATE TABLE YOUR_TABLE ("in" NUMBER);
Then you need to use "in" wherever you want to refer the column. -- case sensitive names.
I hope it will clear all your doubts.
Cheers!!
Is there a difference between the following two queries?
CREATE TABLE alpha(age INTEGER);
and
CREATE TABLE alpha("age" INTEGER);
I read that database objects are case INSENSITIVE unless they are kept in quotation marks.
This applies when I try to keep the TABLE object in quotation marks but isn't working for the attributes.
I guess the reason is attributes are not database objects. Is this correct?
If yes, then in what category does attributes fall under, if not database object?
Is there a difference ? yes.
when you don't use quotes, like:
CREATE TABLE alpha(age INTEGER);
oracle will store that column and table as UPPERCASE in the data dictionary.
in other words , these statements are identical:
CREATE TABLE alpha(age INTEGER);
CREATE TABLE "ALPHA"("AGE" INTEGER);
CREATE TABLE ALPHA(AGE INTEGER);
Also, any subsequent dml/ddl on the table like:
select age from alpha;
would be first converted to uppercase when looking up the object/column; so the above SQL would work fine. i.e oracle would look for the column AGE and the table ALPHA and not the column age or the table alpha as was typed.
however, when you create with a quoted identifier:
CREATE TABLE alpha("age" INTEGER);
oracle would create table ALPHA with a column in lowercase age (check user_tab_columns to see this). so ONLY the following would work to select it:
select "age" from alpha;
select "age" from "ALPHA";
select "age" from ALPHA;
and not:
select age from alpha;
eg:
http://sqlfiddle.com/#!4/3084e/1
It depends on the database engine, and even how the database itself or the connected session is configured. However, in general, SQL Server would treat age and "age" as the same identifier, while Oracle would consider age and "age" to be different.
In regard to attributes (columns), treatment of those identifiers vary widely as well. Most database engines will allow the attributes to be identified by quotation marks (or other delimiters) and in the same way the table identifiers are handled.
I have a unique column. I also have a known set of elements that are possible values for the column. I need to know which of the possible values are not already in the table, and as such, are suitable for insertion.
Is this possible with SQL or is post processing required?
Currently, I am using the "in" operator to select all rows where the column value equals an element in my set. Then I remove all matched elements from my set via post processing.
Stick the allowed values in a temporary table allowed, then use a subquery using NOT IN:
SELECT *
FROM allowed
WHERE allowed.val NOT IN (
SELECT maintable.val
)
Some DBs will allow you to build up a table "in-place", instead of having to create a separate table. E.g. in PostgreSQL (any version):
SELECT *
FROM (
SELECT 'foo'
UNION ALL SELECT 'bar'
UNION ALL SELECT 'baz' -- etc.
) inplace_allowed
WHERE inplace_allowed.val NOT IN (
SELECT maintable.val
)
More modern versions of PostgreSQL (and perhaps other DBs) will let you use the slightly nicer VALUES syntax to do the same thing.
To do this entirely in SQL you will need to create a separate table with one column. Each row holds one value from the known set of elements. Assuming the table is called ElementList and the other table is called Existing:
SELECT * FROM ElementList WHERE Element NOT IN
(SELECT DISTINCT Element FROM Existing)
Depending on what database engine you're using you may be able to use a temporary table to create and hold the list without saving it permanently in the database. However, storing the list of allowed elements is valuable for constraining the Element column in the Existing table (and for presenting the user with allowed Elements in the user interface).