MySQL存储过程

时间:2013-12-12 08:46:44

标签: mysql mysql-workbench

在使用sqlworkbench的MySQL中,如何为表创建一个过程以从文本文件中获取输入值(仅前100行)并将其存储在数据库中?

例如:在文本中,像(12,abc,heg,258)这样的值就像100行。

请帮助我。

我试过了

DELIMITER $$ 

drop procedure if exists `proctable` $$
create definer =`root`@`localhost` procedure `proctable`(in C:\Documents and Settings\Sridevi\tablevalue.txt varchar(200)) 
begin
load data local infile C:\Documents and Settings\Sridevi\tablevalue.txt 
into table test.testtbl
fields terminated by  '|' 
lines terminated by '\n' 
end $$ 

DELIMITER ;

1 个答案:

答案 0 :(得分:1)

drop procedure if exists `proctable`;
DELIMITER $$ 

create definer =`root`@`localhost` procedure `proctable`(/*remove that parameter*/) 
begin
/*the filename and the path to it have to be in quotes*/
load data local infile 'C:\Documents and Settings\Sridevi\tablevalue.txt' 
into table test.testtbl
fields terminated by  '|' 
lines terminated by '\n' 
(your_column_names_here, column2, column3, column4)
end $$ 

DELIMITER ;

创建过程时的参数名称不是文件的路径,它必须是变量名称,您可以在过程本身中引用它。这是一个例子,虽然我不确定load data local infile命令是否适用于变量。你必须亲自试试。

drop procedure if exists `proctable`;
DELIMITER $$ 

create definer =`root`@`localhost` procedure `proctable`(IN my_variable varchar(255)) 
begin
/*the filename and the path to it have to be in quotes*/
load data local infile my_variable
into table test.testtbl
fields terminated by  '|' 
lines terminated by '\n' 
(your_column_names_here, column2, column3, column4)
end $$ 

DELIMITER ;

然后您将使用

执行该功能
CALL proctable('C:\Documents and Settings\Sridevi\tablevalue.txt');