Programming Program for Information Processing (Language Elements)



Tokens

The basic syntactical units of the language are called tokens. A token consists of one or more characters, excluding blanks, control characters, and characters within a string constant or delimited identifier. (These terms are defined later.)
Tokens are classified as ordinary or delimiter tokens:
  • An ordinary token is a numeric constant, an ordinary identifier, a host identifier, or a keyword.
    Examples
        1        .1        +2        SELECT        E        3
  • delimiter token is a string constant, a delimited identifier, an operator symbol, or any of the special characters shown in the syntax diagrams. A question mark (?) is also a delimiter token when it serves as a parameter marker, as explained under prepare.
    Examples
        ,      'Myst Island'        "fld1"        =        .

Spaces:

space is a sequence of one or more blank characters.

Control Characters:

control character is a special character that is used for string alignment. The following table contains the control characters that are handled by the database manager:
Table 1. Control Characters
Control CharacterEBCDIC Hex ValueUnicode Graphic Hex Value
Tab05U+0009
Form Feed0CU+000C
Carriage Return0DU+000D
New Line15U+0085
Line Feed (New line)25U+000A
DBCS SpaceU+3000

Data types

The smallest unit of data that can be manipulated in SQL is called a value.
How values are interpreted depends on the  attributes of their source, which includes the data type, length, precision, scale, and CCSID. The sources of values are:
  • Columns
  • Constants
  • Expressions
  • Functions
  • Special registers
  • Variables (such as host variables, SQL variables, global variables, parameter markers and parameters of routines)

  

Promotion of data types

Data types can be classified into groups of related data types. Within such groups, an order of precedence exists where one data type is considered to precede another data type. This precedence enables the database manager to support the promotion of one data type to another data type that appears later in the precedence ordering. For example, the data type CHAR can be promoted to VARCHAR; INTEGER can be promoted to DOUBLE PRECISION; but CLOB is NOT promotable VARCHAR.
The database manager considers the promotion of data types when:
  • performing function resolution
  • casting distinct types 
  • assigning distinct types to built-in data types 
For each data type, Table 1 shows the precedence list (in order) that the database manager uses to determine the data types to which each data type can be promoted. The table indicates that the best choice is the same data type and not promotion to another data type. Note that the table also shows data types that are considered equivalent during the promotion process. For example, CHARACTER and GRAPHIC are considered to be equivalent data types.
Table 1. Data Type Precedence Table
Data TypeData Type Precedence List (in best-to-worst order)
SMALLINTSMALLINT, INTEGER, BIGINT, decimal, real, double, DECFLOAT
INTEGERINTEGER, BIGINT, decimal, real, double, DECFLOAT
BIGINTBIGINT, decimal, real, double, DECFLOAT
decimaldecimal, real, double, DECFLOAT
realreal, double, DECFLOAT
doubledouble, DECFLOAT
DECFLOATDECFLOAT
CHAR or GRAPHICCHAR or GRAPHIC, VARCHAR or VARGRAPHIC, CLOB or DBCLOB
VARCHAR or VARGRAPHICVARCHAR or VARGRAPHIC, CLOB or DBCLOB
CLOB or DBCLOBCLOB or DBCLOB
CHAR FOR BIT DATACHAR, VARCHAR, CLOB, BINARY, VARBINARY, BLOB
VARCHAR FOR BIT DATAVARCHAR, CLOB, VARBINARY, BLOB
BINARYBINARY, VARBINARY, BLOB, CHAR FOR BIT DATA, VARCHAR FOR BIT DATA
VARBINARYVARBINARY, BLOB, VARCHAR FOR BIT DATA
BLOBBLOB
DATEDATE
TIMETIME
TIMESTAMPTIMESTAMP
DATALINKDATALINK
ROWIDROWID
XML  XML 
 ARRAY  ARRAY 
udtsame udt
Note:
The lower case types above are defined as follows:
decimal
= DECIMAL(p,s) or NUMERIC(p,s)
real
= REAL or FLOAT(n) where n is a specification for single precision floating point
double
= DOUBLE, DOUBLE PRECISION, FLOAT or FLOAT(n) where n is a specification for double precision floating point
udt
= a user-defined type
Shorter and longer form synonyms of the data types listed are considered to be the same as the synonym listed.
Character and graphic strings are only compatible for Unicode data.

Casting between data types

