I'm using MySQL ,
and trying to create stored procedure
that get CSV file (with 2 columns - ID & Name)
,delete table person_table and
create it with the CSV content.
This is the code:
DELIMITER $$
drop procedure if exists load_persons$$
CREATE PROCEDURE load_persons(in file_name varchar(300))
BEGIN
drop table if exists person_table;
CREATE TABLE `person_table` (
`ID` varchar(30) NOT NULL,
`Name` varchar(150) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
LOAD DATA LOCAL INFILE file_name INTO TABLE person_table;
END$$
DELIMITER ;
unfortunately I get an error : 'LOAD DATA is not allowed in stored procedures'
what can I do ?
Thank you!!
You can call to load command after stored procedure:
CALL load_persons(...);
-- and then
LOAD DATA LOCAL INFILE your_file_name INTO TABLE person_table;
Related
Here is the way I am creating my stored procedure:
SET #SQL =
(SELECT ddl_script FROM TABLENAME.versions where ID = 5)
;
USE TABLENAME;
DROP procedure IF EXISTS `ddl_stored_procedure`;
DELIMITER $$
CREATE PROCEDURE `ddl_stored_procedure` ()
BEGIN
PREPARE part1 FROM #SQL;
EXECUTE part1;
DEALLOCATE PREPARE part1;
END$$
DELIMITER ;
This part works fine and my stored procedure is created. Now I try to run my stored procedure with this line of code to test it:
CALL `DBNAME`.`ddl_stored_procedure`();
And I get the error message:
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 'USE `xxx_landing`; CREATE TABLE `TABLE1` ( `ROW1` varchar(1), `T' at line 1
The code that is running, is an SQL statement inside one of my databases as longtext and looks like this:
CREATE DATABASE `xxx_landing` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `xxx_landing`;
CREATE TABLE `TABLE1` (
`ROW1` varchar(1),
`ROW2` varchar(20),
`ROW3` varchar(3)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `TABLE2` (
`ROW4` varchar(2),
`ROW5` varchar(10),
`ROW6` varchar(10),
`ROW7` varchar(10),
`ROW8` varchar(10),
`ROW9` varchar(50)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8;
I do not know why the stored procedure fails to create the database, use it and create all tables. If I execute the SQL-Query manually there is no syntax error.
Can someone explain me what I am doing wrong?
Thanks in advance!
I have some millions records and I want to insert them as some batches to increase the speed.
I have a table as follows:
CREATE TABLE `source_names` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`source_name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `source_name_UNIQUE` (`source_name`)
) ENGINE=InnoDB AUTO_INCREMENT=39 DEFAULT CHARSET=latin1;
and in one step I want to add 500 source_name in source_names like this:
insert ignore into source_names (source_name)
values ('a01'),('a02'),...,('a500')
up to here there is no problem.
In Qt I have a QString which is like this (of course the IDE is not important in this question):
QString totalSourceNames="('a01'),('a02'),...,('a500')";
I want to insert this QString into source_names table by insertPro stored procedure and these commands:
query.prepare("CALL insertPro(:source_names)");
query.bindValue(":source_names",totalSourceNames);
query.exec()
and my stored procedure is:
CREATE DEFINER=`root`#`localhost` PROCEDURE `insertPro`(
IN source_names text
)
BEGIN
insert ignore into source_names(source_name) values (source_names);
END
By MySQL inserts just one row and concatenates all 500 values in one value. I know this is because of parenthesis in front of values (...) in above code but removing parenthesis makes an error in MySQL.
How can I insert 500 records with stored procedure?
I am trying to insert data to Second server's DB table after inserted to First server' table like below structure,
Second server :
use SecondServerDB;
CREATE TABLE user (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`age` smallint(6) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB;
After created table in Second sever, I have created table in First server like below,
First server :
use FirstServerDB;
CREATE TABLE remote_user2 (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`age` smallint(6) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=FEDERATED
DEFAULT CHARSET=latin1
CONNECTION='mysql://MysqlUserName:MysqlUserPassword#SecondServer.MyDomain.com:3306/SecondServerDB/user';
After created FEDERATED table in second server, Created triggers in same server like below,
DELIMITER $$
CREATE TRIGGER remote_insert_Testing AFTER INSERT ON remote_user2 FOR EACH ROW
BEGIN
INSERT INTO user (ID,name, age) VALUES (NEW.ID,NEW.name, NEW.Age);
END $$
DELIMITER ;
If i run insert query like below,
INSERT INTO remote_user2 (ID,name, age) VALUES (1,'Name',27);
Then it shows, Error Code: 1146. Table 'FirstServerDB.user' doesn't exist.
where am i doing mistake?
Note : I googled and found out What i am trying is possible. How to create trigger to insert data to a database on another server for possible
Try using the fully qualified table names in your trigger:
DELIMITER $$
CREATE TRIGGER remote_insert_Testing
AFTER INSERT ON FirstServerDB.remote_user2 FOR EACH ROW
BEGIN
INSERT INTO SecondServerDB.user (ID,name, age)
VALUES (NEW.ID, NEW.name, NEW.Age);
END $$
DELIMITER ;
If you don't mind moving the data through a normal query, you can try the following:
INSERT INTO SecondServerDB.user (ID, name, age)
SELECT ID, name, age
FROM FirstServerDB.remote_user2
I'm trying to create a procedure which adds / removes a table to the database. I'm working in SQL Server. The query works (it succeeds) but the table isn't added to the database.
I did refreshed...
ALTER procedure [dbo].[upgrade_1]
as
begin
create table awards (
ID int NOT NULL IDENTITY,
name nvarchar(256) DEFAULT 'award',
description nvarchar(256)
PRIMARY KEY (ID)
)
/*update goto_vs
set current_version = 1*/
end
The script you have in the question will only modify the PROCEDURE. The procedure needs to be executed for it to perform the required task e.g. create the table
Execute the procedure with this statement
EXEC upgrade_1
That should create the table
Can I create tables using Stored Procedures?
I am using MySQL. I had searched the web for instructions on how to create a table using stored procedure, but I didn't find any results, I just found that you can create temporary tables. Is there any problem if I create tables using stored procedures?
Actually I want to check whether a table exists or not, then I have to create it.
You can create tables using procedures in mysql.
delimiter |
CREATE PROCEDURE SP_CREATE_TABLE_TEST ()
BEGIN
CREATE TABLE TEST
(
TestID int(11) default NULL,
TestName varchar(100) default NULL
)
ENGINE=InnoDB DEFAULT CHARSET=utf8;
END;
|
Reference link.
EDIT: Thanks Peter! So the solution to this particular problem:
DELIMITER //
CREATE PROCEDURE createTable(IN tableName VARCHAR(40))
BEGIN
SET #table := tableName;
SET #sql_text:=CONCAT('CREATE TABLE ',#table,'(id INT NOT NULL AUTO_INCREMENT, FILENAME VARCHAR(40) NOT NULL, DATA BLOB NOT NULL, PRIMARY KEY (ID))');
PREPARE stmt from #sql_text;
EXECUTE stmt;
END //
DELIMITER ;
In addition to the manual I also found a very helpful tut by Roland Bouman at:
http://rpbouman.blogspot.com/2005/11/mysql-5-prepared-statement-syntax-and.htmlenter code here
El autor de la respuesta: Conrad Ljungström
link de la respuesta original: https://forums.mysql.com/read.php?98,495352
You can create a table in a stored procedure without a problem. Example:
delimiter |
create procedure createtab ()
begin
create table a(id int);
insert into a select 1;
select * from a;
drop table a;
end
|
delimiter ;
Something like this should work.
delimiter |
CREATE PROCEDURE SP_CREATE_TABLE( )
BEGIN
CREATE TABLE employees (emp_id int, emp_name varchar(100));
END;
|