473,326 Members | 2,076 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,326 software developers and data experts.

Stored procedure help please

hello,

I'm writing a stored procedure and i need some help:
Expand|Select|Wrap|Line Numbers
  1. DELIMITER //
  2. DROP PROCEDURE IF EXISTS INSERTDBsb//
  3.  
  4.  
  5. CREATE PROCEDURE INSERTDBsb( p_email varchar(160) , p_tablename varchar(80) , p_timestamp varchar(30) , p_data double)
  6. BEGIN
  7. DECLARE maxid bigint(20);
  8. DECLARE userid bigint(20);
  9. SELECT max(_id) into maxid from p_tablename;
  10.  
  11. SELECT id into userid FROM users WHERE email= p_email LIMIT 1;
  12.  
  13. INSERT INTO p_tablename (_id, _user_id, _datetime, timestamp, data)
  14. VALUES
  15. (maxid, userid, NOW(), p_timestamp, p_data);
  16.  
  17. END;
  18.  
  19. //
  20.  
This runs, however for SELECT max(_id) into maxid from p_tablename; it will take p_tablename literally and not the variable.
Same thing further on, any help would be appreciated, thanks in advance
Jun 20 '08 #1
7 5389
Atli
5,058 Expert 4TB
Hi.

Take a look at this article.

To sum it up:
Expand|Select|Wrap|Line Numbers
  1. CREATE PROCEDURE colavg(IN tbl CHAR(64), IN col CHAR(64))
  2. BEGIN
  3. SET @s = CONCAT('SELECT AVG(' , col , ') FROM ' , tbl);
  4. PREPARE stmt FROM @s;
  5. EXECUTE stmt;
  6. END;
  7.  
Jun 20 '08 #2
I tried that too before posting, but I kinda beleived it not the way to go:
Expand|Select|Wrap|Line Numbers
  1. DELIMITER //
  2. DROP PROCEDURE IF EXISTS INSERTDBsb//
  3.  
  4. CREATE PROCEDURE INSERTDBsb( IN p_email varchar(160) , IN p_tablename varchar(80) , IN p_timestamp varchar(30) , IN p_data double)
  5. BEGIN
  6. DECLARE maxid bigint(20);
  7. DECLARE userid bigint(20);
  8. SET @s = CONCAT('SELECT max(_id) into maxid from ', p_tablename);
  9. PREPARE stmt FROM @s;
  10. EXECUTE stmt;
  11.  
  12. SET @s = CONCAT('SELECT id into userid from users WHERE email = ', p_email, ' LIMIT 1');
  13. PREPARE stmt FROM @s;
  14. EXECUTE stmt;
  15.  
  16. SET @s = CONCAT('INSERT INTO (' , p_tablename , ') VALUES ( ' , maxid, ',' , userid, ',' , NOW(), ',' , p_timestamp, ',', p_data, ')');
  17. PREPARE stmt FROM @s;
  18. EXECUTE stmt;
  19.  
  20. END;
  21. //
  22.  
my error is : _mysql_exceptions.OperationalError: (1327, 'Undeclared variable: maxid')

Is there some way to fix this so this works?
Jun 20 '08 #3
Atli
5,058 Expert 4TB
The problem is that the 'maxid' variable is not recognized inside the dynamic query.

Try doing something like:
Expand|Select|Wrap|Line Numbers
  1. SET @query = CONCAT('SELECT @maxid:=max(id) FROM ', p_table);
  2. PREPARE stmt FROM @query;
  3. EXECUTE stmt;
  4.  
  5. SET @query = CONCAT('INSERT INTO ', p_table, ' VALUES(@maxid)');
  6. PREPARE stmt FROM @query;
  7. EXECUTE stmt;
  8.  
P.S.
Please us [code] tags when posting code examples.
Jun 21 '08 #4
ok so in the end I have this:
Expand|Select|Wrap|Line Numbers
  1. DELIMITER //
  2. DROP PROCEDURE IF EXISTS INSERTDBsb//
  3. CREATE PROCEDURE INSERTDBsb( IN p_email varchar(160) , IN p_tablename varchar(80) , IN p_timestamp varchar(30) , IN p_data double)
  4. BEGIN
  5. DECLARE maxid bigint(20);
  6. DECLARE userid bigint(20);
  7. SET @s = CONCAT('SELECT @maxid:=max(_id) from ', p_tablename);
  8. PREPARE stmt FROM @s;
  9. EXECUTE stmt;
  10.  
  11. SET @s = CONCAT('SELECT @userid:=id from users WHERE email = ', p_email, ' LIMIT 1');
  12. PREPARE stmt FROM @s;
  13. EXECUTE stmt;
  14.  
  15. SET @s = CONCAT('INSERT INTO (' , p_tablename , ') VALUES ( ' , @maxid, ',' , @userid, ',' , NOW(), ',' , p_timestamp, ',', p_data, ')');
  16. PREPARE stmt FROM @s;
  17. EXECUTE stmt;
  18.  
  19. END;
  20. //
  21.  