There are many occasions when a value with a given data type needs to be cast (changed) to a different data type or to the same data type with a different length, precision, or scale.
Data type promotion, as described in Promotion of data types, is one example of when a value with one data type needs to be cast to a new data type. A data type that can be changed to another data type is castable from the source data type to the target data type.
The casting of one data type to another can occur implicitly or explicitly. The cast functions or CAST specification  can be used to explicitly change a data type. The database manager might implicitly cast data types during assignments that involve a distinct type . In addition, when you create a sourced user-defined function, the data types of the parameters of the source function must be castable to the data types of the function that you are creating.
If truncation occurs when a character or graphic string is cast to another data type, a warning occurs if any non-blank characters are truncated. This truncation behavior is similar to retrieval assignment of character or graphic strings 
If truncation occurs when a binary string is cast to another data type, a warning occurs. This truncation behavior is similar to retrieval assignment of binary strings.
For casts that involve an array type, the source and target data type must both be the same array type.
For casts that involve a distinct type as either the data type to be cast to or from, Table 1 shows the supported casts. For casts between built-in data types, Table shows the supported casts.
Table 1. Supported Casts When a Distinct Type is Involved
Data Type ...Is Castable to Data Type ...
Distinct type DTSource data type of distinct type DT
Source data type of distinct type DTDistinct type DT
Distinct type DTDistinct type DT
Data type ADistinct type DT where A is promotable to the source data type of distinct type DT (see Promotion of data types)
INTEGERDistinct type DT if DT's source type is SMALLINT
DOUBLEDistinct type DT if DT's source data type is REAL
VARCHARDistinct type DT if DT's source data type is CHAR or GRAPHIC
VARGRAPHICDistinct type DT if DT's source data type is GRAPHIC or CHAR
VARBINARYDistinct type DT if DT's source data type is BINARY
Character and graphic strings are only compatible for Unicode data. Character bit data and graphic strings are not compatible.
When a distinct type is involved in a cast, a cast function that was generated when the distinct type was created is used. How the database manager chooses the function depends on whether function notation or the CAST specification syntax is used. For details, see Function resolution, and CAST specification. Function resolution is used for both. However, in a CAST specification, when an unqualified distinct type is specified as the target data type, the database manager resolves the schema name of the distinct type and then uses that schema name to locate the cast function.
The following table describes the supported casts between built-in data types.
Table 2. Supported Casts Between Built-In Data Types



Target Data Type →



Source Data Type ↓
  SMALLINT
  INTEGER
  BIGINT
  DECIMAL
  NUMERIC
  REAL
  DOUBLE
  DECFLOAT
  CHAR
  VARCHAR
  CLOB
  GRAPHIC
  VARGRAPHIC
  DBCLOB
  BINARY
  VARBINARY
  BLOB
  DATE
  TIME
  TIMESTAMP
  ROWID
  DATALINK
  XML
SMALLINTYYYYYY1
INTEGERYYYYYY1
BIGINTYYYYYY1
DECIMALYYYYYY1
NUMERICYYYYYY1
REALYYYYYY1
DOUBLEYYYYYY1
DECFLOATYYYYYY1
CHARYYYYYY1 Y YYYY
VARCHARYYYYYY1 Y YYYY
CLOBYYYYYY1 Y Y
GRAPHICY1Y1Y1Y1Y1YYY1Y1Y1
VARGRAPHICY1Y1Y1Y1Y1YYY1Y1Y1
DBCLOBY1Y1Y1Y1Y1YY
BINARYYY
VARBINARYYY
BLOBY
DATEYYY
TIMEYYY
TIMESTAMPYYYY
ROWIDYYY
DATALINKY
XMLY



Assignments and comparisons

The basic operations of SQL are assignment and comparison. Assignment operations are performed during the execution of CALL, INSERT, UPDATE, FETCH, SELECT, SET variable, and VALUES INTO statements. Comparison operations are performed during the execution of statements that include predicates and other language elements such as MAX, MIN, DISTINCT, GROUP BY, and ORDER BY.
The basic rule for both operations is that the data type of the operands involved must be compatible. The compatibility rule also applies to UNION, EXCEPT, INTERSECT, concatenation, CASE expressions, and the CONCAT, VALUE, COALESCE, IFNULL, MIN, and MAX scalar functions.

Conversion rules for operations that combine strings

The operations that combine strings are concatenation, UNION, UNION ALL, EXCEPT, and INTERSECT. (These rules also apply to the MAX, MIN, VALUE, COALESCE, IFNULL, and CONCAT scalar functions and CASE expressions.) In each case, the CCSID of the result is determined at bind time, and the execution of the operation may involve conversion of strings to the coded character set identified by that CCSID.

Constants

constant (also called a literal) specifies a value. Constants are classified as string constants or numeric constants. String constants are further classified as character or graphic. Numeric constants are further classified as integer, floating point, or decimal.

