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?
------------------------------------------------------- - SELECT @sync:=concat(CURDATE(),CURTIME());
-
-
SET @marcFile = CONCAT('c:/scripts/marcUpdate', @sync,'.txt');
-
-
PREPARE stmt_name FROM concat('SELECT * INTO OUTFILE ', @marcFile , ' FIELDS TERMINATED BY \',\' OPTIONALLY ENCLOSED BY \' " \' LINES TERMINATED BY \'\n\' FROM marcupdate');
-
-
EXECUTE stmt_name;
-
-
DEALLOCATE PREPARE stmt_name;
--------------------------------------------------------
Best regards
8 6322
Heya, Harris. Welcome to TSDN!
What is the text of the error that you are getting?
pbmods:
I get a simple syntax error with no explantion except the line location.
harrisd
Heya, Harris.
What's the line location? Go ahead and copy-paste the error message (that's what they're for, after all!).
When I run the six queries, I get the responses:
------------------------------------------------------------------- - SELECT @sync:=concat(CURDATE(),CURTIME());
-
-
UPDATE marcUpdate
-
SET ssn = replace(ssn,'-','');
-
-
SET @marcFile = CONCAT('c:/scripts/marcUpdate', @sync,'.txt');
-
-
PREPARE stmt_name FROM concat ('SELECT * INTO OUTFILE ', @marcFile , ' FIELDS TERMINATED BY \',\' OPTIONALLY ENCLOSED BY \'"\' LINES TERMINATED BY \'\n\' FROM marcupdate');
-
-
EXECUTE stmt_name;
-
-
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.
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.
pbmods:
I'm still struggling with this issue. I'll take it back to the original problem.
This works! -
SELECT * INTO OUTFILE 'c:/scripts/marcUpdate.txt' FIELDS TERMINATED BY ','OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' FROM marcupdate;
-----------------------------------------------------
This does not! -
SET @marcFile = 'c:/scripts/marcUpdate.txt';
-
-
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
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: -
PREPARE stmt1 FROM 'SELECT SQRT(POW(?,2) + POW(?,2)) AS hypotenuse';
-
SET @a = 3;
-
SET @b = 4;
-
EXECUTE stmt1 USING @a, @b;
-
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: -
set @myvar = concat('select ?, ?, ?, ?, ?, ?, ?, ?, ?, ? INTO OUTFILE ', "'",'my/path/',now() + 0,'_', bookingID,'_',Counter, '.email',"'",' FIELDS TERMINATED by \'\r\n\'');
-
PREPARE writeBookingFile FROM @myvar;
-
EXECUTE writeBookingFile USING @_mailTo, @_mailCC, @_mailBCC, @_mailSubject, @_mailFrom, @_mailFromName, @_mailReplyTo, @_mailPriority, @_mailtype, @_mailBody;
-
DEALLOCATE PREPARE writeBookingFile;
-
Which gives me files such as:
/my/path/20070911190701_79108_1.email
/my/path/20070911190701_79108_2.email
Cheers,
Gus
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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");
|
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 ?
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |