Вы находитесь на странице: 1из 139

Firebird 2.

0
SQL
SQL Firebird InterBase 6


24 2008, 0.9 (Firebird 1.0 2.0.4)
- 6 2009

Firebird 2.0

SQL

SQL Firebird InterBase 6


24 2008, 0.9 (Firebird 1.0 2.0.4)
- 6 2009


1. ........................................................................................................................................... 1
...................................................................................................................... 1
........................................................................................................................... 2
2. .................................................................................... 3
InterBase 6 ........................................................................................................ 3
.......................................................................................... 3
......................................................................................................... 4
InterBase 6 ............................................................................................................ 4
.......................................................................... 4
........................................................................................ 5
........................................................... 5
3. .............................................................................................................. 6
-- ( ) ................................................................................................... 6
CASE ....................................................................................................................... 6
CASE ...................................................................................................................... 7
CASE ................................................................................................................ 7
4. .................................................................................................................................... 9
BIGINT .......................................................................................................................................... 9
BLOB ............................................................................................................................................. 9
() ........................................................................................... 9
NONE .............................................................................. 11
(collation) ..................................................................... 11
5. DDL .............................................................................................................................. 13
ALTER DATABASE ....................................................................................................................... 13
BEGIN BACKUP ................................................................................................................... 13
END BACKUP ...................................................................................................................... 14
ADD DIFFERENCE FILE ....................................................................................................... 14
DROP DIFFERENCE FILE ...................................................................................................... 15
ALTER DOMAIN ........................................................................................................................... 15
SET DEFAULT .................................................. 15
ALTER EXTERNAL FUNCTION ..................................................................................................... 15
ALTER PROCEDURE ..................................................................................................................... 16
............................................................................ 16
........................................................ 16
ALTER SEQUENCE ....................................................................................................................... 16
ALTER TABLE .............................................................................................................................. 17
ADD COLUMN ................................................. 17
ALTER COLUMN: DROP DEFAULT ........................................................................................ 17
ALTER COLUMN: SET DEFAULT .......................................................................................... 18
ALTER COLUMN: POSITION , 1 ............................................... 18
CHECK , NULL ............................... 19
( FOREIGN KEY) ................... 19
(UNIQUE) NULL ........................................ 19
USING INDEX ................................................................................................ 19
ALTER TRIGGER .......................................................................................................................... 20
......................................................................................... 20
........................................................ 20
..................................................... 21
iv

Firebird 2.0 - SQL


ALTER TRIGGER ......................... 21
COMMENT ................................................................................................................................... 21
CREATE DATABASE ..................................................................................................................... 22

16 Kb .................................................................................. 22

CREATE DOMAIN ........................................................................................................................ 22

............................... 23
CREATE GENERATOR ................................................................................................................... 23
CREATE SEQUENCE ......................................................... 23
.......................................... 23
CREATE INDEX ............................................................................................................................ 23
(UNIQUE) NULL ................................................ 24
.................................................................................................... 24
.................................................................. 24
.............................................. 25
CREATE PROCEDURE ................................................................................................................... 26
CREATE SEQUENCE ..................................................................................................................... 26
CREATE TABLE ............................................................................................................................ 27
NULL CHECK ............................................................................................. 27
............... 28
( FOREIGN KEY) ................... 28
NULL ................................................................... 28
USING INDEX ................................................................................................ 30
CREATE TRIGGER ........................................................................................................................ 31
......................................................................................... 32
CREATE TRIGGER ....................... 32
..................................................... 33
CREATE VIEW ............................................................................................................................. 33
SELECT .............................................................. 33
Firebird 1.5 ............................ 33
(updatable views) ................ 34
CREATE OR ALTER EXCEPTION ................................................................................................... 34
CREATE OR ALTER PROCEDURE .................................................................................................. 34
CREATE OR ALTER TRIGGER ....................................................................................................... 35
DECLARE EXTERNAL FUNCTION ................................................................................................ 35
BY DESCRIPTOR ............................................................. 35
RETURNS PARAMETER n ............................................... 36
DECLARE FILTER ........................................................................................................................ 36
DROP GENERATOR ...................................................................................................................... 37
DROP PROCEDURE ...................................................................................................................... 37
........................................................... 37
DROP SEQUENCE ........................................................................................................................ 38
DROP TRIGGER ........................................................................................................................... 38
.......................................................... 38
DROP TRIGGER .......................... 38
RECREATE EXCEPTION ................................................................................................................ 39
RECREATE PROCEDURE .............................................................................................................. 39
.................................................... 39
RECREATE TABLE ....................................................................................................................... 40
RECREATE TRIGGER .................................................................................................................... 40
.................................................... 40
RECREATE VIEW ......................................................................................................................... 40
REVOKE ADMIN OPTION ............................................................................................................. 41

Firebird 2.0 - SQL


SET GENERATOR .........................................................................................................................
6. DML .............................................................................................................................
DELETE .......................................................................................................................................
ORDER BY ...........................................................................................................................
PLAN ...................................................................................................................................
ROWS ..................................................................................................................................
EXECUTE BLOCK ........................................................................................................................
EXECUTE PROCEDURE ................................................................................................................
INSERT ........................................................................................................................................
RETURNING ..................................................................................................
INSERT..SELECT UNION ............................................
SELECT .......................................................................................................................................
: .....................................................
COLLATE BLOB- ............................................................................
(SELECT FROM SELECT) ..............................................................
FIRST SKIP .................................................................................................
GROUP BY ...........................................................................................................................
JOIN ....................................................................................................................................
ORDER BY ...........................................................................................................................
PLAN ...................................................................................................................................
ROWS ..................................................................................................................................
...................................................
UNION .................................................................................................................................
WITH LOCK .........................................................................................................................
UPDATE .......................................................................................................................................
ORDER BY ...........................................................................................................................
PLAN ...................................................................................................................................
ROWS ..................................................................................................................................
7. ...............................................................................................
RELEASE SAVEPOINT ..................................................................................................................
ROLLBACK ..................................................................................................................................
ROLLBACK RETAIN .............................................................................................................
ROLLBACK TO SAVEPOINT ..................................................................................................
SAVEPOINT ..................................................................................................................................
() ...........................................................................
PSQL ..................................................................................................
SET TRANSACTION .....................................................................................................................
IGNORE LIMBO ...................................................................................................................
LOCK TIMEOUT ...................................................................................................................
NO AUTO UNDO ..................................................................................................................
8. PSQL ............................................................................................................................
BEGIN ... END ..................................................................................
BREAK ........................................................................................................................................
CLOSE .........................................................................................................................................
DECLARE CURSOR ......................................................................................................................
DECLARE [VARIABLE] ...................................................................................
EXCEPTION .................................................................................................................................
..................................................................
.............................................
EXECUTE PROCEDURE ................................................................................................................
EXECUTE STATEMENT .................................................................................................................
............................................................................................

vi

41
42
42
42
42
43
43
45
47
47
48
48
48
50
50
52
53
54
56
58
59
60
61
62
63
64
64
64
66
66
66
66
67
67
69
69
69
70
70
70
72
72
72
73
73
74
74
75
75
75
76
76

Firebird 2.0 - SQL


.......................................................................................... 76
........................................................ 77
EXECUTE STATEMENT ..................................................................... 77
EXIT ............................................................................................................................................ 78
FETCH ......................................................................................................................................... 78
FOR EXECUTE STATEMENT ... DO ................................................................................................ 79
LEAVE ......................................................................................................................................... 79
OPEN ........................................................................................................................................... 80
PLAN ........................................................................................................ 80
UDF ......................................................................................................... 81
9. ............................................................................................................... 82
CURRENT_CONNECTION ............................................................................................................. 82
CURRENT_ROLE .......................................................................................................................... 82
CURRENT_TIME .......................................................................................................................... 83
CURRENT_TIMESTAMP ............................................................................................................... 83
CURRENT_TRANSACTION ........................................................................................................... 84
CURRENT_USER .......................................................................................................................... 84
DELETING ................................................................................................................................... 85
GDSCODE ..................................................................................................................................... 85
INSERTING ................................................................................................................................ 86
NEW ............................................................................................................................................. 86
'NOW' ......................................................................................................................................... 87
OLD ............................................................................................................................................. 87
ROW_COUNT ................................................................................................................................ 88
SQLCODE ..................................................................................................................................... 88
UPDATING ................................................................................................................................... 89
10. ................................................................................................................ 90
NULL ........................................................................................................ 90
|| ( ) ................................................................................................ 90
...................................................................................................... 90
UNION ................................................................................................................. 90
IS [NOT] DISTINCT FROM ............................................................................................................ 91
NEXT VALUE FOR ....................................................................................................................... 92
11. .................................................................................................................... 93
BIT_LENGTH() ............................................................................................................................. 93
CAST() ......................................................................................................................................... 93
CHAR_LENGTH(), CHARACTER_LENGTH() ................................................................................... 95
COALESCE() ................................................................................................................................ 95
EXTRACT() .................................................................................................................................. 96
GEN_ID() ..................................................................................................................................... 97
IIF() ............................................................................................................................................. 97
LOWER() ...................................................................................................................................... 98
NULLIF() ...................................................................................................................................... 98
OCTET_LENGTH() ........................................................................................................................ 99
RDB$GET_CONTEXT() .................................................................................................................. 99
RDB$SET_CONTEXT() ................................................................................................................ 100
SUBSTRING() ............................................................................................................................. 101
TRIM() ....................................................................................................................................... 102
UPPER() ..................................................................................................................................... 103
12. (UDF) ............................................................................................................ 104
/ .................................................................................... 104
addDay ............................................................................................................................. 104

vii

Firebird 2.0 - SQL


addHour ........................................................................................................................... 105
addMilliSecond ............................................................................................................ 105
addMinute ....................................................................................................................... 105
addMonth ......................................................................................................................... 106
addSecond ....................................................................................................................... 106
addWeek ........................................................................................................................... 107
addYear ........................................................................................................................... 107
dow ................................................................................................................................... 108
getExactTimestamp ...................................................................................................... 108
sdow ................................................................................................................................. 108
............................................................................................ 109
ascii_char .................................................................................................................... 109
lower ............................................................................................................................... 110
lpad ................................................................................................................................. 110
ltrim ............................................................................................................................... 111
rpad ................................................................................................................................. 111
right ............................................................................................................................... 112
rtrim ............................................................................................................................... 113
string2blob .................................................................................................................. 113
substr ............................................................................................................................. 114
substrlen ....................................................................................................................... 114
......................................................................................................... 115
dpower ............................................................................................................................. 115
log ................................................................................................................................... 115
rand ................................................................................................................................. 116
round, i64round ............................................................................................................ 116
srand ............................................................................................................................... 117
truncate, i64truncate ............................................................................................... 118
*nullif ................................................................................................................................... 119
*nvl ......................................................................................................................................... 120
A: ............................................................................................................. 122
NONE .............................................................. 122
WITH LOCK ................................................................................. 123
......................................................................................................................... 123
WITH LOCK ............................................................................... 125
WITH LOCK .......................................................................... 125
.................................................. 126
CSTRING ......................................................................................................... 126
NULL UDF ............................................................................................... 127
Firebird ........................................... 127
B: ............................................................................................ 129
C: ...................................................................................... 130

viii


4.1. , Firebird ................................................................................... 10
4.2. (collation) Firebird ........................................................ 12
5.1. ( CHAR/VARCHAR) ........................................ 25
5.2. , Firebird 2.0 ....................................................... 25
5.3. NULL ....................................................................... 29
6.1. NULL- ...................................................................................................... 57
10.1. [NOT] DISTINCT = <> ................................................................ 91
11.1. CAST ................................................................. 94
11.2. EXTRACT ................................................................................ 96
11.3. SYSTEM ........................................................ 100
A.1. .................................................. 124
A.2. Firebird 1.0 2.0 .......................................... 128

ix

, SQL Firebird
InterBase 6.0 Firebird 2.0.4. :



DDL- (Data Definition Language - )
DML- (Data Manipulation Language - )

PSQL- (Procedural SQL - , )



UDF (User Defined Functions - )

SQL Firebird 2.0


SQL InterBase 6.0 beta ( LangRef.pdf / SQLRef.html)

, :

ODS (On-Disk Structure - )




,

API

Release Notes Firebird. Release Notes


: http://www.firebirdsql.org/index.php?op=doc.


Firebird 2.0.4 .


90% . Release Notes,
- Whatsnew . :

J. Beesley
Helen Borrie
Arno Brinkman
Alex Peshkov
Nickolay Samofatov
Dmitry Yemanov


SQL Firebird.
(, ), ,
(Dialect 3). ,
, .
. ,
, ,
(. . - ).

InterBase 6

Firebird :
BIGINT
BIT_LENGTH
BOTH
CASE
CHAR_LENGTH
CHARACTER_LENGTH
CLOSE
CROSS
CURRENT_CONNECTION
CURRENT_ROLE
CURRENT_TRANSACTION
CURRENT_USER
FETCH
LEADING
LOWER
OCTET_LENGTH
OPEN
RECREATE
RELEASE
ROW_COUNT
ROWS


SAVEPOINT
TRAILING
TRIM
USING


:
BACKUP
BLOCK
COALESCE
COLLATION
COMMENT
DELETING
DIFFERENCE
IIF
INSERTING
LAST
LEAVE
LOCK
NEXT
NULLIF
NULLS
RESTART
RETURNING
SCALAR_ARRAY
SEQUENCE
STATEMENT
UPDATING

