I want to use a variable as the file name in Load data Infile. I run a below code:
Set #d1 = 'C:/Users/name/Desktop/MySQL/1/';
Set #d2 = concat( #d1, '20130114.txt');
load data local infile #d2 into table Avaya_test (Agent_Name, Login_ID,ACD_Time);
Unfortunately after running, there is a error with the commment like below:
"Error Code: 1064. You have an error in your SQL syntax ......"
Variable "#D2" is underlined in this code so it means that this error is caused by this variable.
Can you help me how to define correctly a variable of file name in LOAD DATA #variable infile ?
Thank you.
A citation from MySQL documentation:
The file name must be given as a literal string. On Windows, specify backslashes in path names as forward slashes or doubled backslashes. The character_set_filesystem system variable controls the interpretation of the file name.
That means that it can not be a parameter of a prepared statement, stored procedure, or anything "server-side". The string/path evaluation must be done client side.
In general, it is not possible directly but you can use a temporary file.
The procedure is similar to using a combination PREPARE-EXECUTE. Notice that you cannot use PREPARE with LOAD DATA INFILE, for this reason you require a temporary file.
Here's an example of how to read a file with today's date:
SET #sys_date = CURRENT_DATE();
SET #trg_file = CONCAT("LOAD DATA INFILE '/home/data/file-",#sys_date, "' INTO TABLE new_data FIELDS TERMINATED BY ' ';");
SELECT #trg_file INTO OUTFILE '/tmp/tmp_script.sql';
SOURCE /tmp/tmp_script.sql;
WARNING: You cannot overwrite files with mysql, for this reason the temporary file must not exist. It is a serious problem if you want to automatize the previous example.
Unfortunately, this does not seem to be possible in mysql.
In addition to Binary Alchemist's answer, it's also not possible with prepaired statements either, as it is not on this list: SQL Syntax Allowed in Prepared Statements
You could use soemthing external to generate your load data infile statement, and then run the sql. For instance you could create it in Excel.
This sysntax can't work since the variables are interpreted by the server, and the file is read by the mysql client.
Therefore for the client #d2 is an illegal file name.
My solution works on Linux/Mac/[Windows with bash] (e.g. cygwin)
Create a template with the load SQL, e.g. load.tpl -- note the %FILENAME% placeholder
LOAD DATA LOCAL INFILE '/path/to/data/%FILENAME%'
INTO TABLE Avaya_test ( Agent_Name, Login_ID, ACD_Time );
Then run this command:
for n in `ls *.txt`; do echo $n; sed -e 's/%FILENAME%/'$n'/g' load.tpl | mysql -u databaseUser databaseName; done
Here's my SQL to bulk load a CSV file into SQL Server 2008, but its returning:
0 row(s) affected.
Code:
USE energyDB
GO
BULK INSERT energydata
FROM 'c:\temp\24544_MSSQL_out.csv'
WITH
(
FIRSTROW = 2,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
The CSV file looks like this (the top line is ignored)
24544,"1970-01-01 10:00:00","8056060 kWh"
24544,"2012-12-04 00:15:00",0.176
24544,"2012-12-04 00:30:00",0.163
24544,"2012-12-04 00:45:00",0.016
Bulk insert doesn't remove quotes from the data, you'll either need to change the file being imported or import to a table where every column is a character field and strip the quotes and convert datatypes in a query. I've just found the following MSDN article:
http://msdn.microsoft.com/en-us/library/ms188609.aspx
It states:
Comma-separated value (CSV) files are not supported by SQL Server bulk-import operations.
And then continues on with a few examples of the situations it will work under.
This question is a little old, but it showed up when I searched for the problem, so I thought I would provide my solution.
In my case it was a simple mistake of not inserting into the right table, so be sure to check the table that you are inserting into. In trying to figure out what was going on though, I found that you can have the bulk insert process create an error file that will hopefully guide you in the right direction. To do this, you can use something like ERRORFILE ='E:\Error.txt'. This should output the error you are receiving to a file called Error.txt. I've provided a full example below:
BEGIN TRANSACTION
BEGIN TRY
BULK INSERT [Table Name]
FROM 'E:\FileName.csv'
WITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '\n', ERRORFILE ='E:\Error.txt')
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
END CATCH
You'll notice that I've wrapped the bulk insert process in a Transaction. This is so that if any issue occurs during the bulk insert it will rollback everything and I won't get a partial data import.
Hey guys im trying to import schema from mysql vesion 5.0 to 5.1. Every time i'm trying to import schema I got this error:
Error near Drop Proceduer service_tt_yes'
MySql.Data.MySqlClient.MySqlException: Column count of mysql.proc is wrong. Expected 20, found 16. The table is probably corrupted.
I try to skip that procedure but error occur in next procedure.
Any solution!
You can probably use following command in MySQL Client
USE DATABASE_NAME; SOURCE [QUALIFIED-LOCATION]IMPORTED_SCHEMA.sql
Like if you have IMPORTED_SCHEMA.sql in D: drive then fire following command. Note there is no semi colon in the end
SOURCE D:\IMPORTED_SCHEMA.sql
While importing data i am getting error corresponding view not available in the db. But i checked in the DB that view is available . I tried manually insert into view that time its working.
Any setting to change for this?
While data importing from table to view via SSIS . I 'm getting following error "Invalid object name [Feed].[VwPharmaClaim]." This already exists. I traced the query in the profiler Quotes surrounded with the object name as ("[Feed]"."[VwPharmaClaim]").Without the quotes its working.
While I'm inserting data in 2008 version I am getting following error
Msg 208, Level 16, State 1, Line 1
Invalid object name '[Feed].[VwPharmaClaim]'
I traced the insert query in profiler
exec sp_executesql N'INSERT INTO "[Feed]"."[VwPharmaClaim]" ("PharmaKey","member_id","MemberRefID","claim_id","ndc","days_supply","refill_number","fill_dt","paid_dt","prescriber_id","allowed_amt","paid_amt","Member_amt","quantity","Import_Id","age") VALUES (#P1,#P2,#P3,#P4,#P5,#P6,#P7,#P8,#P9,#P10,#P11,#P12,#P13,#P14,#P15,#P16)',N'#P1 bigint,#P2 varchar(8000),#P3 numeric(18),#P4 varchar(8000),#P5 varchar(8000),#P6 numeric(10),#P7 int,#P8 datetime,#P9 datetime,#P10 varchar(8000),#P11 money,#P12 money,#P13 money,#P14 float,#P15 numeric(18),#P16 int',1,'000000000052',52,'085565501321 ','00472030115',5,0,'2008-06-04 00:00:00','2008-06-08 00:00:00','BO5586396 ',$10.0000,$0.0000,$10.0000,15,7,5368
OUTPUT
Msg 208, Level 16, State 1, Line 1
Invalid object name '[Feed].[VwPharmaClaim]'.
When I remove the double quotes it’s working fine
exec sp_executesql N'INSERT INTO [Feed].[VwPharmaClaim] ("PharmaKey","member_id","MemberRefID","claim_id","ndc","days_supply","refill_number","fill_dt","paid_dt","prescriber_id","allowed_amt","paid_amt","Member_amt","quantity","Import_Id","age") VALUES (#P1,#P2,#P3,#P4,#P5,#P6,#P7,#P8,#P9,#P10,#P11,#P12,#P13,#P14,#P15,#P16)',N'#P1 bigint,#P2 varchar(8000),#P3 numeric(18),#P4 varchar(8000),#P5 varchar(8000),#P6 numeric(10),#P7 int,#P8 datetime,#P9 datetime,#P10 varchar(8000),#P11 money,#P12 money,#P13 money,#P14 float,#P15 numeric(18),#P16 int',1,'000000000052',52,'085565501321 ','00472030115',5,0,'2008-06-04 00:00:00','2008-06-08 00:00:00','BO5586396 ',$10.0000,$0.0000,$10.0000,15,7,5368
OUTPUT
(1 row(s) affected)
My package developed 2005 . I have converted this package to following version in 2008.
version 9.0.30729.4462.QFE
This My error while importing data from table to view
[OLE DB Destination [185]] Error: SSIS Error Code DTS_E_OLEDBERROR. An OLE DB error has occurred. Error code: 0x80004005.
An OLE DB record is available. Source: "Microsoft SQL Server Native Client 10.0" Hresult: 0x80040E37 Description: "Invalid object name '[Feed].[VwPharmaClaim]'.".
By default in SQL Server 2008 you are not allowed to use double quotes to identify database objects.
Run
ALTER DATABASE <dbname> SET QUOTED_IDENTIFIER = ON
against your destination database and then try running
SELECT * FROM "Feed"."VwPharmaClaim"
Check out http://technet.microsoft.com/en-US/library/ms174393(v=sql.105).aspx for some more info.
When you say SSIS is generating these queries, what sort of task are you using to generate them? I've never seen it produce double quote identifiers before, just curious.
I experienced this with SSIS 2013 and trying to insert to an updatable view. This view uses an 'instead of' trigger to do the insert.
In order for it to work, I had to go into the advanced editor for the destination and on the component properties tab remove the square brackets from the OpenRowset, so [dbo].[myView] becomes dbo.myView. Also, the AccessMode needs to be plain OpenRowset; fastload does not work.
On the Linux command line I call this script via the mysql terminal using source...
--First insert new template types
insert into TemplateType(id,name,size)
values (10,'Feature Sheet','8.5x11 ss');
insert into TemplateType(id,name,size)
values (11,'Feature Sheet','8.5x11 ds');
insert into TemplateType(id,name,size)
values (12,'Feature Sheet','11x17');
When I do I get an error saying my sql syntax is invalid. Is this not the correct way to comment an sql file?
Apparently you need whitespace after the -- or use a # instead....
http://dev.mysql.com/doc/refman/5.0/en/comments.html