Given below is my table structure
CREATE TABLE gtab86
(
mlid integer DEFAULT nextval('seq_gtab86_id'::regclass),
acyrid integer,
lmonth integer,
islocked boolean
)
In this table lmonth is the month fied, and acyrid is the year denoting value for example 1.
I wrote the following Function to insert into gtab86.
CREATE OR REPLACE FUNCTION createmonthlock(iacyrid integer)
RETURNS void AS '
BEGIN
INSERT INTO gtab86(acyrid, lmonth) VALUES (iacyrid,1);
INSERT INTO gtab86(acyrid, lmonth) VALUES (iacyrid,2);
INSERT INTO gtab86(acyrid, lmonth) VALUES (iacyrid,3);
INSERT INTO gtab86(acyrid, lmonth) VALUES (iacyrid,4);
INSERT INTO gtab86(acyrid, lmonth) VALUES (iacyrid,5);
INSERT INTO gtab86(acyrid, lmonth) VALUES (iacyrid,6);
INSERT INTO gtab86(acyrid, lmonth)VALUES (iacyrid,7);
INSERT INTO gtab86(acyrid, lmonth)VALUES (iacyrid,8);
INSERT INTO gtab86(acyrid, lmonth)VALUES (iacyrid,9);
INSERT INTO gtab86(acyrid, lmonth)VALUES (iacyrid,10);
INSERT INTO gtab86(acyrid, lmonth)VALUES (iacyrid,11);
INSERT INTO gtab86(acyrid, lmonth)VALUES (iacyrid,12);
END;'
LANGUAGE plpgsql VOLATILE COST 100;
As you can see, insert into is repeated 12 times (for 12 lmonth field rows).
How to make it only one insert query?
Is it possible to use for 0 to 12 kind things inside a function?
You can use insert .. select based on generate_series().
You also don't need PL/pgSQL for this, a plain SQL function will do:
create or replace function createmonthlock(iacyrid integer)
returns void
as
$body$
INSERT INTO gtab86(acyrid, lmonth)
select iacyrid, num
from generate_series(1,12) num;
$body$
language sql;
You can insert multiple rows with a single INSERT statement:
INSERT INTO gtab86(acyrid, lmonth) VALUES (iacyrid,1), (iacyrid,2), (iacyrid,3), (iacyrid,4), (iacyrid,5), (iacyrid,6), (iacyrid,7), (iacyrid,8), (iacyrid,9), (iacyrid,10), (iacyrid,11), (iacyrid,12);
Related
How can i pass string in values rather than passing each value.
Receiving error
Error Code: 1136. Column count doesn't match value count at row 1
INSERT INTO `ProfileResumeActivityLog`
(`Email`,`ProfileID`,`ExternalUserDID`, `EventType`, `ResumeDID`, `EventText`, `CreatedOn`)
VALUES('checkpp#test.com,73443358,XR2Z1WT6WR5PPR6Z99L4,r
esume_backload,R3W6WN6LFY95PKL27SR,resume_create, NOW()' );
I want to pass this as string because my stored procedure takes dynamic number of values for batch insert
CREATE DEFINER=`root`#`localhost` PROCEDURE `SaveBatchMultipleProfileResumeActivityLog`(
_resumeBatch TEXT
)
BEGIN
INSERT INTO `ProfileResumeActivityLog`
(`Email`,`ProfileID`,`ExternalUserDID`, `EventType`, `ResumeDID`, `EventText`, `CreatedOn`)
VALUES(_resumeBatch);
END
calling
CALL CBAX.SaveBatchMultipleProfileResumeActivityLog ('checkpp#test.com,73443358,XR2Z1WT6WR5PPR6Z99L4,resume_backload,R3W6WN6LFY95PKL27SR,resume_create, NOW()',
'checkpp#test1.com,73443358,XR2Z1WT6WR5PPR6Z99L41,resume_backload,R3W6WN6LFY95PKL27SR1,resume_create, NOW()' )
when you put string inside a insert statement you have to put them inside a "" thats why you get that error.
INSERT INTO `ProfileResumeActivityLog`(`Email`,`ProfileID`,`ExternalUserDID`, `EventType`, `ResumeDID`, `EventText`, `CreatedOn`)
VALUES('checkpp#test.com',73443358,'XR2Z1WT6WR5PPR6Z99L4','resume_backload','R3W6WN6LFY95PKL27SR','resume_create', NOW() );
Can insert into table * number with same data in Mysql?(Yes, have same value 3 or 100 times)
INSERT INTO `test`(`name`, `test1`, `other_status`, `status`)
VALUES ('string', 'KPrBf9', 1, 0) * 3;
You need a stored procedure to insert same statement multiple times:
DELIMITER $$
CREATE PROCEDURE insert_loop ( IN nr_input bigint)
BEGIN
DECLARE counter BIGINT DEFAULT 0;
my_loop: LOOP
SET counter=counter+1;
IF counter=nr_input THEN
LEAVE my_loop;
END IF;
INSERT INTO `test`(`name`, `test1`, `other_status`, `status`)
VALUES ('string', 'KPrBf9', 1, 0);
END LOOP my_loop;
END$$
DELIMITER ;
Above procedure will insert same values based on the input parameter you give.
Call the procedure by using:
call insert_loop(5); ---number of rows to be inserted
Not sure what do you mean by *3
But if you want to insert multiple records you can use the following
INSERT INTO `test`(`name`, `test1`, `other_status`, `status`)
VALUES ('string', 'KPrBf9', 1, 0),
('string', 'KPrBf9', 1, 0),
('string', 'KPrBf9', 1, 0);
Forget about INSERT .. VALUES and study INSERT .. SELECT .
INSERT INTO table_name (columns_names) -- (`name`, `test1`, `other_status`, `status`)
WITH RECURSIVE cte AS (
SELECT 1 AS amount
UNION ALL
SELECT amount + 1 FROM cte WHERE amount < #amount_of_rows_to_insert
)
SELECT 'values list' -- 'string' AS name, 'KPrBf9' AS test1, 1 AS other_status, 0 AS status
FROM cte;
If MySQL version is too old and does not support CTE then
INSERT INTO table_name (columns_names)
SELECT 'values list'
FROM ( SELECT 1 AS amount UNION ALL
SELECT 2 UNION ALL
-- ...
SELECT #amount_of_rows_to_insert ) AS cte;
I have a variable #ids = "894,891,896,899..."
What I like to do is the following
loop #id: #ids
INSERT INTO `testcases_types` (`testcase_id`, `type_id`) VALUES (#id, '57');
INSERT INTO `testcases_types` (`testcase_id`, `type_id`) VALUES (#id, '58');
INSERT INTO `testcases_types` (`testcase_id`, `type_id`) VALUES (#id, '59');
INSERT INTO `testcases_types` (`testcase_id`, `type_id`) VALUES (#id, '60');
end loop
So I tried a lot now , but I cannot find out the right syntax. I do not want to write a procedure. I just want to execute it once .
Is it possible to implement a 1:N relationship that has 10 rows of the many referencing 1 row from the one table at most?
// ID int
INSERT INTO one VALUES (1);
// ...
INSERT INTO one VALUES (25);
//ID int, one_id int
INSERT INTO many VALUES (1,1);
// ...
INSERT INTO many VALUES (1,10);
INSERT INTO many VALUES (1,11); // ERROR!
INSERT INTO many VALUES (2,11); // working
// ...
INSERT INTO many VALUES (2,20); // working
INSERT INTO many VALUES (2,21); // ERROR!
You can do this with a trigger:
create trigger trg_mytable_max10 before insert on mytable
for each row
begin
declare cnt int;
set cnt = (select count(*) from mytable where col1 = new.col1);
if cnt = 10 then
signal sqlstate '45000' set message_text = 'only 10 records per col1 allowed';
end if;
end;
http://rextester.com/EORH56497
I want to to create 5 string sequential data like
aaaaa
aaaab
aaaac
.... upto
zzzzx
zzzzy
zzzzz
Does sql have any function that would help me with sequential data generation?
Currently I have four digits sequential data, how can I make generate five digit sequential data?
What i have
aaaa
aaab
aaac
....upto
zzzx
zzzy
zzzz
I wrote the following procedure but it takes forever to complete.. can anybody help me rewrite the procedure or advise a different approch.
CREATE DEFINER=`root`#`localhost` PROCEDURE `new_procedure`()
BEGIN
DECLARE a INT Default 1 ;
DECLARE tran varchar(255) Default 'aaaa';
simple_loop: LOOP
SET a=a+1;
SET tran = (select fourth from m where idm=a);
Insert into test.qwe(zxc) values (CONCAT(tran,'a'));
Insert into test.qwe(zxc) values (CONCAT(tran,'b'));
Insert into test.qwe(zxc) values (CONCAT(tran,'c'));
Insert into test.qwe(zxc) values (CONCAT(tran,'d'));
Insert into test.qwe(zxc) values (CONCAT(tran,'e'));
Insert into test.qwe(zxc) values (CONCAT(tran,'f'));
Insert into test.qwe(zxc) values (CONCAT(tran,'g'));
Insert into test.qwe(zxc) values (CONCAT(tran,'h'));
Insert into test.qwe(zxc) values (CONCAT(tran,'i'));
Insert into test.qwe(zxc) values (CONCAT(tran,'j'));
Insert into test.qwe(zxc) values (CONCAT(tran,'k'));
Insert into test.qwe(zxc) values (CONCAT(tran,'l'));
Insert into test.qwe(zxc) values (CONCAT(tran,'m'));
Insert into test.qwe(zxc) values (CONCAT(tran,'n'));
Insert into test.qwe(zxc) values (CONCAT(tran,'o'));
Insert into test.qwe(zxc) values (CONCAT(tran,'p'));
Insert into test.qwe(zxc) values (CONCAT(tran,'q'));
Insert into test.qwe(zxc) values (CONCAT(tran,'r'));
Insert into test.qwe(zxc) values (CONCAT(tran,'s'));
Insert into test.qwe(zxc) values (CONCAT(tran,'t'));
Insert into test.qwe(zxc) values (CONCAT(tran,'u'));
Insert into test.qwe(zxc) values (CONCAT(tran,'v'));
Insert into test.qwe(zxc) values (CONCAT(tran,'w'));
Insert into test.qwe(zxc) values (CONCAT(tran,'x'));
Insert into test.qwe(zxc) values (CONCAT(tran,'y'));
Insert into test.qwe(zxc) values (CONCAT(tran,'z'));
IF a=1 THEN
LEAVE simple_loop;
END IF;
END LOOP simple_loop;
END
From scratch:
CREATE TABLE alpha (a CHAR(1) NOT NULL);
INSERT INTO alpha (a) VALUES
('a'), ('b'), ('c'), ('d'), ('e'), ('f'),
('g'), ('h'), ('i'), ('j'), ('k'), ('l'),
('m'), ('n'), ('o'), ('p'), ('q'), ('r'),
('s'), ('t'), ('u'), ('v'), ('w'), ('x'),
('y'), ('z');
CREATE TABLE qwe (zxc CHAR(5) NOT NULL);
INSERT INTO qwe (zxc)
SELECT CONCAT(a1.a, a2.a, a3.a, a4.a, a5.a)
FROM alpha a1, alpha a2, alpha a3, alpha a4, alpha a5;
If you already have all the strings of length 4 in a table, you can just join to alpha once and concatenate the values to generate all the strings of length 5. It's still going to take some time, no way around that.
The sql procedure I wrote worked.. It took around 4-5 hours to popoulta 26x26x26x26 combinations of 6 string words.
Thanks for all the suggestions !