InterBase 6

Firebird 2.0,
:
ACTION
CASCADE
FREE_IT
RESTRICT
ROLE
TYPE
WEEKDAY
YEARDAY


Firebird 2.0:
BASENAME
CACHE
CHECK_POINT_LEN
GROUP_COMMIT_WAIT
LOG_BUF_SIZE
LOGFILE
NUM_LOG_BUFS
RAW_PARTITIONS



Firebird 2.0,
, , , :
ABS
BOOLEAN
FALSE
TRUE
UNKNOWN


-- ( )
: DSQL, PSQL
: 1.0
: 1.5
: , -- ( ), . SQL.
Firebird 1.5 , -- , SQL. -- .
:
-- :
create table Customers (
name varchar(32),
added_by varchar(24),
custno varchar(8),
purchases integer
--
)

, Firebird 1.5 .

CASE
: DSQL, PSQL
: 1.5
: CASE .
:
CASE, case Pascal switch C.
6


CASE , if ... else if ...
else if.

CASE
:
CASE <>
WHEN <1> THEN 1
WHEN <2> THEN 2
...
[ELSE < >]
END

<> <1>, <2> ..,


, .
ELSE, <
>. ELSE , NULL.
=, <> - NULL,
<1>.. <N>,
NULL.
: , ,
, NULL ..
:
select name,
age,
case upper(sex)
when 'M' then ''
when 'F' then ''
else ''
end,
religion
from people

CASE
:
CASE
WHEN <_1> THEN 1
WHEN <_2> THEN 2
...
[ELSE ]
END

<_1>.. <_N> - , : TRUE, FALSE NULL. ,


TRUE, . TRUE ELSE,
7


. TRUE ELSE , NULL.
CASE, , , , NULL.
:
CanVote = case
when Age >= 18 then ''
when Age < 18 then ''
else ''
end;


BIGINT
: 1.5
: BIGINT - SQL99- 64- , .
-2 63 263-1 -9,223,372,036,854,775,808
9,223,372,036,854,775,807.
BIGINT

:
create table WholeLottaRecords (
id bigint not null primary key,
description varchar(32)
)

BLOB
: 2.0
: BLOB :
COLLATE DML-.
BLOB .
BLOB BLOB .
:
select NameBlob from MyTable
where NameBlob collate KOI8R_RU = ''

()
: 1.0, 1.5, 2.0
9


, Firebird.
4.1. , Firebird

Firebird

DOS737

1.5

DOS775

1.5

DOS858

= DOS850

1.5

DOS862

1.5

DOS864

1.5

DOS866

1.5

DOS869

1.5

ISO8859_2

-2, -- 1.0

ISO8859_3

-3,

-- 1.5

ISO8859_4

-4,

-- 1.5

ISO8859_5

1.5

ISO8859_6

1.5

ISO8859_7

1.5

ISO8859_8

1.5

ISO8859_9

-5,

1.5

ISO8859_13

-7,

1.5

KOI8R

2.0

KOI8U

2.0

UTF8 (*)

2.0

WIN1255

1.5

WIN1256

1.5

WIN1257

1.5

WIN1258

2.0

(*)

Firebird 1.5, UTF8 - UNICODE_FSS. . Firebird


2, UTF8 - "" , UNICODE_FSS.

10


NONE
: 1.5.1
: Firebird 1.5.1 NONE
. .


(collation)
: 1.0, 1.5, 1.5.1, 2.0
, Firebird. , (case insensitive - ci), (accent insensitive - ai)
(dictionary-sorted - dic).

11

4.2. (collation) Firebird



ISO8859_1

ES_ES_CI_AI

ci, ai

2.0

PT_BR

(- ci, ai
)

2.0

CS_CZ

1.0

ISO_HUN

1.5

ISO_PLK

2.0

ISO8859_13

LT_LT

1.5.1

UTF8

UCS_BASIC

2.0

UNICODE

BS_BA

PXW_HUN

ci

1.0

WIN_CZ

ci

2.0

WIN_CZ_CI_AI

ci, ai

2.0

WIN1251

WIN1251_UA

1.5

WIN1252

WIN_PTBR

(- ci, ai
)

2.0

WIN1257

WIN1257_EE

dic

2.0

WIN1257_LT

dic

2.0

WIN1257_LV

dic

2.0

KOI8R

KOI8R_RU

dic

2.0

KOI8U

KOI8U_UA

dic

2.0

ISO8859_2

WIN1250

dic

2.0
2.0

UCS_BASIC UTF8 ( Unicode): A, B, a, b,

UNICODE UCA (Unicode Collation Algorithm):


a, A, , b, B

12

DDL
ALTER DATABASE
: DSQL, ESQL
:
.
:
ALTER {DATABASE | SCHEMA}
[< > [< > ...]]
[ADD DIFFERENCE FILE ' ']
[DROP DIFFERENCE FILE]
[{BEGIN | END} BACKUP]
< >
< >

::=

ADD < > [< > ...]

::= FILE ' '


