Você está na página 1de 10

Sql Tips

Contributed By
Version Number

Anbu Cheliyan
1

Creation Date

28/05/2013

Page 1 of 10

Table of Contents

1 List all the tables from schema EWS_V5I1_DEV.................................................................................2


2 How to view definition of Stored procedures.........................................................................................2
3 Get the column details of table............................................................................................................. 2
4 Create table as Select.......................................................................................................................... 3
5 Ascii function in Oracle and DB2.......................................................................................................... 3
6 Alter table issue.................................................................................................................................... 4
7 Alter table issue.................................................................................................................................... 5
8 Oracle version and NLS info.................................................................................................................5
9 DB2 commands.................................................................................................................................... 6
9.1 Connecting to DB2 from unix..................................................................................6
9.2 List all tables from a Schema..................................................................................6
9.3 Get DDL of a table..................................................................................................6
10 Create a table using LIKE...................................................................................................................6
11 Function to recreate Temp table if it is locked due to tablespace issue..............................................6
12 View data from DB2 table in Unix.......................................................................................................7
13 Assignment of a NULL value to a NOT NULL column........................................................................7
14 Insert Multiple Rows with a Single INSERT Statement.......................................................................8
15 Copy QA data to DEV......................................................................................................................... 8
16 Inserting record in Db2 with single quote in data................................................................................8
17 DB2 directory...................................................................................................................................... 9
18 Oracle - Converting Exponential to number......................................................................................10

List all the tables from schema EWS_V5I1_DEV


Oracle
SELECT TABLE_NAME FROM ALL_TABLES WHERE OWNER = 'EWS_V5I1_DEV'
DB2
SELECT NAME FROM SYSIBM.SYSTABLES
WHERE CREATOR = 'EWS_V5I1_DEV'
AND TYPE = 'T'

2 How to view definition of Stored procedures


select PROCNAME,TEXT from SYSIBM.SYSPROCEDURES

3 Get the column details of table


SELECT * FROM SYSCAT.COLUMNS
WHERE TABNAME = 'TABLE' AND TABSCHEMA = 'SCHEMA'
OR
SELECT DISTINCT(NAME), COLTYPE, LENGTH FROM SYSIBM.SYSCOLUMNS
WHERE TBNAME = 'TABLE' AND TBCREATOR = 'SCHEMA'

Page 2 of 10

4 Create table as Select


Oracle
1. Create new table from existing table with data
CREATE TABLE MY_TABLE_NAME AS SELECT * FROM TABLE
2. Create new table from existing table without data
CREATE TABLE MY_TABLE_NAME AS SELECT * FROM TABLE WHERE 1=2
3. Create table syntax won't copy constraints, indices etc.
Use dbms_metadata.get_ddl to retrieve DDL and create the new
table.
SELECT dbms_metadata.get_ddl( 'TABLE', 'TABLE_NAME' ) FROM
DUAL;
DB2
1.Create new table from existing table without data
CREATE TABLE MY_TABLE_NAME AS (SELECT * FROM TABLE) WITH NO
DATA
2. Insert data into the newly created table
INSERT INTO MY_TABLE_NAME SELECT * FROM TABLE
3. Create new table from existing table without data using
Like statement
CREATE TABLE MY_TABLE_NAME LIKE TABLE

5 Ascii function in Oracle and DB2


Oracle
SELECT dump(COL_NM) FROM TABLE_NM
DB2
In DB2 ASCII function gives Ascii value of one character. To
find Ascii value of an entire string, we can use this unix
script to create ASCII() for each character.
==============================================================
=====
i=1
while [[ $i -le 32 ]] # 32 length of column COL_NM
do
echo "ASCII(SUBSTR(COL_NM,$i,1)) || ',' ||"
i=$(( i + 1 ))
done | tr -d '\n'
==============================================================
=====

Page 3 of 10

select ASCII(SUBSTR(COL_NM,1,1)) || ',' ||


