473,396 Members | 2,140 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Stored Procedure SELECT Statement Error

2
Hello everyone,

I need help with a stored procedure or two.

My stored procedures are supposed to check if a certain record exists. If it does exist, then I select everything from that row, as well as a value for the exit status of the SP. If the record doesn't exist, it selects an error message as the exit status.

I've recreated the SP (while changing field names) and restructured the table, just to get straight to the point.



The table I'm working with looks something like this:
Expand|Select|Wrap|Line Numbers
  1. DROP TABLE IF EXISTS `test_table`;
  2. CREATE TABLE `test_table` (
  3. `id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  4. `name` VARCHAR(32));

As for the stored procedures, here is the first one:

Expand|Select|Wrap|Line Numbers
  1. DROP PROCEDURE IF EXISTS `test_sp2`;
  2. DELIMITER //
  3. CREATE PROCEDURE `test_sp2` (IN `p_id` INT)
  4. BEGIN
  5.     SELECT @num := COUNT(`id`) FROM `test` WHERE `id` = `p_id`;
  6.     IF @num > 0 THEN
  7.         SELECT * FROM `test` WHERE `id` = `p_id`;
  8.         SELECT 0 AS `status`; -- 'Return value,' to be used by the app. 0 if SP executed as desired, otherwise it will contain an error message
  9.     ELSE
  10.         SELECT 'Record does not exist.' AS `status`;
  11.     END IF;
  12. END;
  13. //
  14. DELIMITER ;




Then here is the second SP, which I tried to rewrite with the IF EXISTS statement, but still no luck.

Expand|Select|Wrap|Line Numbers
  1. DELIMITER //
  2. CREATE PROCEDURE `test_sp` (IN `p_id` INT)
  3. BEGIN
  4.     IF EXISTS(SELECT @num := COUNT(`id`) FROM `test` WHERE `id` = `p_id`) THEN
  5.         SELECT * FROM `test` WHERE `id` = `p_id`;
  6.         SELECT 0 AS `status`; -- 'Return value,' to be used by the app. 0 if SP executed as desired, otherwise it will contain an error message
  7.     ELSE
  8.         SELECT 'Record does not exist.' AS `status`;
  9.     END IF;
  10. END;
  11. //
  12. DELIMITER ;

The error I get (for either SP) is:

Expand|Select|Wrap|Line Numbers
  1. 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 '* FROM `test` WHERE `id` = `p_id`;


I've tried completely stripping down the SP, and it looks like the problem lies in the first SELECT statement.

What baffles me is that these were working just the other day, and now they're not.


I'm currently running MySQL 5.0.51a.


Any help or advice is greatly appreciated. If anyone needs more information out of me regarding this, I'd be glad to answer any questions.
Sep 8 '08 #1
2 3390
kxyz
2
I found the solution. Using two or more tabs to indent (for any statement), will NOT work.

Indenting one tab will work or not indenting at all will work, but indenting with just two or more tabs is a syntax error.

Does not work (two tabs):
Expand|Select|Wrap|Line Numbers
  1. DROP PROCEDURE IF EXISTS `test_sp2`;
  2. DELIMITER //
  3. CREATE PROCEDURE `test_sp2` (IN `p_id` INT)
  4. BEGIN
  5.     SELECT @num := COUNT(`id`) FROM `test_table` WHERE `id` = `p_id`;
  6.     IF @num > 0 THEN
  7.         SELECT * FROM `test_table` WHERE `id` = `p_id`;
  8.         SELECT 0 AS `status`; -- 'Return value,' to be used by the app. 0 if SP executed as desired, otherwise it will contain an error message
  9.     ELSE
  10.         SELECT 'Record does not exist.' AS `status`;
  11.     END IF;
  12. END;
  13. //
  14. DELIMITER ;


Works (one tab):
Expand|Select|Wrap|Line Numbers
  1. DROP PROCEDURE IF EXISTS `test_sp2`;
  2. DELIMITER //
  3. CREATE PROCEDURE `test_sp2` (IN `p_id` INT)
  4. BEGIN
  5.     SELECT @num := COUNT(`id`) FROM `test_table` WHERE `id` = `p_id`;
  6.     IF @num > 0 THEN
  7.     SELECT * FROM `test_table` WHERE `id` = `p_id`;
  8.     SELECT 0 AS `status`; -- 'Return value,' to be used by the app. 0 if SP executed as desired, otherwise it will contain an error message
  9.     ELSE
  10.     SELECT 'Record does not exist.' AS `status`;
  11.     END IF;
  12. END;
  13. //
  14. DELIMITER ;


After messing around with this SP for a bit, I see that using two or more tabs will not only trigger an error for the statements inside the IF ELSE block, but anywhere in the SP.

Does not work (three tabs):
Expand|Select|Wrap|Line Numbers
  1. DROP PROCEDURE IF EXISTS `test_sp2`;
  2. DELIMITER //
  3. CREATE PROCEDURE `test_sp2` (IN `p_id` INT)
  4. BEGIN
  5.             SELECT @num := COUNT(`id`) FROM `test_table` WHERE `id` = `p_id`;
  6.     IF @num > 0 THEN
  7.     SELECT * FROM `test_table` WHERE `id` = `p_id`;
  8.     SELECT 0 AS `status`; -- 'Return value,' to be used by the app. 0 if SP executed as desired, otherwise it will contain an error message
  9.     ELSE
  10.     SELECT 'Record does not exist.' AS `status`;
  11.     END IF;
  12. END;
  13. //
  14. DELIMITER ;


However, you can use two or more tabs for indentation, but you MUST include at least one space.

Works (two tabs and one space):
Expand|Select|Wrap|Line Numbers
  1. DROP PROCEDURE IF EXISTS `test_sp2`;
  2. DELIMITER //
  3. CREATE PROCEDURE `test_sp2` (IN `p_id` INT)
  4. BEGIN
  5.     SELECT @num := COUNT(`id`) FROM `test_table` WHERE `id` = `p_id`;
  6.     IF @num > 0 THEN
  7.          SELECT * FROM `test_table` WHERE `id` = `p_id`;
  8.          SELECT 0 AS `status`; -- 'Return value,' to be used by the app. 0 if SP executed as desired, otherwise it will contain an error message
  9.     ELSE
  10.          SELECT 'Record does not exist.' AS `status`;
  11.     END IF;
  12. END;
  13. //
  14. DELIMITER ;


Also, I see that I mislabeled the table that the stored procedures pulling data from. In my example the table the stored procedures should have been selecting data from should have been `test` instead of `test_table`. This wasn't actually part of the problem, I had just created the `test` table as an example.



I hope this will help someone else down the road.
Sep 8 '08 #2
Atli
5,058 Expert 4TB
Hi.

Thanks for sharing the solution!

I got to ask tho. What OS are you using and how are you executing the command? (Like say, via the CLI?)

I tested this myself on my Ubuntu system, using the standard CLI, and I found that it worked no matter how many tabs I used.
But, I did get some weird display problems in the CLI when I added 2 or more tabs.

I wrote the code in Kate and pasted it into the CLI.

This was my test code:
Expand|Select|Wrap|Line Numbers
  1. DELIMITER $$
  2. DROP PROCEDURE IF EXISTS getMaxMemoryRange$$
  3. CREATE PROCEDURE getMaxMemoryRange(IN bits INT)
  4. BEGIN
  5.     SELECT POW(2, bits) / POW(1024, 3) AS 'Gigabytes';
  6. END;
  7. $$
  8. DELIMITER ;
  9.  
Which worked just as expected.

But when I added a second tab before the SELECT, the CLI input changed to this (when I pasted it):
Expand|Select|Wrap|Line Numbers
  1. mysql> DELIMITER $$
  2. mysql> DROP PROCEDURE IF EXISTS getMaxMemoryRange$$
  3. Query OK, 0 rows affected (0.00 sec)
  4.  
  5. mysql> CREATE PROCEDURE getMaxMemoryRange(IN bits INT)
  6.     -> BEGIN
  7.     ->
  8. amarok              dates.date_col      mysql
  9. date_col            dates.qty           qty
  10. dates               information_schema  test
  11.     -> SELECT POW(2, bits) / POW(1024, 3) AS 'Gigabytes';
  12.     -> END;
  13.     -> $$
  14. Query OK, 0 rows affected (0.00 sec)
  15.  
  16. mysql> DELIMITER ;
  17.  
It looks wrong but it still appeared to work as expected.

I thought his was a problem with the Linux kernel. Normally, when typing a path, double clicking the tab while in the middle of writing a path will show you suggestions as to how that path could be completed.

This kind of looks like it may be related.
Sep 8 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Bill Kellaway | last post by:
Hi there - this should be fairly simple for someone. Basically I can't figure out how to pass the parameters from ASP to a Stored Procedure on SQL. Here's my code: I just need to help in...
3
by: Jarrod Morrison | last post by:
Hi all Im relatively new to using stored procedures and im not sure if it is possible to do what I am trying to do so any help here is greatly appreciated. I am using the variable @MachineName...
8
by: Thomasb | last post by:
With a background in MS SQL Server programming I'm used to temporary tables. Have just started to work with DB2 ver 7 on z/OS and stumbled into the concept of GLOBAL TEMPORARY TABLE. I have...
5
by: Timppa | last post by:
Hi, Could anyone help me with my problem ? Environment: Access 2000 and Sql Server 2000. I have a stored procedure as follows: DROP table1 SELECT alias1.field1,alias2.field2,table2.field6...
1
by: deepdata | last post by:
Hi, I am trying to fetch data from db2 (express version) database by calling stored procedure. I have tried to use both cursor and for loop but still i am getting error. --======Start...
7
by: Siv | last post by:
Hi, I have a stored procedure that I want to execute and then wait in a loop showing a timer whilst it completes and then carry on once I get notification that it has completed. The main reason...
1
by: peaceburn | last post by:
Hi, I'm gonna pull my hair in the coming days with these DB2 stored procedures. So the issue, let's assume a simple stored procedure like this : CREATE PROCEDURE MYSCHEMA.PROCEDURE1 ( )...
7
by: sri | last post by:
My environment is DB2v9.1, Windows/AIX and I am trying to compile the stored proc below and I am getting an error, "ERROR SQL0104N An unexpected token "WITH" was found following "User_ID =...
4
by: barmatt80 | last post by:
I am stumped on the error reporting with sql server. I was told i need to return @SQLCode(code showing if successful or not) and @ErrMsg(and the message returned). I am clueless on this. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.