If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > stored procedure for display table data( select * from table name)

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-07-11, 05:59
rohitamitpathak rohitamitpathak is offline
Registered User
 
Join Date: Feb 2011
Location: india- new delhi
Posts: 31
stored procedure for display table data( select * from table name)

good afternoon sir,
i am trying to create a stored procedure which is dynamically show the detail of table..
for showing a table we use select * from tablename;
i want to make it happen with help of stored procdure where u pass the table name as a IN parameter and u will display the table data - i wrote this code for creating procedure

DELIMITER $$
DROP PROCEDURE IF EXISTS `zero`.`gettablevalues`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `gettablevalues`(IN table_name varchar(100))
BEGIN
select * from table_name;
END$$

DELIMITER ;
i know i can not use IN parameter variable like this, ( i should use this with = operator) , what i write i m not getting... can any body help me..
------------> i got the error that table is not exist in database... whrere i m doing wrong.. pls let me know my mistake

Last edited by rohitamitpathak; 03-07-11 at 07:57.
Reply With Quote
  #2 (permalink)  
Old 03-07-11, 10:01
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 623
I understand what you are trying to do but if you look at performance this is going to create an overhead. Every call to the stored procedure is going to place information onto the stack call the stored procedure, dynamically generate the SQL, parse the statement and finally execute it.

If this is called once this will be negligible, however, if this is called many times then you could run into performance issues. You should access the data directly i.e. SELECT * FROM <tablename> rather than doing it through a stored procedure.

In any case, the code for creating this is:

Code:
DROP PROCEDURE IF EXISTS aProc;
DELIMITER $$
CREATE PROCEDURE aProc(tableName VARCHAR(30))
BEGIN
	SET @s = CONCAT('SELECT * FROM ', tableName);
	PREPARE stmt FROM @s;
	EXECUTE stmt;
	DEALLOCATE PREPARE stmt;
END
$$
DELIMITER ;
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #3 (permalink)  
Old 03-08-11, 01:37
rohitamitpathak rohitamitpathak is offline
Registered User
 
Join Date: Feb 2011
Location: india- new delhi
Posts: 31
thank you so much

thank you so much sir, thank you so much,
it work fine... sir can u suggest me any book or any site to learn about mysql stored procedure syntax and semantics...
Reply With Quote
  #4 (permalink)  
Old 03-08-11, 01:39
rohitamitpathak rohitamitpathak is offline
Registered User
 
Join Date: Feb 2011
Location: india- new delhi
Posts: 31
thnk u so much sir

Quote:
Originally Posted by it-iss.com View Post
I understand what you are trying to do but if you look at performance this is going to create an overhead. Every call to the stored procedure is going to place information onto the stack call the stored procedure, dynamically generate the SQL, parse the statement and finally execute it.

If this is called once this will be negligible, however, if this is called many times then you could run into performance issues. You should access the data directly i.e. SELECT * FROM <tablename> rather than doing it through a stored procedure.

In any case, the code for creating this is:

Code:
DROP PROCEDURE IF EXISTS aProc;
DELIMITER $$
CREATE PROCEDURE aProc(tableName VARCHAR(30))
BEGIN
	SET @s = CONCAT('SELECT * FROM ', tableName);
	PREPARE stmt FROM @s;
	EXECUTE stmt;
	DEALLOCATE PREPARE stmt;
END
$$
DELIMITER ;
it woks fine, i m looking for exactly this ....thank you
Reply With Quote
Reply

Tags
mysql stored procedure

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On