SELECT IF((SELECT id FROM users where store='XXX' limit 1) IS NULL, null, (SELECT id FROM users where store='XXX' limit 1)) INTO #user_id;
select #user_id;
INSERT INTO `orders`(`user_id`)VALUES(#user_id);
I need is that when #user_id is NULL I don't want to insert into my table orders. If I put an if else statement above the insertion statement,I'll get an error saying that "syntax error unexpected IF".
Use IFNULL function, this way:
SELECT IFNULL(condition, value_if_condition_is_null)
In your query, something like:
SELECT IFNULL((SELECT id FROM users where store='XXX' limit 1), (SELECT id FROM users where store='XXX' limit 1))) INTO #user_id;
Related
I can't figure out what's wrong with my stored procedure.
CREATE PROCEDURE duplicatecheck2
#QmemberID INT,
#InputDate DATE
AS
INSERT INTO tbl_availableMembers
SELECT memberId
FROM tbl_attendancesheet
WHERE MemberId = #QmemberId
AND [date] = #InputDate
AND [clockin] IS NOT NULL
AND [clockout] IS NULL
UNION
INSERT INTO tbl_availableMembers
SELECT memberId
FROM tbl_attendancemembers
WHERE memberId NOT IN (SELECT memberId
FROM tbl_attendanceSheet)
WHERE date = #InputDate)
I get these errors:
Msg 156, Level 15, State 1, Procedure duplicatecheck2, Line 14
Incorrect syntax near the keyword 'Insert'.
Msg 156, Level 15, State 1, Procedure duplicatecheck2, Line 18
Incorrect syntax near the keyword 'WHERE'.
My other stored procedure that union has worked in it:
CREATE PROCEDURE duplicateCheck
#inputdate DATE
AS
BEGIN
DELETE FROM tbl_availableMembers
INSERT INTO tbl_availableMembers
SELECT memberId
FROM tbl_attendancemembers
WHERE memberId NOT IN (SELECT memberId FROM tbl_attendanceSheet)
UNION
SELECT memberId
FROM tbl_attendancemembers
WHERE memberId IN (SELECT memberId
FROM tbl_attendanceSheet
GROUP BY memberId, date
HAVING COUNT(*) <= 1 AND date = #inputdate)
END
Get rid of the "UNION". The stored procedure can just run two insert statements.
Noel
You cannot apply the UNION to two INSERT statements (as in your first code snippet) - but you can apply it to two SELECT statements (as in your second code sample)
By #marc_s
Am trying this query to get result. but am not able to find any results. I don't know where i did the mistake.
Select * from loadcell_transaction_log where id =
(SELECT max(id) as id1
FROM [addpro].[dbo].[loadcell_transaction_log] group by line2_count);
This is the error am getting:
Msg 512, Level 16, State 1, Line 2
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Try this:
Select * from loadcell_transaction_log where id IN
(SELECT max(id) as id1
FROM [addpro].[dbo].[loadcell_transaction_log] group by line2_count);
I replace '=' with the keyword 'IN' which allows for multiple results in the subquery.
using group by with max(id) will return max(id) for each line2_count :
select * from
(select log.*,row_number() over (partition by line2_count order by id desc ) rn FROM [addpro].[dbo].[loadcell_transaction_log] as log)
where rn = 1;
If you want to check only for max(id) in the full table data then dont use group by clause else use in clause as referred by #farbiondriven.
I am trying to count the number of distinct email addresses in a view, but I can't figure out the syntax... Here's what I have:
BEGIN
SELECT COUNT(*) AS UserCount FROM (
SELECT DISTINCT EmailAddress
FROM viewCohortsAuthorizedByContractor
WHERE (ListAccess = 1) AND (ContractorId = #id)
)
END
The inner select works fine, but if I try adding the COUNT, I get the following error: Msg 156, Level 15, State 1, Procedure rptContractorUsersWithListUserCount, Line 23 [Batch Start Line 15]
Incorrect syntax near the keyword 'END'.
I'm sure there's a simple solution, I just can't figure it out...
You don't need the subquery. Try this:
SELECT count(DISTINCT EmailAddress) as UserCount
FROM viewCohortsAuthorizedByContractor
WHERE (ListAccess = 1) AND (ContractorId = #id)
BEGIN
SELECT COUNT(*) AS UserCount FROM (
SELECT DISTINCT EmailAddress
FROM viewCohortsAuthorizedByContractor
WHERE (ListAccess = 1) AND (ContractorId = #id)
)A -----give a name for result set
END
INSERT INTO admin_userhistroy(sno,UserDetails_ID,UserMessage,SystemMessage_ID,insdate,STATUS,UserDetails_MsgTo_ID,License_Status)
VALUES ('1',(SELECT DISTINCT id FROM admin_userdetails WHERE token = 'ABCDE1' LIMIT 1)
,'New User is Created','4',cast(NOW() as string),'Y',cast((SELECT DISTINCT ID FROM admin_userdetails WHERE UserName = 'cangoadmin' AND token ='ABCDE1' LIMIT 1)as string)
,NULL)
Above is my query I have executed the same query in Mysql but I cant execute the same in Impala, someone please help me out.
I get the following error:
Subqueries not supported in the selected list
Try insert . . . select:
INSERT INTO admin_userhistroy(sno,U serDetails_ID, UserMessage, SystemMessage_ID, insdate, STATUS, UserDetails_MsgTo_ID, License_Status)
SELECT '1', x.id, 'New User is Created', '4',
cast(NOW() as string), 'Y', y.id
FROM (SELECT id FROM admin_userdetails WHERE token = 'ABCDE1' LIMIT 1) x CROSS JOIN
(SELECT CAST(ID as string) as id FROM admin_userdetails WHERE UserName = 'cangoadmin' AND token ='ABCDE1' LIMIT 1) y;
Note that you don't need select distinct in the subqueries.
I'm trying to use a while loop in a one-off query on a MySQL (5.1.41-3ubuntu12.10-log) database:
WHILE ((SELECT COUNT(*) FROM
(SELECT id, COUNT(*) AS cnt
FROM foo
GROUP BY id
ORDER BY COUNT(*) DESC) cnts
WHERE cnt > 1) != 0) DO
BEGIN
SET #curr_id = (SELECT id FROM
(SELECT id, COUNT(*) AS cnt
FROM foo
GROUP BY id
ORDER BY COUNT(*) DESC) cnts
WHERE cnt > 1
LIMIT 1);
SET #new_id = (SELECT MAX(id) + 1
FROM foo);
UPDATE foo
SET id = #new_id
WHERE id = #curr_id
LIMIT 1;
END WHILE;
What this does is while there are multiple records with the same id, update one of them with the next id.
The syntax in the body is correct and the predicate used in the while statement also executes without any trouble on it's own. MySQL returns a syntax error on the beginning of the query:
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 'WHILE ((SELECT count(*) FROM
(SELECT id, COUNT(*) AS cnt
FROM stock_produ' at line 1
I realize this might not be The Right Way of doing things, but this is a very badly (or rather not-at-all) thought out database, so it would be awesome if I could get it to work this way.
Thanks,
Robin
It looks as though you are trying to run this procedural code as an anonymous block. While this works in some databases (like Oracle) it can't be done in MySQL.
If you want to run this then put it in a stored procedure and then call the procedure. Hence:
Create procedure
DELIMITER $$
CREATE PROCEDURE `foo_update_routine`()
BEGIN
WHILE ((SELECT COUNT(*) FROM
(SELECT id, COUNT(*) AS cnt
FROM foo
GROUP BY id
ORDER BY COUNT(*) DESC
) cnts
WHERE cnt > 1) != 0)
DO
SET #curr_id = (SELECT id FROM
(SELECT id, COUNT(*) AS cnt
FROM foo
GROUP BY id
ORDER BY COUNT(*) DESC
) cnts
WHERE cnt > 1
LIMIT 1);
SET #new_id = (SELECT MAX(id) + 1 FROM foo);
UPDATE foo SET id = #new_id
WHERE id = #curr_id
LIMIT 1;
END WHILE;
END $$
Call Procedure
CALL `foo_update_routine`;
PS You might want to investigate the HAVING clause for your select statements...