Click here to Skip to main content
15,914,160 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have upper given procedure and it display syntax error.
I have to use while loop in where clause as per my project requirement.

I have to compare two comma separated fields in where clause so i have set while loop in where clause which is used to get comma separated values one by one and pass it to find_in_set().

Before WHILE statement is closed with semicolon(;) but in query how it's possible...

This is the because it display syntax error... Is there any solution for this ??
If you have alternative for this so please guide me ...

Thank you in advance

What I have tried:

SQL
BEGIN
	DECLARE i INT DEFAULT 0;
	DECLARE ctr INT DEFAULT 0;
	DECLARE str_len INT;
	DECLARE total_row INT;
	DECLARE occurance INT;
	DECLARE split_comma text;
	DECLARE out_str TEXT DEFAULT '';
	DECLARE str TEXT DEFAULT '';
	DECLARE tmp_str TEXT DEFAULT '';
	DECLARE tmp_val VARCHAR(255) DEFAULT '';
	
	SELECT course AS str FROM student where s.id = studentid1;
	SET str_len = LENGTH(str);
	SET i = (LENGTH(str)-LENGTH(REPLACE(str, ',', '')))/LENGTH(',') + 1;  	
	
select (SELECT count(distinct t.id)
	FROM test As t, package AS p, packagetestassoc as pt, course AS c, student as s
	WHERE p.id = pt.packageid AND find_in_set(t.id, pt.testid) <> 0 AND 
	t.status = 1 AND find_in_set(c.id,p.courseid) AND 
	
	WHILE (ctr < i) DO
		SET ctr = ctr + 1;
		SET tmp_str = REPLACE(SUBSTRING(SUBSTRING_INDEX(str, ',',ctr), LENGTH(SUBSTRING_INDEX(str,',',ctr-1))+1),',','');
	 	SELECT find_in_set(tmp_str, p.courseid);
	END WHILE;
	
	AND s.id = studentid1 AND 
	(p.packagecost =0 or
 	p.id IN (SELECT ps.packageid FROM paymentstatus AS ps WHERE ps.studentid = studentid1)))
AS totaltest,

(select (SELECT count(distinct t.id)
	FROM test As t, package AS p, packagetestassoc as pt, course AS c, student as s
	WHERE p.id = pt.packageid AND find_in_set(t.id, pt.testid) <> 0 AND
	
	WHILE (ctr < i) DO
		SET ctr = ctr + 1;
		SET tmp_str = REPLACE(SUBSTRING(SUBSTRING_INDEX(str, ',',ctr), LENGTH(SUBSTRING_INDEX(str,',',ctr-1))+1),',','');
	 	SELECT find_in_set(tmp_str, p.courseid);
	END WHILE;
	
	AND s.id = studentid1 AND 
	t.status = 1 AND find_in_set(c.id,p.courseid) AND t.testtype = 2 AND CURDATE() > t.enddate AND
	 t.id NOT IN (SELECT testid FROM testresult WHERE studentid = studentid1) AND t.id NOT IN (SELECT testid FROM incompletedtest WHERE studentid = studentid1) AND (p.packagecost =0 or
 	p.id IN (SELECT ps.packageid FROM paymentstatus AS ps WHERE ps.studentid = studentid1))))
AS missedtest,

(SELECT count(*)
 FROM test AS t, incompletedtest AS i
 where i.studentid = studentid1 AND t.id = i.testid) AS
incompletetest,

(SELECT count(*)
 FROM test AS t, testresult AS i
 where i.studentid = studentid1 AND t.id = i.testid) AS
completetest;
END;
Posted
Updated 4-May-16 22:02pm
v2
Comments
George Jonsson 5-May-16 3:59am    
I understand that English is not your first language, but it is difficult to understand on which line the syntax error occurs.
Member 12479831 5-May-16 4:11am    
On line 22 Where While loop is start
George Jonsson 5-May-16 9:54am    
If I calculated correctly, this is the problem line
SET tmp_str = REPLACE(SUBSTRING(SUBSTRING_INDEX(str, ',',ctr), LENGTH(SUBSTRING_INDEX(str,',',ctr-1))+1),',','');

What is the value of str and what is the expected result?

Update your question with this info.
(And next time, reply to my message so I get notified)

1 solution

You need to separate off a function to convert the comma separated list into a table.

There are plenty of examples out there. The example below gives you all you need:

Using comma separated value parameter strings in SQL IN clauses[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900