ASCII(SUBSTR(COL_NM,2,1)) || ',' ||ASCII(SUBSTR(COL_NM,3,1))
|| ',' ||ASCII(SUBSTR(COL_NM,4,1)) || ','
||ASCII(SUBSTR(COL_NM,5,1)) || ',' ||ASCII(SUBSTR(COL_NM,6,1))
|| ',' ||ASCII(SUBSTR(COL_NM,7,1)) || ',' ||
ASCII(SUBSTR(COL_NM,8,1)) || ',' ||
ASCII(SUBSTR(COL_NM,9,1)) || ',' ||ASCII(SUBSTR(COL_NM,10,1))
|| ',' ||ASCII(SUBSTR(COL_NM,11,1)) || ',' ||
ASCII(SUBSTR(COL_NM,12,1)) || ',' ||
ASCII(SUBSTR(COL_NM,13,1)) || ',' ||ASCII(SUBSTR(COL_NM,14,1))
|| ',' ||ASCII(SUBSTR(COL_NM,15,1)) || ',' ||
ASCII(SUBSTR(COL_NM,16,1)) || ',' ||
ASCII(SUBSTR(COL_NM,17,1)) || ',' ||ASCII(SUBSTR(COL_NM,18,1))
|| ',' ||ASCII(SUBSTR(COL_NM,19,1)) || ',' ||
ASCII(SUBSTR(COL_NM,20,1)) || ',' ||
ASCII(SUBSTR(COL_NM,21,1)) || ',' ||ASCII(SUBSTR(COL_NM,22,1))
|| ',' ||ASCII(SUBSTR(COL_NM,23,1)) || ',' ||
ASCII(SUBSTR(COL_NM,24,1)) || ',' ||
ASCII(SUBSTR(COL_NM,25,1)) || ',' ||ASCII(SUBSTR(COL_NM,26,1))
|| ',' ||ASCII(SUBSTR(COL_NM,27,1)) || ',' ||
ASCII(SUBSTR(COL_NM,28,1)) || ',' ||
ASCII(SUBSTR(COL_NM,29,1)) || ',' ||ASCII(SUBSTR(COL_NM,30,1))
|| ',' ||ASCII(SUBSTR(COL_NM,31,1)) || ',' ||
ASCII(SUBSTR(COL_NM,32,1))
from TABLE_NM

6 Alter table issue


ALTER TABLE TABLE_NM DROP COLUMN COL_NM
When I tried to insert into TABLE_NM, after altering the table, I
received this error message.
SQLSTATE = 57016: Native Error Code = -668: Msg = [IBM][CLI
Driver][DB2/LINUXX8664] SQL0668N Operation not allowed for
reason code "7" on table "TABLE_NM". SQLSTATE=57016
Reason code 7 means - Reorganize the table using the REORG TABLE
command (note that INPLACE REORG TABLE is not allowed for a table
that is in the reorg pending state).
If you try to Reorg the table from IBM Data Studio, you will get
this error.
REORG TABLE SCHEMA_NM.TABLE_NM
An unexpected token "SCHEMA_NM" was found following "REORG TABLE
". Expected tokens may include: "JOIN".. SQLCODE=-104,
SQLSTATE=42601, DRIVER=4.13.111
Run the Reorg command from commandline

Page 4 of 10

db2 "connect to <Database> user <UserName> using <Password>"


db2 REORG TABLE TABLE_NM

7 Alter table issue


- Alter table failed with the below message
$ db2 "ALTER TABLE TABLE_NM ALTER COLUMN COL_NM SET DATA
TYPE VARCHAR(40)"
DB21034E The command was processed as an SQL statement
because it was not a
valid Command Line Processor command. During SQL
processing it returned:
SQL0270N Function not supported (Reason code = "21").
SQLSTATE=42997
Reason code 21 means
A column cannot be dropped or have its length, data
type, or nullability altered on a table which is a base
table for a materialized query table.
- To find the materialized query table using your base table,
use this query
SELECT TABSCHEMA, TABNAME
FROM SYSCAT.TABDEP
WHERE BTYPE='T'
AND BSCHEMA='<schema of your base table>'
AND BNAME='<table name>'
- Take materialized query table name from above query and drop
it using
drop table <TABSCHEMA>.<TABNAME>
- Run your alter sql again and recreate materialized table
http://webdocs.caspur.it/ibm/db2/8.1/doc/htmlcd/en_US/admin/r
0001067.htm

8 Oracle version and NLS info


select * from v$version -- Returns the version of the Oracle
database.
select * from nls_database_parameters -- Returns NLS information for
Oracle database
$ORACLE_HOME/bin/sqlplus version -- Returns the version of the
client

Page 5 of 10

9 DB2 commands
9.1 Connecting to DB2 from unix
db2 connect to <DB_Name> user <User_Name> using <Password>

9.2 List all tables from a Schema


db2 list tables for schema <Schema_name>

9.3 Get DDL of a table


db2look -d <DB_Name> -i <User_Name> -w <Password> -z
<Schema_Name> -t <Table_Name> -e xd

10 Create a table using LIKE


$ db2 connect to <DB_Name> user <User_Name> using <Password>
Database Connection Information
Database server
SQL authorization ID
Local database alias

= DB2/LINUXX8664 9.7.4
= ETLUSER
= DEVDB

$ db2 "CREATE TABLE SCHEMA_NM.TABLE_NM_NEW LIKE ADW.TABLE_NM"


DB20000I The SQL command completed successfully.
Note
Following options are not copied to new table:

Check constraints
Column default values
Column comments
Foreign keys
Logged and compact option on BLOB columns
Distinct types

https://publib.boulder.ibm.com/infocenter/iseries/v5r4/index.jsp?topic=
%2Fsqlp%2Frbafycrttbllike.htm

11 Function to recreate Temp table if it is locked due to


