Home » Database » How to run commands/scripts in db2?

How to run commands/scripts in db2?

  • You can write multiple sql statements in the file and can call in db2 to run in script mode. These sql statements will then execute one by one.
  • You can call commands/scripts using interactive mode from Command Line Processor (CLP) also to do this make sure you type ! before db2. “!” is used to run opertating system commands in interactive mode. Example.
    • CLP1
  • Else you can directly call the commands/scripts using non-interactive mode.
  • If you are calling sql statements in unix which contains aestrix (*) please make sure you add double quotes to complete sql statement.
example : db2 "select * from employee"
  • If you are calling sql statements in windows which contains less than or greater than sign, >,< then also add double quotes to complete the sql statement.
example : db2 "select * from employee where salary > 20000"
  • You can run below script in both interactive and non-inetractive mode. Please notice I have used $ as line terminator character, you can use any special characters. Default line terminator is ;
-- script1.txt 
------------------------------------- 
connect to sample$
create table test_table(id int)$
insert into test_table values (1),(2),(3),(4)$
select * from test_table$
update test_table set id=20 where id > 2$
select * from test_table$
  • to run above script pass the line terminator character, in this it is $ so parmeter will be -td$, you can also do verbose mode with -v to see what is the script executing, and -f <abolute_path\file_name>. example
!db2 -td$ -v -f /sapcommon/script/script1.txt

 


Leave a comment

Your email address will not be published. Required fields are marked *

Categories

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

%d bloggers like this:
000webhost logo