473,396 Members | 1,938 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,396 software developers and data experts.

dynamic query question

Hello,
Hello,

I'm new in dynamic SQL query and I have a problem need help.

For example: I have a SQL query like this:

select hw.family,hw.id where hw.id="1234567";

and I have a txt file holding more than ten thousands id.
I need feed the ids from the file to the SQL query, inside
the "???", please let me know how can I do this?

then I put the SQL query in a file and run it from another .csh
file. Inside the .csh file, I have something like this:

mysql -u usrname dataname< /directory/sqlfile
/directory/outputfile/id_???


how can I have the output files created with different names according
with the id inputs in the run time?

Thanks a lot.

Jul 23 '05 #1
3 2261
le******@hotmail.com wrote:
For example: I have a SQL query like this:

select hw.family,hw.id where hw.id="1234567";

and I have a txt file holding more than ten thousands id.
I need feed the ids from the file to the SQL query, inside
the "???", please let me know how can I do this?
Probably the most efficient way to do this is to load your txt file into
a temporary table, and then perform a join between that table and your
hw table.

CREATE TEMPORARY TABLE tmp (id CHAR(20));
LOAD DATA INFILE 'idlist.txt' INTO TABLE tmp;

SELECT hw.family,hw.id
FROM hw JOIN tmp ON hw.id = tmp.id;

That's just an example. I don't know if it conforms to your text file
format, or if the CHAR is long enough, etc. Adjust to suit your needs,
and read the docs on LOAD DATA:
http://dev.mysql.com/doc/mysql/en/load-data.html

Another alternative is to loop through your txt file in your shell
script and execute SQL queries one by one, capturing the output of each
one to a separate output file. This executes the queries less
efficiently than the method above, but it's easier to separate the
output. See lower in this post for an example.

BTW, one should use sh instead of csh for shell programming. See
http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ for the classic
explanation.
then I put the SQL query in a file and run it from another .csh
file. Inside the .csh file, I have something like this:

mysql -u usrname dataname< /directory/sqlfile

/directory/outputfile/id_???

how can I have the output files created with different names according
with the id inputs in the run time?


Here's an example in sh (I think it's been over 15 years since I wrote
anything in csh -- gulp!):

cat idlist.txt | while read id
do
mysql -u username dataname -e "select hw.family, hw.id from hw
where hw.id = '$id'" > /directory/outputfile/id_$id
done

Regards,
Bill K.
Jul 23 '05 #2
On 19/05/2005, le******@hotmail.com wrote:
Inside the .csh file, I have something like this:

mysql -u usrname dataname< /directory/sqlfile
>/directory/outputfile/id_???


how can I have the output files created with different names according
with the id inputs in the run time?


The small Perl program below will do what you want.
Run it as: foo.pl file_with_ids.txt

#! /usr/bin/perl
# foo.pl - save query results to files based on id
use strict;
use warnings;
my $dp = "/path/to/destination/directory";
my $cmd = "/path/to/mysql/mysql -uuser -ppass db >$dp/id_%s";
my $qry = "select * from foo where id='%s'";

while (<>) {
chomp;
open CMD, sprintf("| $cmd", $_) or die "Cannot create pipe: $!\n";
print CMD sprintf($qry, $_);
close CMD;
}

--
felix
Jul 23 '05 #3
Cool, this works just great. Save me lots of time.
Thank you very much.

lei

Jul 23 '05 #4

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

Similar topics

1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
3
by: Peter Bailey | last post by:
Could someone please tell me how to pass criteria as if it were a parameter. I have a routine now that creates the sql string (well almost). at present the parameter is so I can pass one item ie...
15
by: Richard Hollenbeck | last post by:
I tried to ask this question before on the 14th of January but I never got a reply. I'm still struggling with the problem. I'll try to rephrase the question: I have a crosstab query with rows...
7
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always...
9
by: Jimbo | last post by:
Hello, I have a user request to build a form in an Access database where the user can check off specific fields to pull in a query. For example, let's say I have 10 fields in a table. The user...
8
by: Sandy Pittendrigh | last post by:
I have a how-to-do-it manual like site, related to fishing. I want to add a new interactive question/comment feature to each instructional page on the site. I want (registered) users to be able...
3
by: topmind | last post by:
I am generally new to dot.net, coming from "scriptish" web languages such as ColdFusion and Php. I have a few questions if you don't mind. First, how does one go about inserting dynamic SQL...
0
by: cfaheybestpitch | last post by:
Hi There, I have designed a DTS package which extracts a query into an excel file. It uses a query that changes dynamically based on user preferences, so I have used the dynamic property...
5
by: alingsjtu | last post by:
Hello, every body. When execute dynamic generated multiple OPENQUERY statements (which linkes to DB2) in SQLServer, I always got SQL1040N The maximum number of applications is already connected...
6
by: =?ISO-8859-1?Q?Tim_B=FCthe?= | last post by:
Hi, we are building a Java webapplication using JSF, running on websphere, querying a DB2 9 on Suse Enterprise 10. The app uses JDBC and PreparedStatements only (aka dynamic SQL). Every night,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.