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

Outfile using Prepared Statement

I am attempting to generate a outfile with a unique name using time and date along with a concat of the file name.
The prepare statement throws an syntax error. I have tried single quotes, double quotes and combinations of each to no success. What am I missing? Can the outfile even be dynamically named as I'd like?

-------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. SELECT @sync:=concat(CURDATE(),CURTIME());
  2.  
  3. SET @marcFile = CONCAT('c:/scripts/marcUpdate', @sync,'.txt');
  4.  
  5. PREPARE stmt_name FROM concat('SELECT * INTO OUTFILE ', @marcFile , ' FIELDS TERMINATED BY \',\' OPTIONALLY ENCLOSED BY \' " \' LINES TERMINATED BY \'\n\' FROM marcupdate');
  6.  
  7. EXECUTE stmt_name;
  8.  
  9. DEALLOCATE PREPARE stmt_name;
--------------------------------------------------------
Best regards
Aug 22 '07 #1
8 6322
pbmods
5,821 Expert 4TB
Heya, Harris. Welcome to TSDN!

What is the text of the error that you are getting?
Aug 22 '07 #2
pbmods:

I get a simple syntax error with no explantion except the line location.

harrisd
Aug 22 '07 #3
pbmods
5,821 Expert 4TB
Heya, Harris.

What's the line location? Go ahead and copy-paste the error message (that's what they're for, after all!).
Aug 22 '07 #4
When I run the six queries, I get the responses:
-------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. SELECT @sync:=concat(CURDATE(),CURTIME());
  2.  
  3. UPDATE marcUpdate
  4. SET ssn = replace(ssn,'-','');
  5.  
  6. SET @marcFile = CONCAT('c:/scripts/marcUpdate', @sync,'.txt');
  7.  
  8. PREPARE stmt_name FROM concat ('SELECT * INTO OUTFILE ', @marcFile , ' FIELDS TERMINATED BY \',\' OPTIONALLY ENCLOSED BY \'"\' LINES TERMINATED BY \'\n\' FROM marcupdate');
  9.  
  10. EXECUTE stmt_name;
  11.  
  12. DEALLOCATE PREPARE stmt_name;
----------------------------------------------------------------
(1 row(s) returned)
(0 ms taken)

(0 row(s) affected)
(0 ms taken)

(0 row(s) affected)
(0 ms taken)

Error Code : 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'concat ('SELECT * INTO OUTFILE ', @marcFile , ' FIELDS TERMINATED BY \',\' OPTIO' at line 1
(0 ms taken)

Error Code : 1243
Unknown prepared statement handler (stmt_name) given to EXECUTE
(0 ms taken)

Error Code : 1243
Unknown prepared statement handler (stmt_name) given to DEALLOCATE PREPARE
(0 ms taken)

Obviously the last two fail because the prepared statement is not valid. The error shows from line 1 saying that it is having trouble with the @sync variable. However I have checked the creation of the variable and it is working, as is the @marcFile.(I actually do an update to a table with the variables, I did not include that part of the code). I am running mysql ver. 4.1.18

The frustrating part is this code works if I remove the prepared formatting and use a constant for the outfile. It will not however, overwrite the outfile. A updated file is criticial to the project, thus the dynamic naming requirement.

Best regards.
Aug 23 '07 #5
pbmods
5,821 Expert 4TB
Heya, Harris.

MySQL's PREPARE syntax requires a string literal, not an evaluated value. As a result, you can't use CONCAT() there. However, you can use a placeholder and replace it later.

Have a look at this document.
Aug 23 '07 #6
pbmods:

I'm still struggling with this issue. I'll take it back to the original problem.

This works!
Expand|Select|Wrap|Line Numbers
  1. SELECT * INTO OUTFILE 'c:/scripts/marcUpdate.txt' FIELDS TERMINATED BY ','OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' FROM marcupdate;
-----------------------------------------------------
This does not!
Expand|Select|Wrap|Line Numbers
  1. SET @marcFile = 'c:/scripts/marcUpdate.txt';
  2.  
  3. SELECT * INTO OUTFILE @marcFile FIELDS TERMINATED BY ','OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' FROM marcupdate;
-----------------------------------------------------
The SET works just fine, the SELECT can not read the @marcFile variable.
Is this actually possible with MySQL?

harrisd
Aug 27 '07 #7
pbmods
5,821 Expert 4TB
Heya, Harris.

Please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.

Have a look at this:
Expand|Select|Wrap|Line Numbers
  1. PREPARE stmt1 FROM 'SELECT SQRT(POW(?,2) + POW(?,2)) AS hypotenuse';
  2. SET @a = 3;
  3. SET @b = 4;
  4. EXECUTE stmt1 USING @a, @b;
  5.  
Aug 27 '07 #8
GW22
1
I was looking for exactly the same functionality. Have a look at this thread - it sorted it out for me!

http://forums.mysql.com/read.php?97,...2359#msg-62359

The code I ended up with is:
Expand|Select|Wrap|Line Numbers
  1. set @myvar = concat('select ?, ?, ?, ?, ?, ?, ?, ?, ?, ? INTO OUTFILE ',  "'",'my/path/',now() + 0,'_', bookingID,'_',Counter, '.email',"'",' FIELDS TERMINATED by \'\r\n\'');
  2.     PREPARE writeBookingFile FROM @myvar;
  3.     EXECUTE writeBookingFile USING @_mailTo, @_mailCC, @_mailBCC, @_mailSubject, @_mailFrom, @_mailFromName, @_mailReplyTo, @_mailPriority, @_mailtype, @_mailBody;
  4.     DEALLOCATE PREPARE writeBookingFile;
  5.  
Which gives me files such as:
/my/path/20070911190701_79108_1.email
/my/path/20070911190701_79108_2.email

Cheers,
Gus
Sep 11 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Yves Touze | last post by:
Hi All, I'm trying to migrate from SQL Server 7.0 to SQL Server 2000. I've got some ASP page which call VB components that retrieve shaped recordsets from SQL Server using the MSDATASHAPE...
2
by: solartimba | last post by:
Hi, I am in another country, and I need to finish a program. But I do not have any reference books. Can you tell me how to print this to a file without ending the stream with an endl. Stated...
2
by: Cyril VELTER | last post by:
I'm converting an application to use the V3 protocol features in the 7.4 libpq. As I need to make a design choice regarding the use of prepared statements, I'm wondering what ressources does a...
3
by: pmiller | last post by:
I ported my code from the development to application platform, I found a "type error" on a fileout statement: outfile.write(object.id +",") Object.id is provided by a library routine that is...
3
by: birju | last post by:
Hi, I'm running SQL Profiler on an SQL Server 2000 database. I see that one stored procedure gets repeatedly executed having a handle of '1'. This query takes a long time to complete. How do I...
2
by: varusnyc | last post by:
Hello, Im having really hard time writing an Employee Payroll program that uses functions to read data from file then send all data to another file. I managed to construct some pieces of the code,...
7
by: rhitz1218 | last post by:
All: I'm trying to create a log file. I now I can be able to do that by doing the following: FILE *outfile; outfile = fopen("log.txt", "w"); fprintf("This is the text");
1
by: kaushikm | last post by:
How to access sql server inbuilt functions from java prepared statement ? I have used getdate() of sql in a java prepared statement and am connecting to sqlserver. But it's not working. What to do ?
2
by: Pugi! | last post by:
It is by accident that I noticed that I forgot to use mysql_real_escape_string in part of my webapp. I tested input with following text : Hélène 51°56'12'' http://www.mysite.org/folder 3 functions...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.