tablespace issue
function recreate {
dt=$(date +%Y%m%d%H%M%S)
db2 "create table $1_$dt like $1"
db2 "drop table $1"
db2 "create table $1 like $1_$dt"

Page 6 of 10

db2 "drop table $1_$dt"


}
Usage: recreate <Schema_Name>.<Table_Name>
$ recreate Schema_nm.Table_nm
DB20000I The SQL command completed
DB20000I The SQL command completed
DB20000I The SQL command completed
DB20000I The SQL command completed
DB20000I The SQL command completed

successfully.
successfully.
successfully.
successfully.
successfully.

12 View data from DB2 table in Unix


$ db2 "select * from Schema.Table" | tr -s ' ' ','

COL1,COL2,COL3,COL4,COL5,COL6,COL7,COL8,COL9,COL10,COL11,COL12
,COL13,COL14,COL15,COL16,COL17,COL18,COL1
--------------------------------------------------,--------------,------,------------,---------------------------------,----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------,-------------------------------------------------,---,--------------------,----------,------------------------,------------------,------------------------,----------------,---------------------,---------------------,---------------,--------------,-------------------100000033177673,1,E,645802771500327026860461290628.,01/01/0001,30/12/9999,1515
,1515,1595499,1595499,19/04/2013,19/04/2013,18/04/2013,
100000033177673,COM01,1,E,645802771500327026860461290628.,01/0
1/0001,30/12/9999,1519,1519,1596679,1596679,23/04/2013,23/04/2
013,22/04/2013,
Note: Varchar fields having spaces in it will be removed from
the output. Column Set_Id is blank in first row in above
output and -1 is value of third column Org_Id.

13 Assignment of a NULL value to a NOT NULL column


Assignment of a NULL value to a NOT NULL column "TBSPACEID=-6,
TABLEID=-32599, COLNO=7" is not allowed. SQLSTATE=23502
$ db2 "select TABSCHEMA,TABNAME from syscat.tables where
TBSPACEID=-6 and TABLEID=-32599"

Page 7 of 10

TABSCHEMA
TABNAME
----------------------------------------ADW
ACCOUNT_MSISDN_HISTORY
1 record(s) selected.
$ db2 "select COLNAME from syscat.columns where COLNO=7 and
TABSCHEMA='ADW' and TABNAME='ACCOUNT_MSISDN_HISTORY'"
COLNAME
-----------------------------------------------------------------------------------------------------------------------------SERVICE_STATUS_ID
1 record(s) selected.

14 Insert Multiple Rows with a Single INSERT Statement


Oracle
Insert all
into table_name (col1,col2) values ('a',1)
into table_name (col1,col2) values ('b',2)
select * from dual;
DB2
INSERT INTO table_name VALUES
('a',1)
,('b',2);

15 Copy QA data to DEV


Connect to QA database Qadb and export the data to a file
db2 connect to Qadb user usernm using pwd
db2 "export to File_name of del select * from
schemaname.tablename with ur"
Connect to DEV database Devdb and import the data from
file to table
db2 connect to devdb user usernm using pwd
db2 "import from File_name of del insert into
schemaname.tablename"

16 Inserting record in Db2 with single quote in data


INSERT INTO TABLE (COLUMN) VALUES ('''T''')
Note: ''' above is three single quotes in a row

Page 8 of 10

17 DB2 directory
Database and server information is stored in the DB2
directories.
To list system database directory use the command db2
list database directory
$ db2 list database directory
System Database Directory
Number of entries in the directory = 5
Database 1 entry:
Database alias
Database name
Node name
Database release level
Comment
Directory entry type
Catalog database partition number
Alternate server hostname
Alternate server port number

=
=
=
=
=
=
=
=
=

UAT
UDB
UATNODE
c.00

=
=
=
=
=
=
=
=
=

DEVDB
UDB
DEVNODE
c.00

Remote
-1

Database 2 entry:
Database alias
Database name
Node name
Database release level
Comment
Directory entry type
Catalog database partition number
Alternate server hostname
Alternate server port number

Remote
-1

To list node directory use db2 list node directory


$ db2 list node directory
Node Directory

Page 9 of 10

Number of entries in the directory = 5


Node 1 entry:
Node name
Comment
Directory entry type
Protocol
Hostname
Service name

=
=
=
=
=
=

DEVINT

=
=
=
=
=
=

DEVNODE

LOCAL
TCPIP
100.100.100.86
50000

Node 2 entry:
Node name
Comment
Directory entry type
Protocol
Hostname
Service name

LOCAL
TCPIP
100.100.100.85
50000

18 Oracle - Converting Exponential to number


1. Converting Exponential to number using TO_CHAR
SELECT TO_CHAR(1.23365465451346E21) FROM DUAL;
TO_CHAR(1233654654513460000000
---------------------------------------1233654654513460000000
1 row selected
2. If it's a varchar column use TO_NUMBER
SELECT TO_NUMBER('1.23365465451346E21') FROM DUAL;
3. Use NUMWIDTH to display exponential in number
SQL> set numwidth 40
SQL> with t as (
select 1.23365465451346E21 n from dual
)
--select * from t
/
N
---------------------------------------1233654654513460000000
1 row selected.

Page 10 of 10

Você também pode gostar