[STARTING [AT [PAGE]] ]
[LENGTH [=] [PAGE[S]]

DIFFERENCE FILE BACKUP, Firebird 2.0, ESQL.

BEGIN BACKUP
: DSQL
: 2.0
: - ,
- . ,
- , -.
, ;
.
:
alter database begin backup

13

DDL

END BACKUP
: DSQL
: 2.0
: - , - . -
, ,
( gbak .)
:
alter database end backup

BEGIN BACKUP END BACKUP, nbackup:


/ . nbackup Firebird.

ADD DIFFERENCE FILE


: DSQL
: 2.0
: -,
ALTER DATABASE BEGIN BACKUP.
:
alter database add difference file 'C:\Firebird\Databases\Example.delta'

:
, -, .
( ), .
Windows .
-, DROP
DIFFERENCE FILE, ADD DIFFERENCE FILE.
- , -
, , delta.
14

DDL

DROP DIFFERENCE FILE


: DSQL
: 2.0
: -, ALTER DATABASE ADD
DIFFERENCE FILE. -,
.
:
alter database drop difference file

ALTER DOMAIN
: DSQL, ESQL

SET
DEFAULT
: IB
: , ,
SET DEFAULT. InterBase 6, Language
Reference USER.
:
alter domain DDate
set default current_date

ALTER EXTERNAL FUNCTION


: DSQL
: 2.0
: / (UDF). .
:
ALTER EXTERNAL FUNCTION
<> [<>]

15

DDL
<>

::=

ENTRY_POINT ' '


| MODULE_NAME ' '

:
alter external function Phi module_name 'NewUdfLib'

ALTER PROCEDURE
: DSQL, ESQL


: 2.0
: ,
.
:
ALTER PROCEDURE (< > [, < > ...])
< >

::=

[{= | DEFAULT} ]

: ,
.
:
alter procedure TestProc
(a int, b int default 1007, s varchar(12) = '-')
...

: 2.0, 2.0.1
: Firebird 2.0 //
, . 2.0.1.
.

ALTER SEQUENCE
: DSQL
16

DDL
: 2.0
: (sequence). SEQUENCE - ,
Firebird. ALTER SEQUENCE ... RESTART WITH SET GENERATOR ... TO Firebird
2.0 .
:
ALTER SEQUENCE RESTART WITH < >
< >

::=

64- .

:
alter sequence seqtest restart with 0

ALTER SEQUENCE
!
CREATE SEQUENCE.

. : CREATE SEQUENCE

ALTER TABLE
: DSQL, ESQL

ADD
COLUMN
: IB
: , ,
. InterBase 6,
Language Reference USER.
:
alter table MyData
add MyDay date default current_date

ALTER COLUMN: DROP DEFAULT


: DSQL
17

DDL
: 2.0
: Firebird 2 .
, , ALTER
COLUMN ... DROP DEFAULT ,
.
:
ALTER TABLE tablename ALTER [COLUMN] DROP DEFAULT

:
alter table Docs alter Status drop default

DROP DEFAULT ,
, .

ALTER COLUMN: SET DEFAULT


: DSQL
: 2.0
: Firebird 2 / . , .
( ).
:
ALTER TABLE tablename ALTER [COLUMN] SET DEFAULT < >
< >

::=

| | NULL

:
alter table Customers alter EnteredBy set default current_user

"" ,
NULL.

ALTER COLUMN: POSITION ,

1
: 1.0

: , 1. SQL
InterBase, InterBase 0.
18

DDL
:
ALTER TABLE _ ALTER [COLUMN] _ POSITION <>
<>

::=

:
alter table Stock alter Quantity position 3

, CREATE/ALTER TRIGGER. 0.

CHECK
, NULL
: 2.0
: CHECK NULL, Firebird 2.0
(, ). SQL, Firebird 2.0 , NULL, , false. CREATE TABLE.

(FOREIGN KEY)

: 2.0
: Firebird 2.0 (foreign key)
.

(UNIQUE)
NULL
: 1.5
: SQL-99, NULL ( ) , (UNIQUE). .
CREATE TABLE :: (UNIQUE) NULL

USING INDEX
: DSQL
: 1.5
19

DDL
: USING INDEX (primary),
(unique) (foreign) .
, ,
( - ).
:
[ADD] [CONSTRAINT ]
< > <>
[USING [ASC[ENDING] | DESC[ENDING]] INDEX index_name]

CREATE TABLE :: USING INDEX.

ALTER TRIGGER
: DSQL, ESQL
: . (view),
, .
:
ALTER TRIGGER _
<> [, <> ...]
<>

::=

{ACTIVE | INACTIVE}
| {BEFORE | AFTER} < >
| POSITION
| AS < >

< >
<>
< >
< >
<>

::= <> [OR <> [OR <>]]


::= INSERT | UPDATE | DELETE
::= [< >] <>
::= DECLARE VARIABLE <> <>; ...
::= <> | ; [<> | ;]


: 1.5
: ALTER TRIGGER, , - .
CREATE TRIGGER :: .

: 2.0, 2.0.1
20

DDL
: Firebird 2.0 //
, . 2.0.1.
.

: 1.5
: Firebird 1.5 PLAN .
.

ALTER TRIGGER


: 1.0

: , CREATE/ALTER/DROP TRIGGER, InterBase . 255,


( - ) - (backup-restore),
.
, , , , ALTER TRIGGER (,
), ,
.
, Firebird CREATE/ALTER/DROP TRIGGER.
, 255, ,
.

COMMENT
: DSQL
: 2.0
: . RDB$DESCRIPTION ,
.
:
COMMENT ON <> IS {' ' | NULL}

21

DDL

::=

<>

< >

DATABASE
| < >
| COLUMN .
| PARAMETER .
::= CHARACTER SET | COLLATION | DOMAIN | EXCEPTION
| EXTERNAL FUNCTION | FILTER | GENERATOR | INDEX
| PROCEDURE | ROLE | SEQUENCE | TABLE | TRIGGER | VIEW

(''), NULL.

:
comment on database is ' .'
comment on table Metals is ' '
comment on column Metals.IsAlloy is '0 = , 1 = '
comment on index ix_sales is
' !'

CREATE DATABASE
: DSQL, ESQL

16 Kb
: 1.0
: 16384 .
:
CREATE {DATABASE | SCHEMA}
...
[PAGE_SIZE [=] < >]
...
< >

::=

1024 | 2048 | 4096 | 8192 | 16384

CREATE DOMAIN
: DSQL, ESQL
22

DDL



: IB
: , , . InterBase 6,
Language Reference USER.
:
create domain DDate as
date
default current_date
not null

CREATE GENERATOR
: DSQL, ESQL

CREATE SEQUENCE
: 2.0
: Firebird 2.0 , CREATE
SEQUENCE, SQL.



: 1.0
: InterBase ,
123 ( 1 ) 1019 ( 8 ). Firebird
- 32,000 .

CREATE INDEX
: DSQL, ESQL
23

DDL
: / .
:
CREATE [UNIQUE] [ASC[ENDING] | [DESC[ENDING]] INDEX
ON
{ ( [, ...]) | COMPUTED BY () }

(UNIQUE)
NULL
: 1.5
: SQL-99, , ,
NULL . CREATE TABLE :: NULL.


: 2.0
: ( ), - COMPUTED BY.
, WHERE, ORDER BY GROUP BY ,
.
:
create index ix_upname on persons computed by (upper(name));
commit;
-- ix_upname:
select * from persons order by upper(name);
select * from persons where upper(name) starting with '';
delete from persons where upper(name) = 'BROWN';
delete from persons where upper(name) = 'BROWN' and age > 65;
create descending index ix_events_yt
on MyEvents
computed by (extract(year from StartDate) || Town);
commit;
-- ix_events_yt:
select * from MyEvents
order by extract(year from StartDate) || Town desc;


: 2.0
24

DDL
: , 252 , 1/4
, .. 256 4096 . ,
, 9 .
(
).
5.1. (CHAR/VARCHAR)

1024

247

123

82

61

2048

503

251

167

125

4096

1015

507

338

253

8192

2039

1019

679

509

16384

4087

2043

1362

1021



: 1.0.3, 1.5, 2.0
: 65
Firebird 1.0.3, Firebird 1.5 257, Firebird 2.0.
, , .
5.2. , Firebird 2.0

1024

50

35

27

2048

101

72

56

4096

203

145

113

8192

408

291

227

16384

818

584

454

25

DDL
, , 50 -
(, , ). , , .
, Firebird 1.01.5, .

CREATE PROCEDURE
: DSQL, ESQL
: 2.0
: ,
.
:
CREATE PROCEDURE _ (<> [, <> ...])
...
<>

::=

[{= | DEFAULT} ]

: ,
.
:
create procedure TestProc
(a int, b int default 8, s varchar(12) = '')
...

CREATE SEQUENCE
: DSQL
: 2.0
: - SQL , InterBase
Firebird . CREATE SEQUENCE CREATE
GENERATOR Firebird 2.0 .
:
CREATE SEQUENCE

26

DDL
:
create sequence seqtest

,
. , .
( ) 64- ,
. :
1, , 32- .
32- , , 32- .
3 , 1 (
, , ).
. : ALTER SEQUENCE, NEXT VALUE FOR, DROP SEQUENCE

CREATE TABLE
: DSQL, ESQL

NULL CHECK
: 2.0
: CHECK NULL, Firebird 2.0
(, ). SQL, Firebird 2.0 , NULL, , false.
: Firebird 2.0,
(value) NULL. 2.0 ( ) :
check (value > 10000)
check ((Sex = '') or (Sex = ''))
check (upper(value) in ( 'A', 'B', 'X' ))
check (Minimum <= Maximum)

Firebird 2.0 . CREATE/ALTER TABLE


and _ is not null CHECK,
NULL.

27

DDL



: IB
: , , .
InterBase 6, Language Reference USER.
:
create table MyData (
id int not null primary key,
record_created timestamp default current_timestamp
)

(FOREIGN KEY)

: 2.0
: Firebird 2.0 (foreign key)
.

NULL
: 1.5
: SQL-99, NULL- ( ) , (UNIQUE).
, , :
, NULL , .
, NULL , .
, NULL , .
, NULL
, .
: , NULL .
-NULL ,
28

DDL
, NULL, , ,
NULL, , .
5.3. NULL
,
A

NULL

NULL

NULL

NULL

NULL

NULL

NULL

NULL

NULL

NULL

NULL

NULL

NULL

NULL

NULL

NULL

NULL

NULL

NULL

NULL

NULL

NULL

NULL

NULL

333

444

NULL

NULL

333

444

, :
create table Test(
ID integer,
A integer,
B integer,
C integer,
D integer
);
alter table Test add constraint UNQ_TEST unique (A,B,C,D);
commit;
-- NULL ,
insert into Test (ID, A, B, C, D)
values (1, NULL, NULL, NULL, NULL);
insert into Test (ID, A, B, C, D)
values (2, NULL, NULL, NULL, NULL); --
-- NULL - ""
insert into Test (ID, A, B, C, D)
values (3, NULL, NULL, 3, 4);
insert into Test (ID, A, B, C, D)
values (4, 1, 2, NULL, NULL); --
-- NULL - ""

29

DDL
insert into Test (ID, A, B, C, D)
values (5, NULL, NULL, 3, NULL);
insert into Test (ID, A, B, C, D)
values (6, NULL, NULL, NULL, 4); --
-- NULL , D
insert into Test (ID, A, B, C, D)
values (7, NULL, NULL, 5, 5)
insert into Test (ID, A, B, C, D)
values (8, NULL, NULL, 5, 7); --
-- NULL (C D)
insert into Test (ID, A, B, C, D)
values (9, NULL, NULL, 333, 444);
insert into Test (ID, A, B, C, D)
values (10, NULL, NULL, 333, 444); -- !
commit;

, Test 9 (
ID) 1 9.

USING INDEX
: DSQL
: 1.5
: (primary), (unique) (foreign)
USING INDEX, :
,
- ( - ).
USING INDEX , // , ( Firebird 1.5) - .. UNQ2_TEST, FK_TEST2_1 ..

1.
- .. USING INDEX .
2. () ,
(, ,
),
.

USING INDEX , ALTER TABLE ... ADD CONSTRAINT. ,


(), CHECK, .
30

DDL
:
[CONSTRAINT _]
< > < >
[USING [ASC[ENDING] | DESC[ENDING]] INDEX _]

:
PK_CUST ()
IX_CUSTNO:
create table customers (
custno int not null constraint pk_cust primary key using index ix_custno,
...

( - INTEG_7) IX_CUSTNO:
create table customers (
custno int not null primary key using index ix_custno,
...

:
create table people (
id int not null,
nickname varchar(12) not null,
country char(4),
..
..
constraint pk_people primary key (id),
constraint uk_nickname unique (nickname) using index ix_nick
)
alter table people
add constraint fk_people_country
foreign key (country) references countries(code)
using desc index ix_people_country

, ,
, , .

CREATE TRIGGER
: DSQL, ESQL
: , .. PSQL-,
(// ) (view).
:

31

DDL
CREATE TRIGGER _ FOR {_ | _}
[ACTIVE | INACTIVE]
{BEFORE | AFTER} < >
[POSITION ]
AS
< >
< >
<>
< >
< >
<>

::= <> [OR <> [OR <>]]


::= INSERT | UPDATE | DELETE
::= [< >] <>
::= DECLARE VARIABLE <> <>; ...
::= <> | ; [<> | ;]


: 1.5
: 1.5 (INSERT /
UPDATE / DELETE). (), ,
, : INSERTING, UPDATING DELETING.
:
create trigger biu_parts for parts
before insert or update
as
begin
/* (if inserting): */
if (inserting and new.id is null)
then new.id = gen_id(gen_partrec_id, 1);
/* : */
new.partname_upper = upper(new.partname);
end

OLD NEW.
(, OLD NEW
) NULL. .

CREATE TRIGGER


: 1.0

: InterBase, Firebird CREATE/ALTER/DROP TRIGGER . ALTER TRIGGER


.
32

DDL

: 1.5
: Firebird 1.5 PLAN .
.

CREATE VIEW
: DSQL, ESQL

SELECT
: 2.0
: Firebird 2.0 (view)
SELECT, FIRST, SKIP, ROWS, ORDER BY, PLAN UNION.

UNION (view)
:
create view premiums (id, name, bonus) --
as
-- ,
select e.id, e.name, e.salary
from employee e
where extract(month from e.birthday) = extract(month from current_date)
union all
--
select id, name, amount from (
select first 1 e.id, e.name, sum(s.amount)/10 as amount
from employee e
join sales s on (e.id = s.employee_id)
where ('01.' || extract(month from current_date) || '.'
|| extract(year from current_date) <= s.s_date)
and (s.s_date < '01.' || (extract(month from current_date)+1) || '.'
|| extract(year from current_date))
group by e.id, e.name
order by 3 desc
)

Firebird 1.5

: 1.5, 2.0
33

DDL
: Firebird 1.5 PLAN.
2.0 .

(updatable views)

: 2.0
: 2.0 ( Interbase 6.0), Firebird ()
, , .. :
. Firebird 2.0, , - ( , ). InterBase 6 (
Data Definition Guide, Updating views with triggers).

, , Firebird 2.0 - .

CREATE OR ALTER EXCEPTION


: DSQL
: 2.0
: (exception) , , CREATE EXCEPTION. , . .
: , CREATE EXCEPTION.

CREATE OR ALTER PROCEDURE


: DSQL
: 1.5
: , , CREATE
PROCEDURE. , . .
34

DDL
: , CREATE PROCEDURE.

CREATE OR ALTER TRIGGER


: DSQL
: 1.5
: , , CREATE
TRIGGER. , .
.
: , CREATE TRIGGER.

DECLARE EXTERNAL FUNCTION


: DSQL, ESQL
: (UDF).
:
DECLARE EXTERNAL FUNCTION _
[< > [, < > ...]]
RETURNS {< > | PARAMETER } [FREE_IT]
ENTRY_POINT ' ' MODULE_NAME ' '
< >
< >

::=
::=

SQL [BY DESCRIPTOR] | CSTRING()


SQL [BY {DESCRIPTOR | VALUE}] | CSTRING()

.
.
( ) - , . CSTRING
CSTRING).


( ).
.

BY DESCRIPTOR
: 1.0
35

DDL
: Firebird ( )
- BY DESCRIPTOR. NULL NULL . , . BY DESCRIPTOR
- ! ,
.

RETURNS
PARAMETER n
: IB 6
: BLOB-, , , RETURNS PARAMETER
( 1). InterBase 6,
Language Reference ( Developer's Guide). BLOB-,
.

DECLARE FILTER
: DSQL, ESQL
: 2.0
: BLOB- .
:
DECLARE FILTER _
INPUT_TYPE < BLOB> OUTPUT_TYPE < BLOB>
ENTRY_POINT '_' MODULE_NAME '_'
< BLOB> ::= | <>
<> ::= binary | text | blr | acl | ranges | summary | format
| transaction_description | external_file_description

Firebird 2 BLOB , ( ). -.
:
declare filter Funnel
input_type blr output_type text

36

DDL
entry_point 'blr2asc' module_name 'myfilterlib'

, RDB
$TYPES ( ). BLOB-.
insert into rdb$types (rdb$field_name, rdb$type, rdb$type_name)
values ('RDB$FIELD_SUB_TYPE', -1, 'MIDI')

rdb$field_name RDB$TYPES BLOB-


'RDB$FIELD_SUB_TYPE'.
, BLOB- .

DROP GENERATOR
: DSQL
: 1.0
Firebird 2.0 DROP SEQUENCE
: .
( ) .
:
DROP GENERATOR

DROP PROCEDURE
: DSQL, ESQL

: 2.0, 2.0.1
: Firebird 2.0 //
, . 2.0.1.
.
37

DDL

DROP SEQUENCE
: DSQL
: 2.0
: .
( ) . DROP SEQUENCE DROP GENERATOR , Firebird 2.0 .
:
DROP SEQUENCE

:
drop sequence seqtest

. : CREATE SEQUENCE

DROP TRIGGER
: DSQL, ESQL

: 2.0, 2.0.1
: Firebird 2.0 //
, . 2.0.1.
.

DROP TRIGGER


: 1.0

: InterBase, Firebird CREATE/ALTER/DROP TRIGGER . ALTER TRIGGER


.
38

DDL

RECREATE EXCEPTION
: DSQL
: 2.0
: . , RECREATE EXCEPTION . ,
.
: , CREATE EXCEPTION.

RECREATE EXCEPTION , , .
.

RECREATE PROCEDURE
: DSQL
: 1.0
: . , RECREATE PROCEDURE .
: , CREATE PROCEDURE.

RECREATE PROCEDURE ,
, . .

: 2.0, 2.0.1
: Firebird 2.0 // , .
2.0.1. .
39

DDL

RECREATE TABLE
: DSQL
: 1.0
: . ,
RECREATE TABLE ( !)
. , .
: , CREATE TABLE.

RECREATE TABLE , , .
.

RECREATE TRIGGER
: DSQL
: 2.0
: . ,
RECREATE TRIGGER .
: , CREATE TRIGGER.

: 2.0, 2.0.1
: Firebird 2.0 //
, . 2.0.1.
.

RECREATE VIEW
: DSQL
: 1.5
40

DDL
: (view). , RECREATE VIEW .
: , CREATE VIEW.

REVOKE ADMIN OPTION


: DSQL
: 2.0
:
/, . / .
:
REVOKE ADMIN OPTION FOR < > FROM < >
< >
< >
<>

::=
::=
::=

[, ...]
[USER] <> [, [USER] <> ...]
| PUBLIC

:
revoke admin option for manager from john, paul, george, ringo

X
, X,
.

SET GENERATOR
: DSQL, ESQL
: .
Firebird 2.0 , ALTER SEQUENCE.
:
SET GENERATOR TO < >
< >

::=

64-

( GEN_ID NEXT VALUE FOR


).
SET GENERATOR ,
.

41

DML
DELETE
: DSQL, ESQL, PSQL
: () ( ) WHERE ROWS.
:
DELETE
[TRANSACTION ]
FROM { | } []
[WHERE {__ | CURRENT OF _}]
[PLAN _]
[ORDER BY _]
[ROWS <m> [TO <n>]]
<m>, <n>

::=

TRANSACTION ESQL.
WHERE CURRENT OF ESQL PSQL.
PLAN, ORDER BY, ROWS ESQL.

ORDER BY
: DSQL, PSQL
: 2.0
: DELETE ORDER BY.
ROWS, .

PLAN
: DSQL, PSQL
: 2.0
42

DML
: DELETE PLAN,
.

ROWS
: DSQL, PSQL
: 2.0
: .
:
ROWS <m> [TO <n>]
<m>, <n>

::=

m, m , WHERE ORDER BY.


:
m , .
m = 0, .
m < 0, .
m n, m- n- .
1.
:

m < 1, .
n < m-1, .
n = m-1, .
m ,
.
m 1.. , n ,
m- .
ROWS SELECT UPDATE.

EXECUTE BLOCK
: DSQL
: 2.0
: PSQL- ( ),
. PSQL- - DSQL, (
).
43

DML
:
EXECUTE BLOCK [(< >)]
[RETURNS (< >)]
AS
[< >]
BEGIN
PSQL
END
< >
< >
< >
< >

::=
::=
::=
::=

= ? [, < >]
[, < >]
< > [< >]
DECLARE [VARIABLE] [= ];

:
0 127 ASCII
ASCIITABLE:
execute block
as
declare i int = 0;
begin
while (i < 128) do
begin
insert into AsciiTable values (:i, ascii_char(:i));
i = i + 1;
end
end

, , :
execute block (x double precision = ?, y double precision = ?)
returns (gmean double precision)
as
begin
amean = (x+y)/2;
suspend;
end

,
(prepared),
. , ( ), .
, integer smallest largest smallest..largest
, .
execute block (smallest int = ?, largest int = ?)
returns (number int, square bigint, cube bigint)
as
begin
number = smallest;
while (number <= largest) do

44

DML
begin
square = number * number;
cube
= number * square;
suspend;
number = number + 1;
end
end

:
, , ( ), EXECUTE BLOCK
SET TERM, , :
set term #;
execute block (...)
as
begin
1;
2;
end
#
set term ;#

isql ; ( ) EXECUTE BLOCK. isql,


-, ,
.
EXECUTE BLOCK , DSQL-. :
(prepare) , .
, , isql Firebird EXECUTE BLOCK .
Firebird
(? ), ( :ParamName, @ ..) .
:xxx ( ),
.
EXECUTE BLOCK ,
SUSPEND, .
, SELECT.
RETURNING_VALUES INTO EXECUTE BLOCK , .

EXECUTE PROCEDURE
: DSQL, ESQL, PSQL
45

DML
: 1.5
: . Firebird 1.0.x InterBase, , ( ESQL) ( PSQL). Firebird 1.5 ( ESQL),
.
:
EXECUTE PROCEDURE
[TRANSACTION ]
[[(] < > [, < > ...] [)]]
[RETURNING_VALUES < > [, < > ...]]
< >
< >
<>
<>
< NULL>

::=
::=
::=
::=
::=

<> [< NULL>]


<> [< NULL>]
,

[INDICATOR]:

TRANSACTION ESQL.
- ESQL Firebird 1.5.
NULL ESQL .
ESQL , (:). PSQL , OLD NEW.

:
PSQL ( ):
execute procedure MakeFullName
:FirstName, :Middlename, :LastName
returning_values :FullName;

ESQL ( ):
exec sql
execute procedure MakeFullName
:FirstName, :Middlename, :LastName
returning_values :FullName;

isql ( ):
execute procedure MakeFullName
'', '', '';

: isql RETURNING_VALUES -
.
- Firebird 1.5 :
46

DML
execute procedure MakeFullName
'Mr./Mrs. ' || FirstName, Middlename, upper(LastName)
returning_values FullName;

INSERT
: DSQL, ESQL, PSQL
: 2.0
: (view).
VALUES (
) SELECT.
:
INSERT [TRANSACTION ]
INTO { | } [(<>)]
{VALUES (<>) [RETURNING <> [INTO <>]]
| select}
<>
<>
<>

::=
::=
::=

[, ...]

[,
...]
: [, : ...]

TRANSACTION ESQL.
RETURNING ESQL.
INTO PSQL.
OLD NEW .
Firebird 2.0
.

RETURNING
: DSQL, PSQL
: 2.0
: INSERT ( VALUES)
RETURNING , , . RETURNING ,
, , , / . , BEFORE-, AFTER-.
:
insert into Scholars (firstname, lastname, address, phone, email)

47

DML
values ('', '', ' 27A', null, null)
returning id, lastname, fullname

INSERT..SELECT
UNION
: 2.0
: SELECT INSERT
UNION.
:
insert into Members (number, name)
select number, name from NewMembers where Accepted = 1
union
select number, name from SuspendedMembers where Vindicated = 1

SELECT
: DSQL, ESQL, PSQL

: 1.5
: , Firebird 1.5. , SELECT::GROUP BY.


Firebird 1.5
.
:
select
r.rdb$relation_name as " ",
(select count(rf.rdb$field_name) || ' - ' || count(distinct s.rdb$field_name)
from rdb$indices i
join rdb$index_segments s on (s.rdb$index_name = i.rdb$index_name)

48

DML
where (i.rdb$relation_name = r.rdb$relation_name)
) as " / ."
from
rdb$relations r
join rdb$relation_fields rf on (rf.rdb$relation_name = r.rdb$relation_name)
where r.rdb$system_flag = 0
group by r.rdb$relation_name
order by 1

( ) ( ) ,
. , distinct. , count(rf.rdb$field_name) , .. . ,
( count(rf.rdb$field_name) ),
, .

Firebird 1.0 , !

HAVING ORDER
BY
Firebird 1.5
HAVING ORDER BY. , HAVING ORDER
BY :
(, HAVING MAX(SALARY) >10000).
, GROUP BY (
/).
, GROUP BY. , Name, HAVING
: UPPER(Name), Name || '!', SUBSTRING(Name
FROM 4 FOR 2).
:
--
select
r.rdb$relation_name as Name,
count(rf.rdb$field_name) as Fields
from
rdb$relations r
join rdb$relation_fields rf on (r.rdb$relation_name = rf.rdb$relation_name)
where r.rdb$system_flag = 0
group by r.rdb$relation_name
order by Fields, Name
--
select

49

DML
r.rdb$relation_name as Name,
count(rf.rdb$field_name) as Fields
from
rdb$relations r
join rdb$relation_fields rf on (r.rdb$relation_name = rf.rdb$relation_name)
where r.rdb$system_flag = 0
group by r.rdb$relation_name
order by max(rf.rdb$field_id)
--
select
r.rdb$relation_name as Name,
count(rf.rdb$field_name) as Fields
from
rdb$relations r
join rdb$relation_fields rf on (r.rdb$relation_name = rf.rdb$relation_name)
where r.rdb$system_flag = 0
group by r.rdb$relation_name
order by r.rdb$relation_id

r.rdb$relation_id GROUP BY
, HAVING (, ) ORDER BY.

COLLATE BLOB-
: 2.0
: BLOB- (, , .) COLLATE .
:
select NameBlob from MyTable
where NameBlob collate pt_br = 'Joo'

(SELECT FROM SELECT)


: 2.0
: (derived table) - , SELECT. SELECT , . , - SELECT- FROM.
:
(select-)
[[AS] __]
[(< >)]
< >

50

:=

[, ...]

DML
:
( )
. SELECT
, ..
.
select fieldcount,
count(relation) as num_tables
from
(select r.rdb$relation_name as relation,
count(*) as fieldcount
from
rdb$relations r
join rdb$relation_fields rf
on rf.rdb$relation_name = r.rdb$relation_name
group by relation)
group by fieldcount

,
( ) :
select dbinfo.descr,
dbinfo.def_charset,
dbinfo.rel_id
from (select * from rdb$database) dbinfo
(descr, rel_id, sec_class, def_charset)

:
, , .
"" (, ),
(alias) .
, , , .. .
:
select '4' || z
from
(
select y || '3' as z
from
(
select x || '2' as y
from
(
select '1' as x from rdb$database
)
)
)

(union) (union). , (join), ,


(join). , c 51

DML
. WHERE, ORDER BY, GROUP BY, FIRST, SKIP,
ROWS ..
. , , , (join),
.

FIRST SKIP
: 1.0
: 1.5
: FIRST , . SKIP .

Firebird 2.0 ROWS


SQL.

:
SELECT [FIRST (<m>)] [SKIP (<n>)] <> FROM ...
<m>, <n>
<>

::=
::=

.
.

< > , ()
. .

FIRST SKIP . , .. FIRST m SKIP n,


n , m
.
SKIP 0 , , , . FIRST 0 1.5
, 1.0 . , FIRST,
SKIP .

SKIP , .
( SKIP) FIRST,
.
:
10 People:
select first 10 id, name from People
order by name asc

52

DML
, 10 :
select skip 10 id, name from People
order by name asc

10 . :
select skip ((select count(*) - 10 from People))
id, name from People
order by name asc

81- 100- :
select first 20 skip 80 id, name from People
order by name asc

, FIRST
:
:
delete from MyTable where ID in (select first 10 ID from MyTable)

, - ,
. Firebird 2.0
ROWS

... where F1 in (select first 5 F2 from Table2 order by 1 desc)

, , IN EXISTS (, FIRST ) :
...where exists
( select first 5 F2 from Table2
where Table2.F2 = Table1.F1
order by 1 desc )

GROUP BY
: GROUP BY ,
(NULL- ). , , , .
:
SELECT ...
FROM ...
GROUP BY <> [, <> ...]
<>

::=

[COLLATE ]
|
|

53

DML
|

, . , "" ,
GROUP BY ( , ).
" " - , 1. .
1 , . .
GROUP BY
(
).
: , , ( ) () .

,
: 1.0, 1.5, 2.0
: , Firebird 2
.
:
:
select strlen(lastname) as len_name, count(*)
from people
group by len_name
select strlen(lastname) as len_name, count(*)
from people
group by 1
select strlen(lastname) as len_name, count(*)
from people
group by strlen(lastname)

: UDF Firebird 1. , CASE Firebird 1.5. Firebird


2 .

JOIN

: 1.0
54

DML
: InterBase 6 , , , JOIN:
--
select
r.rdb$relation_name as TableName,
count(rf.rdb$field_name) as Fields
from
rdb$relations r
join rdb$relation_fields rf on (r.rdb$relation_name = rf.rdb$relation_name)
where rdb$field_id = 1
group by r.rdb$relation_name
--
select
r.rdb$relation_name as TableName,
count(rf.rdb$field_name) as Fields
from
rdb$relations r
join rdb$relation_fields rf on (r.rdb$relation_name = rf.rdb$relation_name)
where r.rdb$field_id = 1
group by r.rdb$relation_name

rdb$field_id rdb$relations rdb


$relation_fields, . Firebird (Dialect 3),
(Dialect 1) , .

CROSS JOIN
: 2.0
: Firebird 2.0 CROSS JOIN, . join , ( ON (1 = 1)) (
FROM ), .
:
SELECT ...
FROM table1 CROSS JOIN table2
[WHERE ...]
...

: CROSS JOIN ON .
:
select * from Men cross join Women
order by Men.age, Women.age
-- :
-select * from Men join Women on 1 = 1
-order by Men.age, Women.age

55

DML
-- :
-select * from Men, Women
-order by Men.age, Women.age

ORDER BY
:
SELECT ... FROM ...
...
ORDER BY <> [, <> ...]
<>

::=

{ | | | }
[COLLATE ]
[ASC[ENDING] | DESC[ENDING]]
[NULLS {FIRST|LAST}]


: 2.0
: Firebird 2.0 .
:
select rdb$character_set_id as charset_id,
rdb$collation_id as coll_id,
rdb$collation_name as name
from rdb$collations
order by charset_id, coll_id

"" select *
: 2.0
: SELECT * ,
, .
:
Firebird 2.0:
select * from rdb$collations
order by 3, 2

Films.Director Firebird
2.0. Firebird 2 Books:
select Books.*, Films.Director
from Books, Films
order by 2

56

DML


: 1.5
: Firebird 1.5 . ,
,
1 , . ( .. )
, .
:
select x, y, note from Pairs
order by x+y desc

UDF, ()
UDF .

NULL-
: 1.5, 2.0
: Firebird 1.5 NULLS FIRST NULLS LAST,
NULL- . Firebird 2.0 NULL .
NULL- NULLS FIRST NULLS LAST,
NULL- :
Firebird 1.0 1.5 - , ( /).
Firebird 2.0 - .
. .
6.1. NULL-
NULL-

Firebird 1

Firebird 1.5

Firebird 2

order by Field [asc]

order by Field desc

order by Field [asc | desc] nulls first

order by Field [asc | desc] nulls last

57

DML

( Firebird 2.0 ) NULL- ,


Firebird, .
NULL- ,
.

:
--
select * from rdb$indices r
order by r.rdb$foreign_key nulls first
--
--
select * from rdb$indices r
order by r.rdb$foreign_key desc nulls last
--
-- ,
-- NULL
select * from rdb$indices r
order by r.rdb$foreign_key desc nulls last, r.rdb$unique_flag nulls first

PLAN
: DSQL, ESQL, PSQL
: , ,
.
:
PLAN <>
<>

::=

[JOIN | [SORT] [MERGE]] (<> | <>


[, <> | <> ...])

<>

::=

{ | }
{NATURAL
| INDEX ( [, ...])
| ORDER [INDEX ( [, ...])]}


: 2.0
: Firebird 2 (PLAN):
(outer join) ,
, .
58

DML

(outer join).
SELECT .

ORDER INDEX
: 2.0
: ORDER INDEX ( ).
:
plan (MyTable order ix_myfield index (ix_this, ix_that))

PLAN
: 2.0
: Firebird 2 , PLAN .
.

ROWS
: DSQL, PSQL
: 2.0
: , SELECT,
.
:
SELECT:
SELECT ... FROM ...
[WHERE ...]
[ORDER BY ...]
ROWS <m> [TO <n>]
<m>, <n>

::=

UNION:
SELECT [FIRST p] [SKIP q] <...> FROM ...
[WHERE ...]
[ORDER BY ...]
UNION [ALL | DISTINCT]
SELECT [FIRST r] [SKIP s] <...> FROM ...

59

DML
[WHERE ...]
[ORDER BY ...]
ROWS <m> [TO <n>]

ROWS <m> (.. TO ),


<m> .
:
m, .
m = 0, .
m < 0, .
ROWS m n, m- n- . 1.
:

m < 1, .
n < m-1, .
n = m-1, .
m , .
m , n - ,
, m-.

ROWS, SQL-, FIRST ...


SKIP, : SKIP , FIRST ,
, (.. n ).
, ( TO) ,
.
ROWS FIRST ... SKIP SELECT
( , UNION),
.
UNION, ROWS ,
SELECT, SELECT.
UNION , - SELECT , : FIRST ... SKIP
ROWS.
ROWS UPDATE DELETE.

: 2.0
: Firebird 2.0 , ( ,
, ..) , . , ,
. DML- ( .. DELETE,
INSERT, UPDATE).
60

DML
:
:
select pears from Fruit
select Fruit.pears from Fruit
select pears from Fruit F
select F.pears from Fruit F

:
select Fruit.pears from Fruit F

( ) Firebird ( Interbase),
, Firebird 2.0.

UNION
: DSQL, ESQL, PSQL

UNION
: 2.0
: UNION . , SELECT,
WHERE ( ANY | SOME, ALL, IN), SELECT-,
INSERT.
:
select name, phone, hourly_rate from clowns
where hourly_rate < all
(select hourly_rate from jugglers
union
select hourly_rate from acrobats)
order by hourly_rate

UNION DISTINCT
: 2.0
: UNION DISTINCT
. UNION
DISTINCT UNION ALL ,
.
61

DML
:
SELECT (...) FROM (...)
UNION [DISTINCT | ALL]
SELECT (...) FROM (...)

:
--
select 1, 2 from rdb$database
union distinct
select 1, 2 from rdb$database
-- ,
-- union distinct -
select 1, 2 from rdb$database
union
select 1, 2 from rdb$database
--
select 1, 2 from rdb$database
union all
select 1, 2 from rdb$database
-- union [distinct], ,
--
select 1, 1 from rdb$database
union
select 1, 2 from rdb$database

WITH LOCK
: DSQL, PSQL
: 1.5
: WITH LOCK
. :
a.
b.

( - ),
.
!
Firebird ,
WITH LOCK.
,
.

:
SELECT ... FROM
[WHERE ...]

62

DML
[FOR UPDATE [OF ...]]
WITH LOCK

WITH LOCK , Firebird



- .
FOR UPDATE, ,
. ,
() ,
( - WITH LOCK
DELETE, INSERT, UPDATE).
WITH LOCK SELECT,
:
;
join;
DISTINCT, GROUP BY
;
(view);
, ;
(external table).
SELECT ... WITH LOCK . , .

UPDATE
: DSQL, ESQL, PSQL
: ( , (view)). SET;
, WHERE ROWS.
:
UPDATE [TRANSACTION ] { | } []
SET = [, = ...]
[WHERE {__ | CURRENT OF }]
[PLAN ]
[ORDER BY _]
[ROWS <m> [TO <n>]]
<m>, <n>

::=

TRANSACTION ESQL.
WHERE CURRENT OF ESQL PSQL.
Firebird 2.0 SET
.

63

DML

ORDER BY
: DSQL, PSQL
: 2.0
: UPDATE ORDER BY.
ROWS, .

PLAN
: DSQL, PSQL
: 2.0
: UPDATE PLAN,
.

ROWS
: DSQL, PSQL
: 2.0
: .
:
ROWS <m> [TO <n>]
<m>, <n>

::=

m, m , WHERE ORDER BY.


:
m , .
m = 0, .
m < 0, .
m n, m- n- .
1.
:
m < 1, .
n < m-1, .
64

DML
n = m-1, .
m ,
.
m 1.. , n ,
m- .
ROWS SELECT DELETE.

65

RELEASE SAVEPOINT
: DSQL
: 1.5
: .
:
RELEASE SAVEPOINT [ONLY]

ONLY, , ,
.
SAVEPOINT.

ROLLBACK
: DSQL, ESQL
:
ROLLBACK [WORK]
[TRANSACTION ]
[RETAIN [SNAPSHOT] | TO [SAVEPOINT] _ | RELEASE]

TRANSACTION RELEASE ESQL.


RETAIN TO DSQL.

ROLLBACK RETAIN
: DSQL
66


: 2.0
: , , .
, RDB$SET_CONTEXT(),
.
:
ROLLBACK [WORK] RETAIN [SNAPSHOT]

ROLLBACK RETAIN InterBase 6,

API- isc_rollback_retaining().

ROLLBACK TO SAVEPOINT
: DSQL
: 1.5
: , .
:
ROLLBACK [WORK] TO [SAVEPOINT]

ROLLBACK TO SAVEPOINT :
, , . ,
RDB$SET_CONTEXT(), .
, . , , . , .
, .
, ,
. , ,
.
SAVEPOINT.

SAVEPOINT
: DSQL
: 1.5
67


: SQL-99, , , () .
.
:
SAVEPOINT <>
<>

::=

,
.
, , :
ROLLBACK [WORK] TO [SAVEPOINT]
ROLLBACK TO SAVEPOINT :

, , . ,
RDB$SET_CONTEXT(), .
, . , , . , .
, .
, ,
. , ,
.
, .
:
RELEASE SAVEPOINT [ONLY]

ONLY, . ONLY
, , , .
:
create table test (id integer);
commit;
insert into test values (1);
commit;
insert into test values (2);
savepoint y; --
delete from test;
select * from test;
-- 0
rollback to y;
select * from test;
-- 2
rollback;
select * from test;
-- 1

68

()
, , ( ) .
( ROLLBACK API-), ,
, ,
, - ,
. .
, , (104
10 6 ), - TIP (Transaction Inventory Page).

, NO
AUTO UNDO SET TRANSACTION isc_tpb_no_auto_undo TPB
(Transaction Parameter Block).
.

PSQL
PSQL,
PSQL-. Firebird (exception)
PSQL-, PSQL-. (
) :
BEGIN...END- ;
, (
- SUSPEND), - .
.

SET TRANSACTION
: DSQL
: 2.0
: .
:
SET TRANSACTION

69


[READ WRITE | READ ONLY]
[ [ISOLATION LEVEL] { SNAPSHOT [TABLE STABILITY]
| READ COMMITTED [[NO] RECORD_VERSION] } ]
[WAIT | NO WAIT]
[LOCK TIMEOUT ]
[NO AUTO UNDO]
[IGNORE LIMBO]
[RESERVING <> [, <> ...]]
<>

::=

_ [, _ ...]
[FOR [SHARED | PROTECTED] {READ | WRITE}]

LOCK TIMEOUT NO WAIT .


: READ WRITE + WAIT + SNAPSHOT.

IGNORE LIMBO
: DSQL
: 2.0
: IGNORE LIMBO, , -. - (-) , (commit) . IGNORE LIMBO isc_tpb_ignore_limbo (TPB), InterBase API
gfix.

LOCK TIMEOUT
: DSQL
: 2.0
: , WAIT, ( ), . . API -
isc_tpb_lock_timeout, (TPB).

NO AUTO UNDO
: DSQL, ESQL
: 2.0
: NO AUTO UNDO ,
. (, ), . 70


, - , NO AUTO UNDO .
NO AUTO UNDO SQL- TPB- isc_tpb_no_auto_undo,
API InterBase.
. : ()

71

PSQL
PSQL (Procedural SQL) .

BEGIN ... END


: PSQL
: 1.5
: Firebird 1.5 BEGIN ... END ,
.
:
create trigger bi_atable for atable
active before insert position 0
as
begin
end

BREAK
: PSQL
: 1.0
: 1.5 LEAVE
: BREAK WHILE FOR
.
:
create procedure PROC
returns (
I integer)
as
begin
i = 10;
while (i > 0) do
begin
i = i - 2;
if (i < 5) then break;

72

PSQL
suspend;
end
end

8 6. 4, 2 0 ,
"i" 4, BREAK .

Firebird 1.5, BREAK SQL-99


LEAVE.

CLOSE
: PSQL
: 2.0
: . , , EXECUTE BLOCK, .
:
CLOSE _;

: . DECLARE CURSOR.

DECLARE CURSOR
: PSQL
: 2.0
: SELECT. ,
( ) . ,
.
, EXECUTE BLOCK.
:
DECLARE [VARIABLE] _ CURSOR FOR (_SELECT);

:
execute block
returns (relation char(31), sysflag int)
as
declare cur cursor for
(select rdb$relation_name, rdb$system_flag from rdb$relations);

73

PSQL
begin
open cur;
while (1=1) do
begin
fetch cur into relation, sysflag;
if (row_count = 0) then leave;
suspend;
end
close cur;
end

. : OPEN, FETCH, CLOSE.

DECLARE [VARIABLE]
: PSQL
: 1.5
: Firebird 1.5 PSQL
. , VARIABLE .
:
DECLARE [VARIABLE] _ _ [{= | DEFAULT} ];

:
create procedure example(a int)
returns (b int)
as
declare p int;
declare q int = 8;
declare r int default 9;
declare variable s int;
declare variable t int = 10;
declare variable u int default 11;
begin
--
end

EXCEPTION
: PSQL
: 1.5, 2.0
: EXCEPTION ,
a.
b.

.
.
74

PSQL
:
EXCEPTION [<> []]
<>

::=


( WHEN ... DO) EXCEPTION . EXCEPTION
.
:
when any do
begin
insert into error_log (...) values (sqlcode, ...);
exception;
end

, .

Firebird 1.5
, , .
:
exception ex_data_error ' ';
exception ex_update_error ' ' || new.id;

Firebird 1.5 78 . Firebird 2.0


1021 .

EXECUTE PROCEDURE
: DSQL, PSQL
: 1.5
: Firebird 1.5
. EXECUTE PROCEDURE.
75

PSQL

EXECUTE STATEMENT
: PSQL
: 1.5
: EXECUTE STATEMENT ,
DSQL-. ,
, :


INSERT, UPDATE, DELETE EXECUTE
PROCEDURE.
:
EXECUTE STATEMENT <>
<statement>

::=

, SQL-,

:
create procedure DynamicSampleOne (ProcName varchar(100))
as
declare variable stmt varchar(1024);
declare variable param int;
begin
select min(SomeField) from SomeTable into param;
stmt = 'execute procedure '
|| ProcName
|| '('
|| cast(param as varchar(20))
|| ')';
execute statement stmt;
end

EXECUTE STATEMENT DDL- ( CREATE/DROP DATABASE), DDL PSQL .


SELECT, .
:

76

PSQL
EXECUTE STATEMENT <> INTO <> [, <> ...]
<>
<>

::=
::=

SQL-, .
PSQL-

:
create procedure DynamicSampleTwo (TableName varchar(100))
as
declare variable param int;
begin
execute statement
'select max(CheckField) from ' || TableName into :param;
if (param > 100) then
exception Ex_Overflow 'Overflow in ' || TableName;
end

FOR SELECT ... DO , .


:
FOR EXECUTE STATEMENT <> INTO <> [, <> ...]
DO <compound-statement>
<>
<>

::=
::=

DSQL-, .
PSQL-

:
create procedure DynamicSampleThree
( TextField varchar(100),
TableName varchar(100) )
returns
( LongLine varchar(32000) )
as
declare variable Chunk varchar(100);
begin
Chunk = '';
for execute statement
'select ' || TextField || ' from ' || TableName into :Chunk
do
if (Chunk is not null) then
LongLine = LongLine || Chunk || ' ';
suspend;
end

EXECUTE STATEMENT
1.
77

PSQL
, .
2.

(, , ..),
.

3.

DSQL- EXECUTE STATEMENT DSQL- ( PSQL-),


.

4.

- (..
) EXECUTE STATEMENT.

5.


( ).

6.

EXECUTE STATEMENT ,
- , .

, ,
. - EXECUTE
STATEMENT , EXECUTE STATEMENT.

EXIT
: PSQL
: 1.5
: Firebird 1.5 EXIT PSQL-.
, .

FETCH
: PSQL
: 2.0
: , , PSQL-.
:
FETCH _ INTO [:] [, [:] ...];

:
ROW_COUNT 0, , 1
.
78

PSQL

UPDATE DELETE WHERE CURRENT OF.


: . DECLARE CURSOR.

FOR EXECUTE STATEMENT ... DO


: PSQL
: 1.5
: . EXECUTE STATEMENT :: .

LEAVE
: PSQL
: 1.5
: 2.0
: LEAVE WHILE FOR. Firebird 2.0
, ,
( ).
.

LEAVE - GOTO, .

:
[:]
{FOR | WHILE} ... DO
...
( , )
...
LEAVE [];

:
,
, c = 0;:
while (b < 10) do
begin

79

PSQL
insert into Numbers(B) values (:b);
b = b + 1;
when any do
begin
execute procedure log_error (current_timestamp, ' B!');
leave;
end
end
c = 0;

- leave LoopA , leave LoopB . ,


leave (
):
stmt1 = 'select Name from Farms';
LoopA:
for execute statement :stmt1 into :farm do
begin
stmt2 = 'select Name from Animals where Farm = ''';
LoopB:
for execute statement :stmt2 || :farm || '''' into :animal do
begin
if (animal = 'Fluffy') then leave LoopB;
else if (animal = farm) then leave LoopA;
else suspend;
end
end

OPEN
: PSQL
: 2.0
: , SELECT, , .
:
OPEN _;

: See DECLARE CURSOR.

PLAN
: 1.5
: Firebird 1.5 PLAN.
80

PSQL

UDF
: 2.0
: Firebird 2.0 PSQL UDF
, .. Pascal void- C. ,
UDF - .
, ,
.

RDB$GET_CONTEXT RDB$SET_CONTEXT , UDF-.


. , , RDB$SET_CONTEXT.

81

CURRENT_CONNECTION
: DSQL, PSQL
: 1.5
: CURRENT_CONNECTION .
: INTEGER
:
select current_connection from rdb$database
execute procedure P_Login(current_connection)

CURRENT_CONNECTION (restore).

CURRENT_ROLE
: DSQL, PSQL
: 1.0
: CURRENT_ROLE .
, CURRENT_ROLE NONE.
: VARCHAR(31)
:
if (current_role <> 'MANAGER')
then exception only_managers_may_delete;
else

82


delete from Customers where custno = :custno;

CURRENT_ROLE NONE. , , CURRENT_ROLE NONE.

CURRENT_TIME
: DSQL, PSQL, ESQL
: 2.0
: CURRENT_TIME .0000, ..
. Firebird 2.0
- 0 ( ) 3 ( ), - 0.
: TIME
:
CURRENT_TIME [()]

::=

0 | 1 | 2 | 3

:
-- ( ), , 14:20:19.0000
select current_time from rdb$database
-- , , 14:20:19.1170
select current_time(3) from rdb$database
-- , , 14:20:19.1200
select current_time(2) from rdb$database

CURRENT_TIMESTAMP 3 (.. ),
CURRENT_TIMESTAMP
CURRENT_DATE CURRENT_TIME, .

CURRENT_TIMESTAMP
: DSQL, PSQL, ESQL
: 2.0
: CURRENT_TIMESTAMP
.0000, .. . Firebird 2.0
83


. 3 (.. ).
: TIMESTAMP
:
CURRENT_TIMESTAMP [()]

::=

0 | 1 | 2 | 3

:
-- ,
-- , 2008-08-13 14:20:19.6170
select current_timestamp from rdb$database
-- ,
-- , 2008-08-13 14:20:19.6200
select current_timestamp(2) from rdb$database

CURRENT_TIMESTAMP 3 (.. ),
CURRENT_TIMESTAMP
CURRENT_DATE CURRENT_TIME, .

CURRENT_TRANSACTION
: DSQL, PSQL
: 1.5
: CURRENT_TRANSACTION .
: INTEGER
:
select current_transaction from rdb$database
New.Txn_ID = current_transaction;

CURRENT_TRANSACTION (restore).

CURRENT_USER
: DSQL, PSQL
84


: 1.0
: CURRENT_USER . USER.
: VARCHAR(31)
:
create trigger bi_customers for customers before insert as
begin
New.added_by = CURRENT_USER;
New.purchases = 0;
end

DELETING
: PSQL
: 1.5
: DELETING , DELETE. .
: boolean
:
if (deleting) then
begin
insert into Removed_Cars (id, make, model, removed)
values (old.id, old.make, old.model, current_timestamp);
end

GDSCODE
: PSQL
: 1.5
: 2.0
: WHEN GDSCODE, GDSCODE Firebird. Firebird 2.0,
WHEN ANY, - Firebird, 0. GDSCODE 0 WHEN SQLCODE WHEN EXCEPTION PSQL-.
: INTEGER
85


:
when gdscode 335544551, gdscode 335544552,
gdscode 335544553, gdscode 335544707
do
begin
execute procedure log_grant_error(gdscode);
exit;
end

INSERTING
: PSQL
: 1.5
: INSERTING , INSERT. .
: boolean
:
if (inserting or updating) then
begin
if (new.serial_num is null) then
new.serial_num = gen_id(gen_serials, 1);
end

NEW
: PSQL
: 1.5, 2.0
: NEW , . NEW - ,
INSERT UPDATE. Firebird 2.0, AFTER- NEW .
:

NEW , DELETE, , NEW


NULL, .

86

'NOW'
: DSQL, PSQL, ESQL
: 2.0
: 'NOW' - , . ,
CAST(), . ,
.0000, Firebird 2.0 3 decimals, ..
. 'NOW' ,
.
: CHAR(3)
:
select 'Now' from rdb$database
-- 'Now'
select cast('Now' as date) from rdb$database
--
select cast('now' as time) from rdb$database
--
select cast('NOW' as timestamp) from rdb$database
--

CURRENT_DATE, CURRENT_TIME
CURRENT_TIMESTAMP.

OLD
: PSQL
: 1.5, 2.0
: OLD , . OLD - , DELETE UPDATE. Firebird 2.0, OLD
.
:
87

OLD , INSERT, , OLD NULL,


.

ROW_COUNT
: PSQL
: 1.5
: 2.0
: ROW_COUNT , DML- (INSERT, UPDATE, DELETE, SELECT FETCH) ,
(execute block).
: INTEGER
:
update Figures set Number = 0 where id = :id;
if (row_count = 0) then
insert into Figures (id, Number) values (:id, 0);

SELECT FETCH:
SELECT, ,
ROW_COUNT 1, , 0 .
FOR SELECT ROW_COUNT 1 ( 0).
FETCH, ROW_COUNT 1, ,
0 . ROW_COUNT 1.
Firebird 1.5 ( Firebird 2.0) ROW_COUNT 0 SELECT.

ROW_COUNT , EXECUTE STATEMENT EXECUTE PROCEDURE.

SQLCODE
: PSQL
88


: 1.5
: WHEN SQLCODE, SQLCODE SQL. Firebird 2.0,
WHEN ANY, - SQL, 0.
SQLCODE 0 WHEN GDSCODE WHEN EXCEPTION PSQL-.
: INTEGER
:
when any
do
begin
if (sqlcode <> 0) then
Msg = ' SQL!';
else
Msg = ' !';
exception ex_custom Msg;
end

UPDATING
: PSQL
: 1.5
: UPDATING , UPDATE. .
: boolean
:
if (inserting or updating) then
begin
if (new.serial_num is null) then
new.serial_num = gen_id(gen_serials, 1);
end

89

10


NULL
: 2.0
: Firebird 2.0
NULL , , A <> NULL, B + NULL, NULL <
ANY(...) .. . NULL
, ,
NULL , .
= NULL <> NULL -
IS [NOT] NULL.
: IN, ANY/SOME ALL NULL. , NULL NULL .
Predicates Firebird Null Guide.

|| ( )
: DSQL, ESQL, PSQL


: 1.0, 1.5
: Firebird 1.0 ,
(32767 ), . Firebird 1.5
32767 .

UNION
: DSQL, ESQL, PSQL
90


: 2.0
: ALL, ANY/SOME, IN
UNION. . UNION .

IS [NOT] DISTINCT FROM


: DSQL, PSQL
: 2.0
: IS DISTINCT FROM , NULL, NULL.
. IS NOT DISTINCT FROM - ,
NULL.
: Boolean
:
1 IS [NOT] DISTINCT FROM 2

:
select id, name, teacher from courses
where start_day is not distinct from end_day
if (New.Job is distinct from Old.Job)
then post_event 'job_changed';

IS [NOT] DISTINCT FROM NULL.


= <> NULL, NULL.
. :
10.1. [NOT] DISTINCT = <>

IS NOT
DISTINCT
FROM

<>

IS DISTINCT
FROM

TRUE

TRUE

FALSE

FALSE

FALSE

FALSE

TRUE

TRUE

NULL

NULL

TRUE

NULL

FALSE


NULL

NULL

FALSE

NULL

TRUE

91

NEXT VALUE FOR


: DSQL, PSQL
: 2.0
: (sequence).
(SEQUENCE) , InterBase Firebird (generator). NEXT
VALUE FOR GEN_ID(..., 1) , Firebird 2.0 .
:
NEXT VALUE FOR

:
new.cust_id = next value for custseq;
NEXT VALUE FOR 1.
, GEN_ID.

. : CREATE SEQUENCE, GEN_ID()

92

11


BIT_LENGTH()
: DSQL, PSQL
: 2.0
: .
, , 8
, RDB$CHARACTER_SETS.
: INTEGER
:
BIT_LENGTH ()

, CHAR, (, ). TRIM,
.

. : OCTET_LENGTH(), CHARACTER_LENGTH

CAST()
: DSQL, ESQL, PSQL
: 2.0
: CAST . , .
:
:
CAST ( AS _)

93


:
DATE, TIME TIMESTAMP :
_ 'date/timestring'

InterBase, .
:
select cast ('12' || '-June-' || '1959' as date) from rdb$database
update People set AgeCat = 'Old'
where BirthDate < date '1-Jan-1943'

, , ,
( DATE):
update People set AgeCat = 'Old'
where BirthDate < '1-Jan-1943'

- , / :
select date 'today' - 7 from rdb$database --
select 'today' - 7 from rdb$database --

.
11.1. CAST



[VAR]CHAR
[VAR]CHAR

[VAR]CHAR


DATE
TIME
TIMESTAMP

DATE
TIME

[VAR]CHAR
TIMESTAMP

TIMESTAMP

[VAR]CHAR
DATE
TIME

, "" - , TIMESTAMP DATE. , , CAST,


94


, . , CAST(123456789 as
SMALLINT) , CAST(' ' as DATE).
Firebird 2.0: :
cast (? as integer)

CHAR_LENGTH(), CHARACTER_LENGTH()
: DSQL, PSQL
: 2.0
: .
: INTEGER
:
CHAR_LENGTH ()
CHARACTER_LENGTH ()

, CHAR, (, ). TRIM, .

. : BIT_LENGTH(), OCTET_LENGTH

COALESCE()
: DSQL, PSQL
: 1.5
: COALESCE
NULL . NULL, NULL.
: .
:
COALESCE (<1>, <2> [, <N> ... ])

:
select
coalesce (Nickname, FirstName, 'Mr./Mrs.') || ' ' || LastName
as FullName

95


from Persons

COALESCE NULL ,
NULL. Mr./Mrs.,
Nickname FirstName NULL.

Firebird 1.0 COALESCE , UDF- *nvl.

EXTRACT()
: DSQL, ESQL, PSQL
: IB 6
: / , DATE, TIME
TIMESTAMP. InterBase 6, .
: SMALLINT DECIMAL(6,4)
:
EXTRACT (<> FROM <>)
<>
<>

::=
::=

YEAR | MONTH | DAY | WEEKDAY | YEARDAY | HOUR | MINUTE | SECOND


DATE, TIME TIMESTAMP

/, (, SECOND DATE YEAR TIME), .


DECIMAL(6,4) SECOND SMALLINT
. :
11.2. EXTRACT

YEAR

19999

MONTH

112

DAY

131

WEEKDAY

06

0 =

YEARDAY

0365

0 = 1

HOUR

023

MINUTE

059

SECOND

0.000059.999
96

GEN_ID()
: DSQL, ESQL, PSQL
: (sequence) () . Firebird 2.0 SQL-
NEXT VALUE FOR, , , 1.
: BIGINT
:
GEN_ID (, <>)
<>

::=

:
new.rec_id = gen_id(gen_recnum, 1);

GEN_ID() 1 !

. : NEXT VALUE FOR, CREATE GENERATOR

IIF()
: DSQL, PSQL
: 2.0
: IIF . TRUE,
, .
: .
:
IIF (<>, 1, 2)
<>

::=

:
select iif( sex = 'M', '.', '.' ) from Customers

97


IIF(, 1, 2) - CASE WHEN
THEN 1 ELSE 2 END. , IIF -

? : C- .

LOWER()
: DSQL, ESQL, PSQL
: 2.0
: , . ASCII , () , : ASCII NONE,
ASCII; OCTETS
.
: VAR(CHAR)
:
LOWER ()

:
select lower(' 123 PRIMER') from rdb$database -- ' 123 primer'

. : UPPER

NULLIF()
: DSQL, PSQL
: 1.5
: , NULL, .
: .
:
NULLIF (<1>, <2>)

:
select avg( nullif(Weight, -1) ) from FatPeople

Firebird 1.0 NULLIF , UDF- *nullif.

98

OCTET_LENGTH()
: DSQL, PSQL
: 2.0
: .
, , ,
RDB$CHARACTER_SETS.

, CHAR, (, ). TRIM, .

: INTEGER
:
OCTET_LENGTH ()

. : BIT_LENGTH(), CHARACTER_LENGTH

RDB$GET_CONTEXT()
: DSQL, ESQL, PSQL
: 2.0
: SYSTEM, USER_SESSION
USER_TRANSACTION.
: VARCHAR(255)
:
RDB$GET_CONTEXT ('<_>', '<>')
<_>
<>

::=
::=

SYSTEM | USER_SESSION | USER_TRANSACTION


80

: USER_SESSION USER_TRANSACTION
.
RDB$SET_CONTEXT(), RDB
$GET_CONTEXT(). SYSTEM , :
99

11.3. SYSTEM
DB_NAME

(alias).

NETWORK_PROTOCOL

, .
'TCPv4', 'WNET', 'XNET' NULL.

CLIENT_ADDRESS

TCPv4 IP-. XNET ID


. NULL.

CURRENT_USER

, CURRENT_USER.

CURRENT_ROLE

, CURRENT_ROLE.

SESSION_ID

,
CURRENT_CONNECTION variable.

TRANSACTION_ID

,
CURRENT_TRANSACTION.

ISOLATION_LEVEL

. 'READ
COMMITTED', 'SNAPSHOT' 'CONSISTENCY'.

: , 255 . SYSTEM, , . , NULL. - , NULL.


:
select rdb$get_context('SYSTEM', 'DB_NAME') from rdb$database
New.UserAddr = rdb$get_context('SYSTEM', 'CLIENT_ADDRESS');
insert into MyTable (TestField)
values (rdb$get_context('USER_SESSION', 'MyVar'))

. : RDB$SET_CONTEXT()

RDB$SET_CONTEXT()
: DSQL, ESQL, PSQL
: 2.0
: ,
USER_SESSION USER_TRANSACTION.
: INTEGER
100


:
RDB$SET_CONTEXT ('<_>', '<>', <> | NULL)
<_>
<>
<>

::=
::=
::=

USER_SESSION | USER_TRANSACTION
80
, VARCHAR(255)

: USER_SESSION USER_TRANSACTION
.
RDB$SET_CONTEXT() RDB$GET_CONTEXT(). USER_SESSION , USER_TRANSACTION , . , .
: 1, , 0,
. , NULL. , .
- , NULL.
:
select rdb$set_context('USER_SESSION', 'MyVar', 493) from rdb$database
rdb$set_context('USER_SESSION', 'RecordsFound', RecCounter);
select rdb$set_context('USER_TRANSACTION', 'Savepoints', 'Yes')
from rdb$database

Notes:
( ) 1000.
, (.. USER_TRANSACTION) ROLLBACK RETAIN
ROLLBACK TO SAVEPOINT , .
RDB$SET_CONTEXT() RDB$GET_CONTEXT() , UDF-, .
UDF- - ,
PSQL, . .
. : RDB$GET_CONTEXT()

SUBSTRING()
: DSQL, PSQL
: 1.0
: 2.0
101


: ,
.
: CHAR(n)
:
SUBSTRING (<> FROM [FOR ])
<>

::=

SUBSTRING , (
1). FOR , . FOR , ,
, .
NULL, NULL. ,
( NULL).
Firebird 1., . Firebird
2.0 .
,
. , substring('' from 2 for 3) CHAR(6)
''.
SUBSTRING :
([VAR]CHAR) ;
BLOB- (subtype 0);
BLOB- (subtype 1), .
SUBSTRING BLOB-
.
:
insert into AbbrNames(AbbrName)
select substring(LongName from 1 for 3) from LongNames

TRIM()
: DSQL, PSQL
: 2.0
: / ( ) .
: VAR(CHAR)
:
TRIM ([<>] )

102

<>
<>
<>

::=
::=
::=

{[<>] [<>]} FROM


BOTH | LEADING | TRAILING /* BOTH */
/* ' ' () */

<> ( ).
:
select trim (' Waste no space
-- 'Waste no space'
select trim (leading from '
-- 'Waste no space

') from rdb$database

Waste no space
'

') from rdb$database

select trim (leading '.' from ' Waste no space


-- ' Waste no space
'

') from rdb$database

select trim (trailing '!' from 'Help!!!!') from rdb$database -- 'Help'


select trim ('la' from 'lalala I love you Ella') from rdb$database
-- ' I love you El'
select trim ('la' from 'lalalala') from rdb$database -- '' ( )
select trim ('la' from 'Lalala I love you Ella') from rdb$database
-- 'Lalala I love you El'

UPPER()
: DSQL, ESQL, PSQL
: 2.0
: , . ASCII , () ,
: ASCII NONE,
ASCII; OCTETS
.
: VAR(CHAR)
:
UPPER ()

:
select upper(' 123 PRIMER') from rdb$database
-- ' 123 PRIMER'

. : LOWER

103

12

(UDF)
. Firebird :
ib_udf InterBase;
fbudf , Firebird 1.0 Windows 1.5 Linux.
.
Firebird 2.0 NULL
UDF- NULL ( BY DESCRIPTOR).
NULL,
NULL , NULL . NULL UDF.

/
addDay
: fbudf
: 1.0 (Win), 1.5 (Linux)
: , , . .
: TIMESTAMP
:
addday (, _)

:
DECLARE EXTERNAL FUNCTION addDay
TIMESTAMP, INT
RETURNS TIMESTAMP

104

(UDF)
ENTRY_POINT 'addDay' MODULE_NAME 'fbudf'

addHour
: fbudf
: 1.0 (Win), 1.5 (Linux)
: , , . .
: TIMESTAMP
:
addhour (, _)

:
DECLARE EXTERNAL FUNCTION addHour
TIMESTAMP, INT
RETURNS TIMESTAMP
ENTRY_POINT 'addHour' MODULE_NAME 'fbudf'

addMilliSecond
: fbudf
: 1.0 (Win), 1.5 (Linux)
: , ,
. .
: TIMESTAMP
:
addmillisecond (, _)

:
DECLARE EXTERNAL FUNCTION addMilliSecond
TIMESTAMP, INT
RETURNS TIMESTAMP
ENTRY_POINT 'addMilliSecond' MODULE_NAME 'fbudf'

addMinute
: fbudf
105

(UDF)
: 1.0 (Win), 1.5 (Linux)
: , , . .
: TIMESTAMP
:
addminute (, _)

:
DECLARE EXTERNAL FUNCTION addMinute
TIMESTAMP, INT
RETURNS TIMESTAMP
ENTRY_POINT 'addMinute' MODULE_NAME 'fbudf'

addMonth
: fbudf
: 1.0 (Win), 1.5 (Linux)
: , , . .
: TIMESTAMP
:
addmonth (, _)

:
DECLARE EXTERNAL FUNCTION addMonth
TIMESTAMP, INT
RETURNS TIMESTAMP
ENTRY_POINT 'addMonth' MODULE_NAME 'fbudf'

addSecond
: fbudf
: 1.0 (Win), 1.5 (Linux)
: , , . .
: TIMESTAMP
106

(UDF)
:
addsecond (, _)

:
DECLARE EXTERNAL FUNCTION addSecond
TIMESTAMP, INT
RETURNS TIMESTAMP
ENTRY_POINT 'addSecond' MODULE_NAME 'fbudf'

addWeek
: fbudf
: 1.0 (Win), 1.5 (Linux)
: , , . .
: TIMESTAMP
:
addweek (, _)

:
DECLARE EXTERNAL FUNCTION addWeek
TIMESTAMP, INT
RETURNS TIMESTAMP
ENTRY_POINT 'addWeek' MODULE_NAME 'fbudf'

addYear
: fbudf
: 1.0 (Win), 1.5 (Linux)
: , , .
.
: TIMESTAMP
:
addyear (, _)

:
DECLARE EXTERNAL FUNCTION addYear
TIMESTAMP, INT

107

(UDF)
RETURNS TIMESTAMP
ENTRY_POINT 'addYear' MODULE_NAME 'fbudf'

dow
: fbudf
: 1.0 (Win), 1.5 (Linux)
: .
: VARCHAR(15)
:
dow ()

:
DECLARE EXTERNAL FUNCTION dow
TIMESTAMP,
VARCHAR(15) RETURNS PARAMETER 2
ENTRY_POINT 'DOW' MODULE_NAME 'fbudf'

getExactTimestamp
: fbudf
: 1.0 (Win), 1.5 (Linux)
: . , Firebird 2.0 CURRENT_TIMESTAMP
. Firebird 2.0 CURRENT_TIMESTAMP,
.
: TIMESTAMP
:
getexacttimestamp()

:
DECLARE EXTERNAL FUNCTION getExactTimestamp
TIMESTAMP RETURNS PARAMETER 1
ENTRY_POINT 'getExactTimestamp' MODULE_NAME 'fbudf'

sdow
: fbudf
108

(UDF)
: 1.0 (Win), 1.5 (Linux)
: .
: VARCHAR(5)
:
sdow ()

:
DECLARE EXTERNAL FUNCTION sdow
TIMESTAMP,
VARCHAR(5) RETURNS PARAMETER 2
ENTRY_POINT 'SDOW' MODULE_NAME 'fbudf'


ascii_char
: ib_udf
: 1.0, 2.0
: ASCII- ( ASCII-).
: VARCHAR(1)
:
ascii_char (int)

:
DECLARE EXTERNAL FUNCTION ascii_char
INTEGER NULL
RETURNS CSTRING(1) FREE_IT
ENTRY_POINT 'IB_UDF_ascii_char' MODULE_NAME 'ib_udf'

, CSTRING(1), CHAR(1),
InterBase. ,
VARCHAR(1).
:
ascii_char(0) , ASCII- 0.

109

(UDF)
Firebird 2.0 CHAR(1).

lower
: ib_udf
: 2.0
: , .
ASCII-. Firebird 2.0 LOWER.
: VARCHAR(n)
:
"LOWER" ()

:
DECLARE EXTERNAL FUNCTION "LOWER"
CSTRING(255) NULL
RETURNS CSTRING(255) FREE_IT
ENTRY_POINT 'IB_UDF_lower' MODULE_NAME 'ib_udf'

LOWER LOWER.

lpad
: ib_udf
: 1.5
: 1.5.2, 2.0
: , , . , , .
: VARCHAR(n)
:
lpad (, , )

:
DECLARE EXTERNAL FUNCTION lpad
CSTRING(255) NULL, INTEGER, CSTRING(1) NULL
RETURNS CSTRING(255) FREE_IT
ENTRY_POINT 'IB_UDF_lpad' MODULE_NAME 'ib_udf'

110

(UDF)
:
,
.
, .
, NULL.
NULL, 0.
NULL NULL, .
Firebird 2.0 CHAR(n).

ltrim
: ib_udf
: 1.5, 1.5.2, 2.0
: .
: VARCHAR(n)
:
ltrim ()

:
DECLARE EXTERNAL FUNCTION ltrim
CSTRING(255) NULL
RETURNS CSTRING(255) FREE_IT
ENTRY_POINT 'IB_UDF_ltrim' MODULE_NAME 'ib_udf'

:
Firebird 2.0 CHAR(n).
Firebird 1.0 NULL, NULL .

Firebird 2.0 TRIM.

rpad
: ib_udf
: 1.5
111

(UDF)
: 1.5.2, 2.0
: , , . , , .
: VARCHAR(n)
:
rpad (, , )

:
DECLARE EXTERNAL FUNCTION rpad
CSTRING(255) NULL, INTEGER, CSTRING(1) NULL
RETURNS CSTRING(255) FREE_IT
ENTRY_POINT 'IB_UDF_rpad' MODULE_NAME 'ib_udf'

:
,
.
, .
, NULL.
NULL, 0.
NULL NULL, .
Firebird 2.0 CHAR(n).

right
: fbudf
: 1.0 (Win), 1.5 (Linux)
: .
: VARCHAR(100)
:
right (, )

:
DECLARE EXTERNAL FUNCTION right
VARCHAR(100) BY DESCRIPTOR, SMALLINT,
VARCHAR(100) BY DESCRIPTOR RETURNS PARAMETER 3
ENTRY_POINT 'right' MODULE_NAME 'fbudf'

112

(UDF)

rtrim
: ib_udf
: 1.5, 1.5.2, 2.0
: .
: VARCHAR(n)
:
rtrim ()

:
DECLARE EXTERNAL FUNCTION rtrim
CSTRING(255) NULL
RETURNS CSTRING(255) FREE_IT
ENTRY_POINT 'IB_UDF_rtrim' MODULE_NAME 'ib_udf'

:
Firebird 2.0 CHAR(n).
Firebird 1.0 NULL, NULL .

Firebird 2.0 TRIM.

string2blob
: fbudf
: 1.0 (Win), 1.5 (Linux)
: BLOB.
: BLOB
:
string2blob (str)

:
DECLARE EXTERNAL FUNCTION string2blob
VARCHAR(300) BY DESCRIPTOR,
BLOB RETURNS PARAMETER 2
ENTRY_POINT 'string2blob' MODULE_NAME 'fbudf'

113

(UDF)

substr
: ib_udf
: 1.0, 1.5.2, 2.0
: , c , ,
, , . 1. , .
1,
. - .
: VARCHAR(n)
:
substr (, _, _)

:
DECLARE EXTERNAL FUNCTION substr
CSTRING(255) NULL, SMALLINT, SMALLINT
RETURNS CSTRING(255) FREE_IT
ENTRY_POINT 'IB_UDF_substr' MODULE_NAME 'ib_udf'

:
Firebird 2.0 CHAR(n).
InterBase, , NULL.

SUBSTRING,
.

substrlen
: ib_udf
: 1.0
: 1.5.2, 2.0
: , , ,
. 1. 1
, .
- .
: VARCHAR(n)
114

(UDF)
:
substrlen (, _, )

:
DECLARE EXTERNAL FUNCTION substrlen
CSTRING(255) NULL, SMALLINT, SMALLINT
RETURNS CSTRING(255) FREE_IT
ENTRY_POINT 'IB_UDF_substrlen' MODULE_NAME 'ib_udf'

: Firebird 2.0 CHAR(n).

Firebird 1.0 SUBSTRING, substrlen .


dpower
: fbudf
: 1.0 (Win), 1.5 (Linux)
: x y.
: DOUBLE PRECISION
:
dpower (x, y)

:
DECLARE EXTERNAL FUNCTION dPower
DOUBLE PRECISION BY DESCRIPTOR, DOUBLE PRECISION BY DESCRIPTOR,
DOUBLE PRECISION BY DESCRIPTOR
RETURNS PARAMETER 3
ENTRY_POINT 'power' MODULE_NAME 'fbudf'

log
: ib_udf
: 1.5
115

(UDF)
: Firebird 1.5 log y x. Firebird 1.0
InterBase x y.
: DOUBLE PRECISION
:
log (x, y)

:
DECLARE EXTERNAL FUNCTION log
DOUBLE PRECISION, DOUBLE PRECISION
RETURNS DOUBLE PRECISION BY VALUE
ENTRY_POINT 'IB_UDF_log' MODULE_NAME 'ib_udf'

Firebird 1.5, ,
.

rand
: ib_udf
: 2.0
: . Firebird 2.0 ,
. Firebird 2 (
) , srand.
: DOUBLE PRECISION
:
rand ()

:
DECLARE EXTERNAL FUNCTION rand
RETURNS DOUBLE PRECISION BY VALUE
ENTRY_POINT 'IB_UDF_rand' MODULE_NAME 'ib_udf'

round, i64round
: fbudf
: 1.0 (Win), 1.5 (Linux)
: 1.5
116

(UDF)
: (numeric/decimal).
.
: INTEGER / NUMERIC(18,4)
:
round ()
i64round ()

0 -0.6 0.
-0.6 -1 +1 ( 1).
-1 -1.6 -1.
-1.6 -2 -2.
..

:
Firebird 1.0 round:
DECLARE EXTERNAL FUNCTION Round
INT BY DESCRIPTOR, INT BY DESCRIPTOR
RETURNS PARAMETER 2
ENTRY_POINT 'round' MODULE_NAME 'fbudf'
DECLARE EXTERNAL FUNCTION i64Round
NUMERIC(18,4) BY DESCRIPTOR, NUMERIC(18,4) BY DESCRIPTOR
RETURNS PARAMETER 2
ENTRY_POINT 'round' MODULE_NAME 'fbudf'

Firebird 1.5 fbround:


DECLARE EXTERNAL FUNCTION Round
INT BY DESCRIPTOR, INT BY DESCRIPTOR
RETURNS PARAMETER 2
ENTRY_POINT 'fbround' MODULE_NAME 'fbudf'
DECLARE EXTERNAL FUNCTION i64Round
NUMERIC(18,4) BY DESCRIPTOR, NUMERIC(18,4) BY DESCRIPTOR
RETURNS PARAMETER 2
ENTRY_POINT 'fbround' MODULE_NAME 'fbudf'

Firebird 1.0 1.5 ( DROP EXTERNAL FUNCTION) .

srand
: ib_udf
117

(UDF)
: 2.0
: , . .
. rand.
: DOUBLE PRECISION
:
srand ()

:
DECLARE EXTERNAL FUNCTION srand
RETURNS DOUBLE PRECISION BY VALUE
ENTRY_POINT 'IB_UDF_srand' MODULE_NAME 'ib_udf'

truncate, i64truncate
: fbudf
: 1.0 (Win), 1.5 (Linux)
: 1.5
: (numeric/decimal).
.
: INTEGER / NUMERIC(18)
:
truncate ()
i64truncate ()

, , ..
(, -1.1
-2), : -1 0 0; -1 -1.

:
Firebird 1.0 truncate:
DECLARE EXTERNAL FUNCTION Truncate
INT BY DESCRIPTOR, INT BY DESCRIPTOR
RETURNS PARAMETER 2
ENTRY_POINT 'truncate' MODULE_NAME 'fbudf'
DECLARE EXTERNAL FUNCTION i64Truncate
NUMERIC(18) BY DESCRIPTOR, NUMERIC(18) BY DESCRIPTOR
RETURNS PARAMETER 2

118

(UDF)
ENTRY_POINT 'truncate' MODULE_NAME 'fbudf'

Firebird 1.5 fbtruncate:


DECLARE EXTERNAL FUNCTION Truncate
INT BY DESCRIPTOR, INT BY DESCRIPTOR
RETURNS PARAMETER 2
ENTRY_POINT 'fbtruncate' MODULE_NAME 'fbudf'
DECLARE EXTERNAL FUNCTION i64Truncate
NUMERIC(18) BY DESCRIPTOR, NUMERIC(18) BY DESCRIPTOR
RETURNS PARAMETER 2
ENTRY_POINT 'fbtruncate' MODULE_NAME 'fbudf'

Firebird 1.0 1.5 ( DROP EXTERNAL FUNCTION) .

*nullif
: fbudf
: 1.0 (Win), 1.5 (Linux)
: *nullif integer, bigint, double varchar
, . ,
NULL. Firebird 1.5 NULLIF.
: .
:
inullif
i64nullif
dnullif
snullif

(integer, integer)
(bigint, bigint)
(double, double)
(varchar, varchar)

NULL, NULL,
. , NULLIF .
i64nullif dnullif ,
(NUMERIC(18,0) DOUBLE PRECISION). , cast.

:
DECLARE EXTERNAL FUNCTION inullif
INT BY DESCRIPTOR, INT BY DESCRIPTOR
RETURNS INT BY DESCRIPTOR
ENTRY_POINT 'iNullIf' MODULE_NAME 'fbudf'

119

(UDF)
DECLARE EXTERNAL FUNCTION i64nullif
NUMERIC(18,4) BY DESCRIPTOR, NUMERIC(18,4) BY DESCRIPTOR
RETURNS NUMERIC(18,4) BY DESCRIPTOR
ENTRY_POINT 'iNullIf' MODULE_NAME 'fbudf'
DECLARE EXTERNAL FUNCTION dnullif
DOUBLE PRECISION BY DESCRIPTOR, DOUBLE PRECISION BY DESCRIPTOR
RETURNS DOUBLE PRECISION BY DESCRIPTOR
ENTRY_POINT 'dNullIf' MODULE_NAME 'fbudf'
DECLARE EXTERNAL FUNCTION snullif
VARCHAR(100) BY DESCRIPTOR, VARCHAR(100) BY DESCRIPTOR,
VARCHAR(100) BY DESCRIPTOR RETURNS PARAMETER 3
ENTRY_POINT 'sNullIf' MODULE_NAME 'fbudf'

*nvl
: fbudf
: 1.0 (Win), 1.5 (Linux)
: nvl integer, bigint, double varchar
, NULL. NULL,
. Firebird 1.5 COALESCE().
: .
:
invl
i64nvl
dnvl
snvl

(integer, integer)
(bigint, bigint)
(double, double)
(varchar, varchar)

i64nvl dnvl ,
( NUMERIC(18,0) DOUBLE PRECISION). ,
cast.

:
DECLARE EXTERNAL FUNCTION invl
INT BY DESCRIPTOR, INT BY DESCRIPTOR
RETURNS INT BY DESCRIPTOR
ENTRY_POINT 'idNvl' MODULE_NAME 'fbudf'
DECLARE EXTERNAL FUNCTION i64nvl
NUMERIC(18,0) BY DESCRIPTOR, NUMERIC(18,0) BY DESCRIPTOR
RETURNS NUMERIC(18,0) BY DESCRIPTOR
ENTRY_POINT 'idNvl' MODULE_NAME 'fbudf'

120

(UDF)
DECLARE EXTERNAL FUNCTION dnvl
DOUBLE PRECISION BY DESCRIPTOR, DOUBLE PRECISION BY DESCRIPTOR
RETURNS DOUBLE PRECISION BY DESCRIPTOR
ENTRY_POINT 'idNvl' MODULE_NAME 'fbudf'
DECLARE EXTERNAL FUNCTION snvl
VARCHAR(100) BY DESCRIPTOR, VARCHAR(100) BY DESCRIPTOR,
VARCHAR(100) BY DESCRIPTOR RETURNS PARAMETER 3
ENTRY_POINT 'sNvl' MODULE_NAME 'fbudf'

121

A:

NONE

Firebird 1.5.1
Firebird 1.5.1 NONE -
.
Firebird 1.5.0 NONE,
, SJIS () WIN1251 ()
, .
/ .
, NONE
- , (introducer) . NONE.
Firebird 1.5.1 - ,
NONE, - , .
NONE NONE
, - , -
.

NONE.
.
sqlsubtype XSQLVAR
RDB$CHARACTER_SETS.

NONE
(introducer).

122

NONE,
, (introducer) cast.
WIN1251. _ () .
:
_WIN1251
Cast:
CAST ( AS [VAR]CHAR() CHARACTER SET WIN1251)

WITH LOCK
,
. Firebird 1.5
- ( ,
), update, ( , ,
..)
Firebird 1.5 WITH LOCK Select.
, - .
Firebird .
, .
, . WITH
LOCK DSQL PSQL, - SELECT, . WITH LOCK :
;
(join);
DISTINCT, GROUP BY
;
(view);
, ;
(external table).

SELECT ... FROM


[WHERE ...]
[FOR UPDATE [OF ...]]
[WITH LOCK]

WITH LOCK , Firebird


- .
123


FOR UPDATE, ,
. ,
() ,
( - WITH
LOCK DELETE, INSERT, UPDATE).
ROLLBACK RETAIN ROLLBACK TO
SAVEPOINT) ( ).
,
( PSQL-), .
(TPB) :
A.1.

isc_tpb_consistency

, .

isc_tpb_concurrency
+ isc_tpb_nowait


, ,

.

isc_tpb_concurrency
+ isc_tpb_wait


, , . , ( ),
( ). , . , , .

, (
isc_tpb_read_committed
), + isc_tpb_nowait
.
, (
),
(isc_tpb_read_committed
) ( ,
+ isc_tpb_wait
) .
(TPB)
.

124

WITH LOCK
UPDATE ( SELECT ... WITH LOCK) , , , ( ). , .
, , (gdscode sqlcode), .
, , SELECT ... WITH LOCK,
WHERE . , , , WHERE, . , , , ( , ).
(fetch).
. Firebird
( ). ,
.
, (,
, , ), FOR UPDATE.
SELECT ( WITH LOCK - ) .
1. , ,
.

WITH LOCK
,
, (
isc_tpb_wait). ,
.
/
, ,
.
. (1) 125


(2)
, ( ).
Firebird , ,
.

--
SELECT * FROM DOCUMENT WHERE ID=? WITH LOCK
--
SELECT * FROM DOCUMENT WHERE PARENT_ID=? WITH LOCK
--
SELECT * FROM DOCUMENT WHERE PARENT_ID=? FOR UPDATE WITH LOCK

CSTRING
(UDF), , CSTRING(n) .
n . , CSTRING, . Firebird

SQL, n. ,
, 32
. ,
CSTRING ( ib_udf.sql) 255 ( Firebird 1.5.1 - 80 ). , lpad:
DECLARE EXTERNAL FUNCTION lpad
CSTRING(255), INTEGER, CSTRING(1)
RETURNS CSTRING(255) FREE_IT
ENTRY_POINT 'IB_UDF_lpad' MODULE_NAME 'ib_udf'

, - ( ).
(, 100 ), , : (1)
CSTRING(32000), , 32000
( 100 ) (2)
:
DECLARE EXTERNAL FUNCTION lpad
CSTRING(100), INTEGER, CSTRING(1)
RETURNS CSTRING(100) FREE_IT

126


ENTRY_POINT 'IB_UDF_lpad' MODULE_NAME 'ib_udf';
DECLARE EXTERNAL FUNCTION lpadbig
CSTRING(32000), INTEGER, CSTRING(1)
RETURNS CSTRING(32000) FREE_IT
ENTRY_POINT 'IB_UDF_lpad' MODULE_NAME 'ib_udf';

lpad() , lpadbig()
- . ,
, .

NULL UDF
Firebird 2.0 UDF NULL
NULL- , , 0 . UDF-,
BY DESCRIPTOR, Firebird 1.0. fbudf
, ,
ib_udf, , InterBase.
, UDF- NULL 0.
Firebird 2 UDF-, NULL , NULL , , :
declare external function ltrim
cstring(255) null
returns cstring(255) free_it
entry_point 'IB_UDF_ltrim' module_name 'ib_udf';

, NULL
NULL . , . ib_udf ib_udf2.sql
UDF ;
ib_udf.sql.


Firebird
Firebird 1.0 2.0 , ,
. .

127

A.2. Firebird 1.0 2.0

Firebird
1.0, 1.0.2

1.0.3

1.5.x

2.0.x

1024

62

50

41

62

50

41

62

50

41

50

35

27

2048

65

65

65

126

101

84

126

101

84

101

72

56

4096

65

65

65

254

203

169

254

203

169

203

145

113

8192

65

65

65

510

408

340

257

257

257

408

291

227

16384

65

65

65

1022

818

681

257

257

257

818

584

454

128

B:

manual CVS- (. http://
sourceforge.net/cvs/?group_id=9028)

0.9
24
2008

PV

, Firebird 1.5 Language


Reference Update, Firebird 2.0
( ).

0.9

GR

06
2009

129

C:

Public Documentation
License Version 1.0 ( ); , . http://www.firebirdsql.org/
pdfmanual/pdl.pdf (PDF) http://www.firebirdsql.org/manual/pdl.html (HTML).
Firebird 2.0 Language Reference Update.
: Paul Vinkenoog .
Copyright (C) 2008. . : paul at vinkenoog dot nl.
material: J. Beesley, Helen Borrie, Arno Brinkman, Alex
Peshkov, Nickolay Samofatov, Dmitry Yemanov.
: GR (C) 2009 . . :
firebird_sql at mail dot ru.
: ,
.
License notice
The contents of this Documentation are subject to the Public Documentation License Version 1.0 (the License);
you may only use this Documentation if you comply with the terms of this License. Copies of the License
are available at http://www.firebirdsql.org/pdfmanual/pdl.pdf (PDF) and http://www.firebirdsql.org/manual/pdl.
html (HTML).
The Original Documentation is titled Firebird 2.0 Language Reference Update.
The Initial Writers of the Original Documentation are: Paul Vinkenoog et al.
Copyright (C) 2008. All Rights Reserved. Initial Writers contact: paul at vinkenoog dot nl.
Writers and Editors of included PDL-licensed material (the et al.) are: J. Beesley, Helen Borrie, Arno
Brinkman, Alex Peshkov, Nickolay Samofatov, Dmitry Yemanov.
Included portions are Copyright (C) 2001-2007 by their respective authors. All Rights Reserved.

130

Оценить