This document discusses two scripting tricks for automating tasks in DB2. The first trick uses SQL to generate scripts with SQL statements, such as dropping triggers or granting permissions. The second trick uses looping in KSH to iterate through a list of objects and execute statements on each one, connecting and disconnecting from the database on each loop. Examples are provided for dropping triggers and granting permissions to illustrate each technique.
This document discusses two scripting tricks for automating tasks in DB2. The first trick uses SQL to generate scripts with SQL statements, such as dropping triggers or granting permissions. The second trick uses looping in KSH to iterate through a list of objects and execute statements on each one, connecting and disconnecting from the database on each loop. Examples are provided for dropping triggers and granting permissions to illustrate each technique.
This document discusses two scripting tricks for automating tasks in DB2. The first trick uses SQL to generate scripts with SQL statements, such as dropping triggers or granting permissions. The second trick uses looping in KSH to iterate through a list of objects and execute statements on each one, connecting and disconnecting from the database on each loop. Examples are provided for dropping triggers and granting permissions to illustrate each technique.
http://db2commerce.com/2012/02/06/basic-scripting-tricks-for-db2-lu/ 1/4 db2commerce.com Expert tips on building and administering DB2 LUWdatabases Menu Home About Guest Bloggers Privacy Policy
Basic scripting tricks for DB2(LUW)
Ember Crooks February 6, 2012 10 comments There are a couple of scripting tricks that I always teach when Imtraining a newdba. There are some areas where we have to iterate over something that db2 doesnt provide us with the tools to iterate. Writing statements with SQL I imagine most dbas have used this one before. Ive mostly used it on Windows where I cant use my ksh. Basically, you can write the SQL to write statements for you. The example Imgoing to use is dropping staging triggers something thats useful for those using WebSphere Commerce. Basically the idea is that I need to drop every trigger that starts with STAG%. DB2 obviously wont let me do drop trigger where name like STAG%, so I have to iterate over the list. Heres the general idea: > db2 -x "select 'drop trigger ' || trigname || ';' from syscat.triggers where trigname like 'STAG%' with ur" >trig_drops.sql > head trig_drops.sql drop trigger STAG0001; drop trigger STAG0002; drop trigger STAG0003; drop trigger STAG0007; drop trigger STAG0008; drop trigger STAG0009; drop trigger STAG0013; drop trigger STAG0014; drop trigger STAG0015; drop trigger STAG0016; Obviously, you can use this for much more detailed statements too, with more complicated SQL. For example, if you need to grant select permissions only on every table and viewin the database (dataaccess is great for select, update, insert, delete, but theres still no database level permission that Imaware of that does read only style access): > db2 -x "select 'grant select on table ' || rtrim(tabschema) || '.' || rtrim(tabname) || ' to group db2sel;' from syscat.tables where type in ('T','V') with ur" >tab_and_view_sel_grants.sql > head tab_and_view_sel_grants.sql grant select on table SYSIBM.SYSTABLES to group db2sel; grant select on table SYSIBM.SYSCOLUMNS to group db2sel; grant select on table SYSIBM.SYSINDEXES to group db2sel; grant select on table SYSIBM.SYSVIEWS to group db2sel; grant select on table SYSIBM.SYSVIEWDEP to group db2sel; grant select on table SYSIBM.SYSPLAN to group db2sel; grant select on table SYSIBM.SYSPLANDEP to group db2sel; grant select on table SYSIBM.SYSSECTION to group db2sel; grant select on table SYSIBM.SYSSTMT to group db2sel; grant select on table SYSIBM.SYSDBAUTH to group db2sel; The main gotchas on this are the typical scripting ones make sure youre putting spaces in the correct places within your single quotes, and use functions like rtrimto get rid of extra spaces. Of course the resulting files can then be executed with db2 -tvf Update on February 7, 2012: Thanks to a reader who commented, I can share with you a way of directly executing the queries rather than writing themto a file and then executing the file. His blog entry on it is here: http://angocadb2.blogspot.com/2011/12/ejecutar-la-salida-de-un-query-en-clp.html The key is the +p command line option. I had some inconsistent results it didnt seemto return for larger result sets, but maybe I just didnt wait long enough. Its definitely something Ill be playing with. Heres a sample syntax that could go with it: db2 -x "select 'select count(1) from ' || rtrim(tabschema) || '.' || rtrim(tabname) || ';' from syscat.tables where tabschema= 'DB2INST1' and tabname like '%ORD%'"| db2 +p -txv Looping in KSH (Linux/Unix only) This is actually my favorite method, and something I use at least once a week for those one-off tasks that I dont already have a script for. I do it all at the command line, but you can also pop it into a simple script. I say KSHbecause thats my favorite shell though I do this in BASHa fair amount, too. The idea here is to pop the list you want into a file, then iterate through it one itemat a time. Imgoing to use exactly the same examples as I did above first generating the statements to drop all triggers that start with STAG: > db2 -x "select trigname from syscat.triggers where trigname like 'STAG%' with ur" >trig.list > head trig.list STAG0001 STAG0002 STAG0003 STAG0007 STAG0008 STAG0009 STAG0013 STAG0014 STAG0015 STAG0016 > cat trig.list |while read t; do db2 connect to dbname; db2 -v "drop trigger schema.$t"; db2 connect reset; done >trig_drops.out > head -40 trig_drops.out Database Connection Information Database server = DB2/LINUXX8664 9.7.5 SQL authorization ID = DB2INST1 Local database alias = DBNAME drop trigger schema.STAG0001 DB20000I The SQL command completed successfully. DB20000I The SQL command completed successfully. Database Connection Information Database server = DB2/LINUXX8664 9.7.5 SQL authorization ID = DB2INST1 Local database alias = DBNAME drop trigger schema.STAG0002 DB20000I The SQL command completed successfully. DB20000I The SQL command completed successfully. Database Connection Information Database server = DB2/LINUXX8664 9.7.5 SQL authorization ID = DB2INST1 Local database alias = DBNAME drop trigger schema.STAG0003 DB20000I The SQL command completed successfully. DB20000I The SQL command completed successfully. Database Connection Information Database server = DB2/LINUXX8664 9.7.5 SQL authorization ID = DB2INST1 Local database alias = DBNAME drop trigger schema.STAG0007 DB20000I The SQL command completed successfully. DB20000I The SQL command completed successfully. Database Connection Information Database server = DB2/LINUXX8664 9.7.5 > And our second example granting select on all tables to a group: > db2 -x "select tabschema, tabname from syscat.tables where type in ('T','V') with ur" >tab.list > head tab.list SYSIBM SYSTABLES SYSIBM SYSCOLUMNS SYSIBM SYSINDEXES SYSIBM SYSVIEWS SYSIBM SYSVIEWDEP SYSIBM SYSPLAN SYSIBM SYSPLANDEP SYSIBM SYSSECTION SYSIBM SYSSTMT SYSIBM SYSDBAUTH > cat tab.list |while read s t; do db2 connect to dbname; db2 -v "grant select on $s.$t to group db2sel"; db2 connect reset; done >db2sel_grants.out > head -40 db2sel_grants.out Database Connection Information Database server = DB2/LINUXX8664 9.7.5 SQL authorization ID = DB2INST1 Local database alias = DBNAME grant select on SYSIBM.SYSTABLES to group db2sel DB20000I The SQL command completed successfully. DB20000I The SQL command completed successfully. Database Connection Information Database server = DB2/LINUXX8664 9.7.5 SQL authorization ID = DB2INST1 Local database alias = DBNAME grant select on SYSIBM.SYSCOLUMNS to group db2sel DB20000I The SQL command completed successfully. DB20000I The SQL command completed successfully. Database Connection Information Database server = DB2/LINUXX8664 9.7.5 SQL authorization ID = DB2INST1 A Cloud-Ready Platform microsoft.com/cloud-os Learn How Microsoft Will Lead the New Enterprise Cloud Era. 8/29/2014 Basic scripting tricks for DB2(LUW) http://db2commerce.com/2012/02/06/basic-scripting-tricks-for-db2-lu/ 2/4 SQL authorization ID = DB2INST1 Local database alias = DBNAME grant select on SYSIBM.SYSINDEXES to group db2sel DB20000I The SQL command completed successfully. DB20000I The SQL command completed successfully. Database Connection Information Database server = DB2/LINUXX8664 9.7.5 SQL authorization ID = DB2INST1 Local database alias = DBNAME > So the gotchas here are: 1. Your connect statement must be inside the while/do loop. You cannot connect before and then loop through the commands. I dont knowwhy, but this happens frequently with shell scripting. 2. You want to make sure to use the -v on the db2 command so that if you get a statement failure, you knowwhat statement failed. 3. It took me about a year before I could get that first semi-colon in the right place without copying and pasting, so dont feel bad if youre copying and pasting to get things right for a while. Update on February 7, 2012: Thanks to a reader who commented, I have a better way of doing this avoiding the connect in every loop. What happens is the pipe creates a newshell process which is not connected to the original shell process that has the connection. This makes complete sense to me, as Ive been playing with some parallelismin my Perl scripting lately. Heres some alternate syntax which avoids this issue and should be much faster. I tested it myself, before sharing it, of course. for TABNAME in `awk '{print $1"."$2}' tab.list`; do db2 -v "grant select on table $TABNAME to group db2sel" ; done >db2sel_grants.out Ill have a completely different post or series of posts on my favorite scripting language Perl. Any other nifty tricks anyone wants to share? Share this: Twitter 2 LinkedIn 1 Facebook 2 Google Reddit Email Related posts: 1. SQL0569 on Feature Pack or any other time, really 10 commentsadd one Frederik Engelen February 7, 2012, 06:54 The reason you must repeat the connect statement is because the pipe will create a newshell process which will not be connected to your original db2bp process. Try this, itll be a lot faster: for TABNAME in `awk {print $1.$2} tab.list`; do db2 -v grant select on table $TABNAME to group db2sel ; done in this case, you would do the concatenation in SQL of course, but awk works here as well for the time being Thx for the blog. Kind regards, Frederik Engelen Reply Ember Crooks February 7, 2012, 07:24 Awesome, thanks! Reply Ember Crooks February 29, 2012, 19:31 Well, thats not working for me. Same issue, it doesnt realize it has a connection: $db2 connect to WC005Q02 Database Connection Information Database server = DB2/AIX64 8.2.9 SQL authorization ID= DB2INST2 Local database alias = WC005Q02 $for TRIGNAME in `awk {print $1} trig.list`; do db2 -v drop trigger wscomusr.$TRIGNAME; done |tee trig_drops.out drop trigger wscomusr.STAG0001 DB21034E The command was processed as an SQL statement because it was not a valid Command Line Processor command. During SQL processing it returned: SQL1024N Adatabase connection does not exist. SQLSTATE=08003 Reply Andres Gomez Casanova February 7, 2012, 07:36 Hi, It looks very interesting your proposition to execute a set of commands in batch. However, you could eventually eliminate the temporary file, and you just execute the DB2 output in another DB2 environment. Here, I give an example: db2 -x select select count(1) from || rtrim(tabschema) || . || rtrim(tabname) || ; fromsyscat.tables where tabschema not like SYS% and type = T | db2 +p -tv (Taken frommy blog: http://angocadb2.blogspot.com/2011/12/ejecutar-la-salida-de-un-query-en-clp.html in Spanish) Reply Ember Crooks February 7, 2012, 08:29 Great option, and one I havent seen before. The only thing I would do if I were running it is to first run the query to make sure it gave the results I expected would hate to have a damaging syntax error get executed. Reply Andres Gomez Casanova July 20, 2012, 17:47 There is a limit when executing with -x, and it is the pipe limit. It cannot be changed. When you execute something with -x | +p -tv and it does not showthe results immediatly, it means that the buffer limit was reached. You have to kill the command (ctrl + c) and try a query with a smaller output. Reply Charles Brown February 8, 2012, 21:57 Hello All, Does anyone have a working example of a db2 shell script that reads multiple columns froma table via a cursor. Whatever Imtrying to do here is definitely not working for me. My host variables are not being populated. Their values are getting lost between shell processes. Somewhere out there I believe there is an example help me. Thanks and best regards, Iefbr14 Reply Carlos July 21, 2014, 01:46 Hi Ive got a questin for you. Is it possible to run a scipt sql in DB2 in a similar way that Oracle does? I cant fond a solution to my problem: For example: CREATE OR REPLACE VARIABLE myVar VARCHAR(50); SET myVar = perico; SELECT * FROMpippo WHERE nome = myVar; db2 -svtf prueba.sql SET myVar = perico
8/29/2014 Basic scripting tricks for DB2(LUW) http://db2commerce.com/2012/02/06/basic-scripting-tricks-for-db2-lu/ 3/4 DB21034E The command was processed as an SQL statement because it was not a valid Command Line Processor command. During SQL processing it returned: SQL0206N MYVAR is not valid in the context where it is used. SQLSTATE=42703 Can you help me, please? Thanks a lot Carlos Reply Ember Crooks July 22, 2014, 18:00 I have only encountered SQL PL like that within stored procedures or triggers. DB2 supports PL SQL since at least 9.7 doesnt mean Ive done it. Maybe check this out: http://www-01.ibm.com/support/knowledgecenter/SSEPGG_10.5.0/com.ibm.db2.luw.apdv.plsql.doc/doc/c0053607.html Reply Thank you! July 25, 2014, 01:46 Thank you very much Ember, so kind of you to help me out!! Reply Leave a Comment Name Email Website Submit Notify me of follow-up comments by email. Notify me of newposts by email. Previous Post: The early history of databases and DB2 Next Post: WebSphere Commerce Instance Creation Doesnt Like DB2 9.7 FixPack 5 To search, type and hit enterSearch 8/29/2014 Basic scripting tricks for DB2(LUW) http://db2commerce.com/2012/02/06/basic-scripting-tricks-for-db2-lu/ 4/4 To subscribe via email, enter your email address: Subscribe Recent Posts DB2 Basics: db2top Quick Hit Tips CPUSPEED, RESTRICTIVE, and DB2_WORKLOAD DB2 Error Logging Bad Message Queue Handler. Sit. Stay. When Index Scans Attack! DB2 Basics: Aliases DB2 Basics: Storage Groups Quick Tip: Simple Errors on Database Connection STMMAnalysis Tool Three Different Ways to Write the Same Join in SQL Top Posts &Pages Howto catalog a DB2 database Howto Find the Size of a DB2 Database Explain Part 2 - Command Line Explain Plans Using db2exfmt DB2 Database Restore and Rollforward: Details of Log Files DB2 Basics: Users, Authentication, and Authorization DB2 Basics: What is an Index? Howto Delete Data Froma DB2 Table and Release Disk Space DB2 Basics: db2top DB2 Errors: SQL0204N name is and undefined name Looking at HowMuch Memory DB2 is Using pureScale enhancements included in the new DB2 Cancun Release 10.5.0.4 ibm.co/VUY9y9 Retweeted by ember_crooks IBMDB2 @db2 Expand #DB2 FP4 is available for download: ibm.com/support/docvie Retweeted by ember_crooks Roland Schock @ARSDB2 Expand 50 DB2 Nuggets #49: Expert Advice - How does DB2 interact with TSAMP ow.ly/2MATeG IBMDB2 Support @db2_support 3h 3h 15h Tweet to @ember_crooks Blogs and Webcasts/Podcasts Dangerous DBA Dave Beulke's Blog DB2 Dean DB2 Night Show Podcasts on DB2 topics DB2 Portal Blog DB2utor (Z/OS) Find out what's happening with Information Management, DB2, InfoSphere, Warehouse The Whole Package Cache DB2 Education Resources Channel DB2 some good free education and videos DB2 Night Show Podcasts on DB2 topics DB2 Tech Talks IBMIMBootcamps and Education Free in-person courses! WebSphere Commerce Reference and Education CSE-WebSphere Commerce good WCS Blog Great IBMdocument on DBClean performance IBMWebSphere Commerce 7 Info Center Webcast on Commerce for the DB2 DBA Disclaimer The posts here represent my personal views and not those of my employer. Any technical advice or instructions are based on my own personal knowledge and experience, and should only be followed by an expert after a careful analysis or consultation with IBMsupport. Please test any actions before performing themin a critical or nonrecoverable environment. Any actions taken based on my experiences should be done with extreme caution. I amnot responsible for any adverse results. DB2 is a trademark of IBM. I amnot an employee or representative of IBM. Posts by Category Select Category