473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Generating and running SQL on the fly

I am trying to generate sql from sql. Like
select 'select count(*) from ' ||
tabschema ..............syscat.tables where.....;

is there a way to run this with a single statement?
I tried
with temp1(sql) as
(
select 'select count(*) from ' ||
tabschema ..............syscat.tables where.....;
)
select * from new table (call admin_cmd(select sql from temp1));

I know the above is a wishful thinking. But this will clear a major
headache I've been having in recent past...
Feb 14 '08 #1
5 1807
Arun Srinivasan wrote:
I am trying to generate sql from sql. Like
select 'select count(*) from ' ||
tabschema ..............syscat.tables where.....;

is there a way to run this with a single statement?
I tried
with temp1(sql) as
(
select 'select count(*) from ' ||
tabschema ..............syscat.tables where.....;
)
select * from new table (call admin_cmd(select sql from temp1));

I know the above is a wishful thinking. But this will clear a major
headache I've been having in recent past...
You need dynamic SQL for that. So you can create a compound statement that
executes your query, fetches the generated statements via a cursor and then
executes the values/statements fetched from the cursor.

--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
Feb 15 '08 #2
Arun, are you embedding the sql on an application or running it as a
script? If its a script, why don't you do this, in a shell script

1. Direct the output of your select 'select count(*) from ' ||
tabschema ..............syscat.tables where.....; to a file
2. after this execute the file

Another option you have is a small stored procedure that can prepare
dynamic sqls

HTH

Feb 15 '08 #3
On Feb 14, 8:33 pm, Roger <wondering...@gmail.comwrote:
Arun, are you embedding the sql on an application or running it as a
script? If its a script, why don't you do this, in a shell script

1. Direct the output of your select 'select count(*) from ' ||
tabschema ..............syscat.tables where.....; to a file
2. after this execute the file

Another option you have is a small stored procedure that can prepare
dynamic sqls

HTH
If you are a CLP, you can avoid temporary files with:

db2 -x "select 'select count(*) from '||tabschema.... from
syscat.tables where ..." | db2
The output from the select statement is piped to another db2 process.

Norm
Feb 15 '08 #4
Arun Srinivasan wrote:
I am trying to generate sql from sql. Like
select 'select count(*) from ' ||
tabschema ..............syscat.tables where.....;

is there a way to run this with a single statement?
I tried
with temp1(sql) as
(
select 'select count(*) from ' ||
tabschema ..............syscat.tables where.....;
)
select * from new table (call admin_cmd(select sql from temp1));

I know the above is a wishful thinking. But this will clear a major
headache I've been having in recent past...
I admit I have never tried that, but....
* Create a procedure that takes your string and performs an EXECUTE
IMMEDIATE (or PREPARE/EXECUTE)
* Wrap the procedure in a function
* invoke that function from SQL.

Cheers
Serge

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Feb 15 '08 #5
Ian
Norm wrote:
On Feb 14, 8:33 pm, Roger <wondering...@gmail.comwrote:
>Arun, are you embedding the sql on an application or running it as a
script? If its a script, why don't you do this, in a shell script

1. Direct the output of your select 'select count(*) from ' ||
tabschema ..............syscat.tables where.....; to a file
2. after this execute the file

Another option you have is a small stored procedure that can prepare
dynamic sqls

HTH

If you are a CLP, you can avoid temporary files with:

db2 -x "select 'select count(*) from '||tabschema.... from
syscat.tables where ..." | db2
The output from the select statement is piped to another db2 process.
This generally works, but be aware that it can be problematic because
both front-end 'db2' processes are hitting talking to the same back-end
db2bp process.

Ian Bjorhovde
Feb 15 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Tim Daneliuk | last post by:
I use a makefile to create distribution tarballs of freestanding Python programs and their documentation. I cannot seem to find the right command line option to just generate a pyc/pyo file from...
1
by: Mark | last post by:
Hi - apologies in advance if this is not the correct forum - tried searching loads, and still to find a workable solution. Generating a word document from ASP - no problem. Inserting page breaks...
4
by: Justin Lebar | last post by:
Sorry about the huge post, but I think this is the amount of information necessary for someone to help me with a good answer. I'm writing a statistical analysis program in ASP.net and MSSQL7 that...
2
by: Ryan | last post by:
We have an MIS system which has approx 100 reports. Each of these reports can take up to several minutes to run due to the complexity of the queries (hundreds of lines each in most cases). Each...
4
by: pauljwilliams | last post by:
Hi there, I'm looking for an algorithm that will generate combinations in the following way: Given a lower bound, and an upper bound, generate a set of size n containing all combinates of...
8
by: pillip | last post by:
I am trying to generate a number of waves. For square wave i am using a mod funcion in a loop: (i & 2)* amplitude , and running a circular buffer. For a triangular and saw tooth waves I am all out...
1
by: Chenna Sudarsan | last post by:
Hello all, I have a typical problem in deploying my ASP .NET application (code behind is VB .NET). I am generating a power point presentation (ppt file) in my application. Program is...
1
by: John Bailo | last post by:
I'm doing a profile on my SQL Server to see if I can eliminate any long running statements, and periodically, I see an entry for ".NET SqlClient Data Provider". The durations for it are very...
2
by: Simon Wittber | last post by:
I'm building a web application using sqlalchemy in my db layer. Some of the tables require single integer primary keys which might be exposed in some parts of the web interface. If users can...
1
by: ajk | last post by:
Hi When doing a .NET compiler what are the benefits of generating ILAsm directly compared to generating another higher level language like C# first and then using the C# compiler? ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.