![]() | ![]() |
Home |
|
|
Access Service User's Guide DirectConnect for AS/400 |
|
| Chapter 7 Using Sybase mode commands |
Chapter 7
This chapter describes the Transact-SQL (T-SQL) syntax subset recognized in the Sybase SQL transformation mode. SQL commands, clauses, and other syntactical elements are presented in alphabetical order.
Many of the commands use up to three-part table names. However, because the AS/400 supports only one-part or two-part names, SQL transformation removes the first part of an AS/400 three-part table name.
All Sybase mode commands are issued as language commands, unless otherwise noted.
The following T-SQL commands are presented in this chapter. Each command section contains a description, syntax, examples, and comments.
SQL Command | Description | Location |
begin transaction | Marks the starting point of a user-specified transaction. | |
commit transaction | Commits all work performed for this transaction. | |
create index | Creates a new index on a table. | |
create table | Creates new tables. | |
create view | Creates a new view. | |
delete (cursor command) | Removes rows from a table using a cursor command. | |
delete (dynamic command) | Removes rows from a table using a dynamic command. | |
delete (language command) | Removes rows from a table using a language command. | |
drop index | Removes an index from a table. | |
drop table | Removes a table. | |
drop view | Removes a view. | |
grant | Assigns authorization to users. | |
insert (dynamic command) | Adds new rows to a table or view using a dynamic command. | |
insert (language command) | Adds new rows to a table or view using a language command. | |
prepare transaction | Checks to see if connections to databases are active. | |
revoke | Revokes authorization of users. | |
rollback transaction | Rolls back or aborts the current transaction. | |
select | Retrieves rows from the database objects. | |
truncate table | Truncates the table by removing all rows. This statement is not logged and is not part of any transaction. | |
update (cursor command) | Changes data in row made current by a read cursor (positional update) | |
update (dynamic command) | Changes data in existing rows using a dynamic command. | |
update (language command) | Changes data in existing rows using a language command. | |
use | Accesses an existing database. |
For more information about the Sybase SQL transformation mode commands, see the Sybase SQL Server Reference Manual.
Any reference to Adaptive Server applies to SQL Server 11.1 or earlier, unless otherwise indicated.
Marks the starting point of a user-specified transaction.
begin transaction [transaction_name]
is the name assigned to the transaction. It must conform to the rules for identifiers.
Example 1
begin transaction
The access service accepts this statement but does not generate any corresponding AS/400 SQL statement. When the access service transaction mode is set to short and it receives this statement, it remembers that a transaction is running. As a result, the access service suspends the automatic commitment of SQL statements until it encounters the next commit or rollback statement.
The access service library accepts the transaction_name, and then strips it off before passing it to the target.
Commits the work resulting from the current transaction.
commit transaction {transaction_name}is the name assigned to the transaction. It must conform to the rules for identifiers that are described in the Sybase SQL Server Reference Manual.
Example 1
commit transaction
The access service accepts this statement and translates it to the AS/400 commit statement.
If a transaction is not currently active, this statement has no effect.
In Sybase mode, transaction_name is stripped from the statement before it is passed on the target.
The access service converts both commit transaction transaction_name and commit transaction without a transaction name to a commit statement.
Using a language command, adds a new index to an existing table.
create [unique] index index_nameon [[database.]owner.]table_name(column_name [, column_name]...)
is an optional keyword that prohibits duplicate index values.
index_nameis the name of the index. Index names must be unique within a table but need not be unique within a database.
table_nameis the name of the table that contains the indexed column or columns.
column_nameis the column to be included in the index. Composite indexes are based on the combined values of up to 16 columns. The sum of the maximum lengths of all the columns used in a composite index cannot exceed 256 bytes.
Example 1
create index au_id_ind on authors (au_id)
Example 2
create index ind1 on titleauthor (au_id, title_id)
Columns of type bit, text, and image cannot be indexed.
You cannot create an index on a view.
The access service initially accepts the following T-SQL statement components but then strips them out of the statement:
clustered and nonclustered
with { fillfactor = x, ignore_dup_key, sorted_data }
on segment_name, which specifies the segment where the index is to be created.
Creates a new table.
create table [database.[owner].]table_name (column_name
datatype {null | not null}
[{, next_column }...])
[on segment_name]is the name of the new table. It conforms to the rules for identifiers and is unique within the database and to the owner.
column_nameis the name of the column in the table. It conforms to the rules for identifiers and is unique in the table.
datatypeis the datatype of the column. Only system datatypes are used. As shown in Table 7-3 , several datatypes expect a length, n, in parentheses:
datatype(n)null | not null
specifies a null value if a user does not provide a value during an insertion and no default exists (for null), or that a user must provide a non-null value if no default exists (for not null).
next_columnindicates that you can include additional column definitions (separated by commas) using the same syntax described for a column definition.
Example 1
create table titles
(title_id tid not null,
title varchar(80) not null,
type char(12) not null,
pub_id char(4) null,
price money null,
advance money null,
total_sales int null,
notes varchar(200) null,
pubdate datetime not null,
contract bit not null)Transact-SQL allows you to specify null or not null, with a default of not null. The AS/400 allows only not null to be specified, and the default is null. The following table shows how the access service transforms this clause.
Transact-SQL Specification | Transformed to... |
null | <nothing> |
not null | not null |
<nothing> | not null |
The following table shows the access service transformation of datatype specifications.
Transact-SQL Datatype | AS/400 Datatype |
tinyint | SMALLINT |
smallint | SMALLINT |
int | INT |
numeric(p,s) | NUMERIC(p,s) |
decimal(p,s) | DECIMAL(p,s) |
float (double precision) | FLOAT |
real | REAL |
smallmoney | DECIMAL(10,4) |
money | DECIMAL(19,4) |
smalldatetime | TIMESTAMP |
datetime | TIMESTAMP |
char(n) | CHAR( n) |
varchar(n) | VARCHAR(n) |
text | LONG VARCHAR(n) |
binary(n) | CHAR(n) FOR BIT DATA |
varbinary(n) | VARCHAR(n) FOR BIT DATA |
image | LONG VARCHAR(n) FOR BIT DATA |
bit | SMALLINT |
Be aware that the AS/400 hard-codes text as varchar(32704). As a result, any table with a text field has only a limited amount of space available for any other columns in that table. For example, the AS/400 translates the SQL statement create table test (col1 text, col2 char(37)) to the following:
create table test (col1 varchar(32704), col2 char(63))
The preceding statement fails, because the AS/400 has a total restriction of 32,776 bytes for all columns, and text is already taking up 32,704 bytes of the total space allowed.
Creates a new view.
create view [database_name.][owner.]view_name[(column_name [, column_name]...)] as select [distinct] select_statement[with check option]
is the name of the view. The view name cannot include the database name. It must conform to the rules for identifiers.
column_nameis the name of the column in the view. It must conform to the rules for identifiers.
selectbegins the select statement that defines the view.
distinctspecifies that the view cannot contain duplicate rows (optional).
select_statementcompletes the select statement that defines the view. It can include more than one table and other views.
with check optionindicates that all data modification statements are validated against the view selection criteria. All rows inserted or updated through the view must remain visible through the view.
Example 1
The following example creates the new view from old view.
create view new_view (col1, col2) as select col1, col2 from old_view
You can use views as security mechanisms by granting authorization on a view but not on its underlying tables.
Using a cursor command, delete removes a row from a table. The row affected must have been made current by a read cursor.
Any valid object in the catalog can be substituted for table_name or view_name.
1. delete [[database.]owner.]{table_name | view_name}2. delete [from] [[database.]owner.]{table_name|view_name}
where current of cursor_name(after delete) is an optional keyword used for compatibility with other versions of SQL. Follow it with the name of the table or view from which you want to remove rows.
where current ofis a standard where clause.
Example 1
declare c1 cursor for select * from tablea for update of col1
delete tablea where current of c1
The cursor can be reused multiple times before it is deallocated.
Using a dynamic command, delete removes a row from a table.
delete [[database.]owner.]{table_name | view_name}
[where search_conditions]Any valid object in the catalog can be substituted for table_name or view_name.
is a standard where clause.
search_conditionsis a valid where clause component. It sets the conditions for the rows that are retrieved. A search condition can include column names, constants, joins, the keywords is null, is not null, or, like, and, or any combination of these items.
Example 1
delete from tablea where col1 = "test"
Following are relational operators that are supported in search conditions: =, <>, <, >, <=, >=, and like.
The prepared statement can execute multiple times before it is deallocated.
Using a language command, delete removes a row from a table.
delete [from] [[database.]owner.]{table_name|view_name}
[where search_conditions]delete [[database.]owner.]{table_name | view_name}
[from [[database.]owner.]{table_name | view_name}
[, [[database.]owner.]{table_name | view_name}]...]
[where search_conditions](after delete) is an optional keyword used for compatibility with other versions of SQL. Follow it with the name of the table or view from which you want to remove rows.
from(after table_name or view_name) allows you to name more than one table or view to use with a where clause when specifying the rows to delete. The from clause allows you to delete rows from one table based on data stored in other tables, giving you much of the power of an embedded select statement.
whereis a standard where clause.
search_conditionsis a valid where clause component. It sets the conditions for the rows that are retrieved. A search condition can include column names, constants, joins, the keywords is null, is not null, or, like, and, or any combination of these items.
Example 1
delete from authors where au_lname = "McBadden"
You cannot use delete with a multi-table view.
If you do not use a where clause, all rows are deleted from the table or view that is named after the delete [from] T-SQL keyword parameter.
Removes an index from a table in the current database.
drop index table_name.index_name
The preceding syntax works only in Sybase mode.
is the table in which the indexed column is located. The table must be in the current database.
index_nameis the name of the index to be dropped.
Example 1
drop index authors.au_id_ind
Not defined
Removes a table definition and all of its data, indexes, triggers, and authorization specifications from the database.
drop table [[database.]owner.]table_name
is the name of the table to be dropped.
Example 1
drop table authors
Not defined
Removes one or more views from the database.
drop view [databasename], [owner].view
is the name of the view to be dropped. The name must be a legal identifier and cannot include a database name.
Example 1
drop view new_price
Each time a view is referenced, another view or stored procedure checks the existence of the view.
Assigns authorizations to users.
To grant authorization to access database objects:
grant {all [privileges]| permission_list}
on {table_name [(column_list)]
| view_name[(column_list)]
| stored_procedure_name}
to {public | name_list}
[with grant option]To grant authorization to create database objects:
grant {all [privileges] | command_list}
to {public | name_list}Any valid object in the catalog can be substituted for table_name or view_name.
when used to assign authorization to access database objects (first syntax format), all specifies that all privileges applicable to the specified object are granted or revoked.
permission_listis a list of authorizations granted.
command_listis a list of commands granted.
table_nameis the name of a table in the database.
column_listis a list of columns, separated by commas, to which the privileges apply.
view_nameis the name of a view in the current database. Only one view can be listed for each grant command.
stored_procedureis the name of a stored procedure in the database.
publicis all users of the "public" group, which includes all users of the system.
name_listis a list of users' database names or group names or both, separated by commas.
with grant optionallows the users specified in name_list to grant the privileges specified by permission_list to other users.
Example 1
grant insert, delete on titles to mary, sales
Example 2
grant update on titles (price, advance) to public
Example 3
grant create database, create table to mary, john
Example 4
grant all on titles to public
Example 5
grant all to public
Example 6
grant update on authors to mary with grant option
Example 7
grant select, update on titles(price) to bob with grant option
You can substitute the word from for to in the grant syntax.
You can only grant or revoke authorizations on objects in the current database.
role_name, which allows you to grant authorizations to all users who have been granted a specific role, is not supported. However, if you include it in the command, an error does not occur.
Using a dynamic command, insert adds a new row to a table or view.
insert [database.[owner.]]{table_name|view_name}
[(column_list)]
values (? [, ?]...) Any valid object in the catalog can be substituted for table_name or view_name.
is a list of one or more columns to which data is to be added. The columns can be in any order, but the incoming data (whether in a values clause or a select clause) is in the same order.
valuesis a keyword that introduces a list of expressions.
?specifies a parameter marker passed by the application.
Example 1
insert titles
(title_id, title, type, pub_id, notes, pubdate,
contract)
values (?, ?, ?, ?, ?, ?, ?)Using a language command, insert adds a new row to a table or view.
insert [into] database.[owner.]{table_name|view_name}
[(column_list)]
values (value1, [,value2]...) Any valid object in the catalog can be substituted for table_name or view_name.
is optional.
column_listis a list of one or more columns to which data is to be added. The columns can be in any order, but the incoming data (whether in a values clause or a select clause) is in the same order.
valuesis a keyword that introduces a list of expressions.
Example 1
insert titles (title_id, title, type, pub_id, notes, pubdate, contract) values (docid, docno, docdate)
Determines whether a server is still connected.(not supported)
prepare transaction
Example 1
prepare transaction
Revokes authorizations from users.
revoke [grant option for]
{all [privileges]| permission_list}on {table_name [(column_list)]
| view_name [(column_list)]
| stored_procedure_name}
from {public | name_list}
[cascade]revoke {all [privileges] | command_list}
from {public | name_list}(in the first syntax format) specifies that all privileges applicable to the specified object are revoked when used to revoke authorizations to access database objects. The second syntax format can revoke create command authorizations:
When the System Administrator uses this command, all create authorizations are revoked.
When the database owner uses this command, all create authorizations are revoked except create database.
is a list of authorizations to be revoked.
command_listis a list of commands for which authorizations are to be revoked.
table_nameis the name of the specified table in the database.
column_listis a list of columns, separated by commas, to which the privileges apply. If columns are specified, only select and update authorizations can be revoked.
view_nameis the name of a view in the current database. Only one view can be listed for each revoke statement.
stored_procedureis the name of a stored procedure in the database. Only one object can be listed for each revoke statement.
publicis all users of the "public" group, which includes all users of the system.
name_listis a list of users' database names and group names, separated by commas.
grant option forprohibits the users specified in name_list from granting the privileges specified by permission_list to other users.
cascaderevokes grant authorization for the privileges specified in permission_list from the users specified in name_list, and from all users to whom they granted privileges.
Example 1
revoke insert, delete on titles from mary, sales
Example 2
revoke all on titles from public
Authorizations can only be revoked on objects in the current database.
grant and revoke commands are order-sensitive. When a conflict occurs, the most recently issued command takes effect.
The word to can be substituted for the word from in the revoke syntax.
The access service does not support role_name.
Rolls back a user-specified transaction to the beginning of the transaction.
rollback {transaction | tran | work}
[transaction_name]is another term for transaction.
workis another term for transaction.
transaction_nameis the name assigned to the transaction. It must conform to the rules for identifiers.
Example 1
rollback transaction
In Sybase mode, transaction_name is stripped from the statement before it is passed to the target.
The access service converts rollback transaction rollback work, and rollback tran statements to a rollback statement.
Retrieves rows from database objects. You can issue this command either as language command or a CT-Library cursor request.
select select_list
[from [[database.]owner.]{table_name |view_name}
[,[[database.]owner.]{table_name|view_name}]...]
[where search_conditions] is one or more of the following items:
A list of column names in the order in which you want them returned
An aggregate function
Any combination of the items listed previously
indicates the particular tables and views to use in the select statement.
table_name, view_namelists tables and views used in the select statement. If more than one table or view is in the list, their names are separated by commas. Table names and view names are given correlating names. This is done by providing the table or view name, then a space, then the correlation name, such as:
select *
from publishers t1, authors t2search_conditionssets the conditions for the rows that are retrieved. A search condition can include column names, constants, joins, the keywords is null, is not null, or, like, and, or any combination of these items.
group byfinds a value for each group. These values appear as new columns in the results, rather than as new rows.
order bysorts the results by columns.
havingsets conditions for the group by clause, similar to the way that where sets conditions for the select clause. There is no limit on the number of conditions that can be included.
unionreturns a single result set that combines the results of two or more queries. Duplicate rows are eliminated from the result set unless the all keyword is specified.
read onlyindicates that the cursor is a read-only cursor and that updates cannot be applied to rows made current by it.
updateindicates that the cursor is an updatable cursor, and that the rows it makes current can be deleted or updated.
Example 1
select count(*) from publishers for read only
Example 2
select pub_id, pub_name, city, state from publishers for read only
Example 3
select pub_name, pub_id from publishers
Example 4
select type, price from titles where price > @p1 for update of price
Example 5
select stor_id, stor_name from sales union select stor_id, stor_name from sales_east
The TEXTPTR() function cannot appear in the select list.
All SQL Server 10.x aggregate functions are supported:
sum ( [all | distinct] )
avg ( [all | distinct ] )
count ( [all | distinct] )
count (*)
max (expression)
min (expression)
The access service does not transform correlation names.
You can issue the select command either as a language command or a client-based cursor request.
If a cursor is passed a new set of parameters before it is opened, it can be reused multiple times.
If passed as a cursor command, the data values used in the where clause search conditions are passed as cursor parameters. These parameters use the datatype associated with the column.
Cursor parameters are indicated with an @ (at) symbol.
Refer to sp_capabilities for specific functions that the access service supports.
Removes all rows from a table.
truncate table [[database.]owner.]table_name
is the name of the table to be truncated.
Example 1
truncate table authors
The client application passes the truncate table command to the access service as a language command.
In Sybase mode, the access service converts truncate table to a delete command without a where clause.
Changes data in a row made current by a read cursor command, either by adding data or by modifying existing data.
update [[database.]owner.]{table_name | view_name}
set column_name1 = @p1]
[, column_name2 = @p2]...Any valid object in the catalog can be substituted for table_name or view_name.
specifies the column name and assigns the new value. The value passes as a cursor parameter.
Example 1
update authors set au_lname = @p1
The row made current by the cursor authors_cursor is modified, and the column au_lname is set to the value of the parameter @p1.
The cursor can be reused multiple times before it is deallocated.
You can include a from clause in the update statement, but because the AS/400 does not support it, the access service will ignore it.
Using a dynamic command, update changes data in existing rows of the referenced table.
update [[database.]owner.]{table_name | view_name}
set column_name1 = ?
[, column_name2 = ?]...[ where search_conditions...] ]specifies the column name and assigns the new value. The value passes as a parameter.
whereis a standard where clause.
search_conditionssets the conditions for the rows that are retrieved. A search condition can include column names, constants, joins, the keywords is null, is not null, or, like, and, or any combination of these items.
Example 1
update authors set au_lname = ? where au_id = ?
The au_lname column is set to the value of <parameter 1> (indicated by a "?"), where the value of au_id is equal to the value of <parameter 2> (indicated by the second "?").
You can include a from clause in the update statement, but because the AS/400 does not support it, the access service will ignore it.
You can substitute table_name and view_name with any valid object in the catalog.
Following are relational operators that are supported in search conditions: =, <>, <, >, <=, >=, and like.
Changes data in existing rows, either by adding data or by modifying existing data. Use this as a language command.
update [[database.]owner.]{table_name | view_name}
set [[[database.]owner.]{table_name.|view_name.}] column_name1 =
{expression1|NULL|(select_statement)}
[column_name2 =
{expression2|NULL|(select_statement)}]...
[,[[database.]owner.]{table_name|view_name}]...]
[where search_conditions]specifies the column name and assigns the new value. The value can be an expression o r a null. When more than one column name and value pair are listed, they must be separated by commas.
whereis a standard where clause.
search_conditionssets the conditions for the rows that are retrieved. A search condition can include column names, constants, joins, the keywords is null, is not null, or, like, and, or any combination of these items.
Example 1
update authors set au_lname = "MacBadden" where au_lname = "McBadden"
You can include a from clause in the update statement, but because the AS/400 does not support it, the access service ignores it.
You cannot update views defined with the distinct clause. However, when the view is created, the select language command allows the term distinct to be used with it. For more information, see the Sybase SQL Server Reference Manual.
Accesses an existing database.
use database_name
is the name of the database you want to access.
Example 1
use authors
The access service returns the database name as part of the result set in an sp_sqlgetinfo call. It is located in the SQL_DATABASE_NAME attribute.
If you issue a use database_name command with more than 127 characters, DirectConnect stores only the first 127 characters that you enter on this command.
|
|