Trying hard to solve error 1064 - mysql

I have tried hard but not getting the solutin regading sp below.. plz help !!
CREATE PROCEDURE AccountLedgerViewUnderBank()
begin
WITH GroupInMainGroup (accountGroupId,HierarchyLevel) AS
(
select accountGroupId,
1 as HierarchyLevel
from tbl_AccountGroup where accountGroupId='9'
UNION ALL
select e.accountGroupId,
G.HierarchyLevel + 1 AS HierarchyLevel
from tbl_AccountGroup as e,GroupInMainGroup G
where e.groupUnder=G.accountGroupId
)
SELECT
ledgerId AS 'Account Ledger Id',
acccountLedgerName AS 'Account Ledger Name'
FROM tbl_AccountLedger
where accountGroupId IN (select accountGroupId from GroupInMainGroup
)
end ; //
The error showing
ERROR 1064 (42000): 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 'Group
InMainGroup (accountGroupId,HierarchyLevel) AS
(
select accountGroupId,
1 a' at line 6

It looks like you're trying to use a CTE, which is not supported with MySQL (at time of writing).
Subqueries are supported with most versions of MySQL, so you may be able to rewrite it as something like:
CREATE PROCEDURE AccountLedgerViewUnderBank()
begin
SELECT ledgerId AS 'Account Ledger Id',
acccountLedgerName AS 'Account Ledger Name'
FROM tbl_AccountLedger
where accountGroupId
IN (select accountGroupId
from tbl_AccountGroup where accountGroupId='9'
UNION ALL
select e.accountGroupId
from tbl_AccountGroup as e,GroupInMainGroup G
where e.groupUnder=G.accountGroupId)
end ; //

Related

select s.no with the select statement in procedure

i am having a hard time to generate the s.no with the select statement in stored procedure. can anyone please tell me how can i increase the value of variable for each row
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `sp_select_invoice_pdf`(IN `cus_ivc_id` INT(11))
NO SQL
BEGIN
DECLARE a INT(11);
set a=0;
SELECT
a:=a+1 as 'S.No',
ci.`order_id` as 'Order ID',
cp.`date` as 'Date',
cp.`product_name` as 'Design name',
(`customer_invoice`.`amount`-`customer_invoice`.`discount`) as 'Amount'
from `customer_invoice` ci
LEFT JOIN `customer_product` cp on cp.`customer_product_id` =
ci.`customer_product_id`
END$$
DELIMITER ;
Error: MySQL said: #1064 - You have an error in your SQL syntax; check
the manual that corresponds to your MariaDB server version for the
right syntax to use near ':=a+1 as 'S.No', ci.order_id as 'Order
ID', cp' at line 5
however the below is working fine for me but can't figure out how to do it in stored procedure. your help will be appreciated.
set #a:=0;
SELECT
#a:=#a+1 as 'S.No',
ci.`order_id` as 'Order ID',
cp.`date` as 'Date',
cp.`product_name` as 'Design name',
(`customer_invoice`.`amount`-`customer_invoice`.`discount`) as 'Amount'
from `customer_invoice` ci
LEFT JOIN `customer_product` cp on cp.`customer_product_id` =
ci.`customer_product_id`;

Creating a trigger which enters value in studies(rollno,code) table only if total credits of a student are less than 30

I am getting the error : #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 '' at line 5.
CREATE TRIGGER db BEFORE INSERT ON studies
FOR EACH ROW
BEGIN
IF((select CAST(SUM(credits) as UNSIGNED) from ((SELECT credits from subject WHERE subject.code = NEW.code) union all (SELECT credits from subject,(SELECT code from studies where studies.rollno = NEW.rollno) as t1 WHERE subject.code = t1.code)) as t2) > 30) THEN
set NEW.rollno = NULL;
ENDIF;
END

SQL If Statment / select into in a stored procedure

I cannot understand why the following SQL procedure will not store on my database and is reporting an error, I've been at it for ages and am totally puzzled.
DELIMITER $$
CREATE PROCEDURE add_brand(IN inBrandName TEXT)
BEGIN
DECLARE brandexists INT DEFAULT 0;
SELECT count(*) WHERE BrandName = inBrandName INTO brandexists;
IF brandexists = 1 THEN
select "exists";
ELSE
INSERT INTO tblBrand (BrandName) VALUES (inBrandName);
SELECT LAST_INSERT_ID();
END IF;
END
The error i'm getting is this:
#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 'WHERE BrandName = inBrandName INTO brandexists; IF brandexists = 1 THEN sele' at line 6
any ideas?
You are missing a table where you select from:
SELECT count(*) WHERE BrandName = inBrandName INTO brandexists;
^---here (from some_table)

My sql error in creating temporaty table stored procedure

I have a stored procedure query and inserting value in temp table.. but shows error
delimiter //
create procedure AccountLedgerViewUnderSundryDebtorCreditorCash()
begin
create temporary table temp_kk(accountGroupID varchar(50),HierarchyLevel varchar(50));
insert into temp_kk(accountGroupID,HierarchyLevel)
select accountGroupId, 1 as HierarchyLevel from tbl_AccountGroup
where accountGroupId='27'or accountGroupId = '28'or
accountGroupId = '11';
create temporary table temp_kk2(accountGroupID varchar(50),HierarchyLevel varchar(50));
insert into temp_kk2(accountGroupID,HierarchyLevel)
select e.accountGroupId, G.HierarchyLevel + 1 AS HierarchyLevel
from tbl_AccountGroup as e, temp_kk G
where e.groupUnder=G.accountGroupId ;
create temporary table temp_kk3(accountGroupID varchar(50),HierarchyLevel varchar(50));
insert into temp_kk3(accountGroupID,HierarchyLevel)
select * from temp_kk union all temp_kk2 ;
SELECT
ledgerId AS 'Account Ledger Id',
accountLedgerName AS 'Account Ledger Name',
accountGroupId AS 'Account Group Id',
openingBalance AS 'Opening Balance',
debitOrCredit AS 'Credit Or Debit',
defaultOrNot AS 'Editable Or Not',
description AS 'Description'
FROM tbl_AccountLedger
where accountGroupId IN (select accountGroupId from temp_kk3);
end //
delimiter ;
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 'temp_kk2 ; SELECT ledgerId AS [Account Ledger Id], acccountLedgerName' at line 20
This line:
select * from temp_kk union all temp_kk2 ;
Needs to be:
select * from temp_kk union all select * from temp_kk2 ;
At least, that what sticks out most obviously.
Though, I tend to avoid using select *. If you change the temp table, it will break. It also ends up making the code harder to read if you don't know for sure what all columns are in the table you're selecting from. Yes, it saves you some typing right now, but you'll lose that time-savings later on when you have to keep looking back at the table definition to figure out what exactly you're selecting.
Though, with these simple temp tables, it isn't as big of a deal.

insert if not exists mysql 5.5.24

I have been looking around, however i can not see my error,
My query
INSERT INTO p_location_check (location_id) VALUES (1)
IF NOT EXISTS (SELECT approved, disapproved FROM p_location_check WHERE approved REGEXP '^1234568745$' OR disapproved = '^1234568745$' AND location_id=1);
after just for testing
INSERT INTO p_location_check (location_id) VALUES (1)
IF NOT EXISTS (SELECT approved, disapproved FROM p_location_check WHERE approved = 1234568745 OR disapproved = 1234568745 AND location_id=1);
reponse
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 'IF NOT EXISTS (SELECT approved, disapproved FROM
qp_location_check WHERE approve' at line 2
server version
Server version: 5.5.24-0ubuntu0.12.04.1
Edited:
Try this query -
INSERT INTO p_location_check (location_id) VALUES (1) FROM dual
WHERE (SELECT COUNT(*)
FROM p_location_check
WHERE approved = 1234568745 OR disapproved = 1234568745 AND location_id=1
) = 0;
Add your WHERE condition.