Special registers

special register is a storage area that is defined for an application process by database manager and is used to store information that can be referenced in SQL statements. A reference to a special register is a reference to a value provided by the current server. If the value is a string, its CCSID is a default CCSID of the current server.
The special registers can be referenced as follows:
Read syntax diagramSkip visual syntax diagram
>>-+-+-CURRENT CLIENT_ACCTNG-+------------+--------------------><
   | '-CLIENT ACCTNG---------'            |   
   +-+-CURRENT CLIENT_APPLNAME-+----------+   
   | '-CLIENT APPLNAME---------'          |   
   +-+-CURRENT CLIENT_PROGRAMID-+---------+   
   | '-CLIENT PROGRAMID---------'         |   
   +-+-CURRENT CLIENT_USERID-+------------+   
   | '-CLIENT USERID---------'            |   
   +-+-CURRENT CLIENT_WRKSTNNAME-+--------+   
   | '-CLIENT WRKSTNNAME---------'        |   
   +-+-CURRENT DATE-----+-----------------+   
   | |              (1) |                 |   
   | '-CURRENT_DATE-----'                 |   
   +---CURRENT DEBUG MODE-----------------+   
   +---CURRENT DECFLOAT ROUNDING MODE-----+   
   +---CURRENT DEGREE---------------------+   
   +---CURRENT IMPLICIT XMLPARSE OPTION---+   
   +-+-CURRENT PATH----------+------------+   
   | +-CURRENT FUNCTION PATH-+            |   
   | |              (1)      |            |   
   | '-CURRENT_PATH----------'            |   
   +-+-CURRENT SCHEMA-----+---------------+   
   | |                (1) |               |   
   | '-CURRENT_SCHEMA-----'               |   
   +-+-CURRENT SERVER-+-------------------+   
   | '-CURRENT_SERVER-'                   |   
   +-+-CURRENT TIME-----+-----------------+   
   | |              (1) |                 |   
   | '-CURRENT_TIME-----'                 |   
   +-+-CURRENT TIMESTAMP-----+------------+   
   | |                   (1) |            |   
   | '-CURRENT_TIMESTAMP-----'            |   
   +-+-CURRENT TIMEZONE-+-----------------+   
   | '-CURRENT_TIMEZONE-'                 |   
   +-+-SESSION_USER-+---------------------+   
   | |      (1)     |                     |   
   | '-USER---------'                     |   
   '---SYSTEM_USER------------------------'   

Column names

The meaning of a column name depends on its context.
A column name can be used to:
  • Declare the name of a column, as in a CREATE TABLE statement.
  • Identify a column, as in a CREATE INDEX statement.
  • Specify values of the column, as in the following contexts:
    • In an aggregate function, a column name specifies all values of the column in the group or intermediate result table to which the function is applied. Groups and intermediate result tables are explained under Quiries. For example, MAX(SALARY) applies the function MAX to all values of the column SALARY in a group.
    • In a GROUP BY or ORDER BY clause, a column name specifies all values in the intermediate result table to which the clause is applied. For example, ORDER BY DEPT orders an intermediate result table by the values of the column DEPT.
    • In an expression, a search condition, or a scalar function, a column name specifies a value for each row or group to which the construct is applied. For example, when the search condition CODE = 20 is applied to some row, the value specified by the column name CODE is the value of the column CODE in that row.
  • Provide a column name for an expression to temporarily rename a column, as in the correlation-clause of a table-reference in a FROM clause, or in the AS clause in the select-clause.

Variables

variable in an SQL statement specifies a value that can be changed when the SQL statement is executed.
There are several types of variables used in SQL statements:
 global variable 
Global variables are defined using the CREATE VARIABLE statement. For more information about how to refer to global variables .
host variable
Host variables are defined by statements of a host language. For more information about how to refer to host variables.
transition variable
Transition variables are defined in a trigger and refer to either the old or new values of columns. For more information about how to refer to transition variables .
SQL variable
SQL variables are defined by an SQL compound statement in an SQL function, SQL procedure, or trigger. For more information about SQL variables.
SQL parameter
SQL parameters are defined in an CREATE FUNCTION (SQL Scalar), CREATE FUNCTION (SQL Table), or CREATE PROCEDURE (SQL) statement. For more information about SQL parameters.
parameter marker
Variables cannot be referenced in dynamic SQL statements. Parameter markers are defined in an SQL descriptor and used instead. For more information about parameter markers
Source http://www-01.ibm.com/support/knowledgecenter/ssw_ibm_i_71/db2/rbafzsqlelem.htm

Walang komento:

Mag-post ng isang Komento