when I try to run it threw python:
Expand|Select|Wrap|Line Numbers
  1.         args = {}
  2.         args[0] = self.params['email']
  3.         args[1] = wholeTableName
  4.         args[2] = l_timestamp
  5.         args[3] = l_data    
  6.         cursor.callproc('INSERTDBsb',args)
  7.  
i get this error

_mysql_exceptions.ProgrammingError: (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 '1' at line 1")

thanks alot so far for the help I feel like I'm getting closer to the solution
Jun 21 '08 #5
Atli
5,058 Expert 4TB
That last INSERT statement is a little weird.

The table name should not be encapsulated.
The @variables can be called withing the query. They don't need to be CONCAT'ed.
Also, the NOW() function should not be a parameter in the CONCAT, it should be in the string.

So, it should look more like:
Expand|Select|Wrap|Line Numbers
  1. SET @s = CONCAT('INSERT INTO ' , p_tablename , ' VALUES (@maxid, @userid, NOW(),' , p_timestamp, ',', p_data, ')');
  2.  
Jun 21 '08 #6
thanks alot, I just tried it all, and even though that would have brought up an error later on, there must be one earlier, truth is that i'm almost sure that when the first statement is executed there is a problem

by just writing this minimalist procedure

Expand|Select|Wrap|Line Numbers
  1. DELIMITER //
  2. DROP PROCEDURE IF EXISTS INSERTDBsb//
  3. # MySQL returned an empty result set (i.e. zero rows).
  4.  
  5. CREATE PROCEDURE INSERTDBsb( IN p_email varchar(160) , IN p_tablename varchar(80) , IN p_timestamp varchar(30) , IN p_data double)
  6. BEGIN
  7. DECLARE maxid bigint(20);
  8. DECLARE userid bigint(20);
  9. SET @s = CONCAT('SELECT @maxid:=max(_id) from ', p_tablename);
  10. PREPARE stmt FROM @s;
  11. EXECUTE stmt;
  12.  
  13.  
  14.  
  15. END;
  16. //
  17.  
and then calling that like I do I'm getting the same error : (_mysql_exceptions.ProgrammingError: (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 '1' at line 1")), hence I believe that this line
Expand|Select|Wrap|Line Numbers
  1. SET @s = CONCAT('SELECT @maxid:=max(_id) from ', p_tablename);
  2. PREPARE stmt FROM @s;
  3. EXECUTE stmt;
  4.  
does not work for some reason, removing it of coarse resolves the problem

do you think that it see what p_tablename is?
Jun 21 '08 #7
Atli
5,058 Expert 4TB
That's weird. I can't see any problem with the query. It should be working.

Try hard-coding the table name into the query instead of adding it dynamically. Just for debugging purposes, to rule out that the parameter is somehow responsible.

Also try replacing the "@maxid:=max(_id)" with just "max(_id)". Just to see if that produces the expected result.

Could it be that Python is somehow messing up the @variable system?
Jun 21 '08 #8

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

Similar topics

2
by: Matt | last post by:
I want to exexute stored procedure in ASP, but it has error "Microsoft VBScript compilation (0x800A0401) Expected end of statement" on line (1). The stored procedure "sp_emp" contain "select *...
7
by: Douglas Buchanan | last post by:
I can't seem to open SQLS2k Stored Procedures in the IDE I am running MDE 2003 Version 7.1.3088 I have a MSDN professional subscription and did a complete install of vs.net Help explains how...
6
by: Wojciech Wendrychowicz | last post by:
Hello to All, I'm trying to retrieve records from AS/400 in an VBA application. So, I've made an RPG program, then a stored procedure wchich calls that RPG program, and finally some VBA code to...
2
by: singlal | last post by:
Hi, my question was not getting any attention because it moved to 2nd page; so posting it again. Sorry for any inconvenience but I need to get it resolved fast. Need your help! ...
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...
2
by: Roger | last post by:
I have a stored procedure running on DB2 V7 Z/os calling a COBOL program to do some inserts. The stored procedure have 3 input columns and one column is of varchar(32648) The stored procedure is...
9
by: fniles | last post by:
I am using VB.NET 2003 and SQL2000 database. I have a stored procedure called "INSERT_INTO_MYTABLE" that accepts 1 parameter (varchar(10)) and returns the identity column value from that table....
1
by: sheenaa | last post by:
Hello Members, I m creating my application forms in ASP.Net 2005 C# using the backend SQL Server 2005. What i have used on forms :: ? On my first form i have used some...
0
by: SOI_0152 | last post by:
Hi all! Happy New Year 2008. Il hope it will bring you love and happyness I'm new on this forum. I wrote a stored procedure on mainframe using DB2 7.1.1 and IBM language c. Everything works...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.