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 > how to dynamically build select statements in a stored procedure?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-13-09, 15:09
ozzii ozzii is offline
Registered User
 
Join Date: Mar 2007
Posts: 194
how to dynamically build select statements in a stored procedure?

Is it possible to dynamically build a select statements in a stored procedure?

I have the following procedure:

Code:
CREATE PROCEDURE count_books(IN inSearchString TEXT)
BEGIN
      PREPARE statement FROM
	 "SELECT COUNT(DISTINCT books.book_id) 
	       FROM tbl_books as books
         INNER JOIN tbl_book_search AS bk_search 
                ON bk_search.book_id = books.book_id
         WHERE  (books.book_status = 'Approved')
         IF (? <> "") THEN
                 AND MATCH (bk_search.book_title, bk_search.book_desc)
                     AGAINST (?)
         END IF
         ";

     SET @p1 = inSearchString;
     
     EXECUTE statement USING @p1, @p1;
END
but keep getting the following error message:
Reply With Quote
  #2 (permalink)  
Old 03-13-09, 16:35
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Quote:
Is it possible to dynamically build a select statements in a stored procedure?
You can't build select statement dynamically in MySQL stored procs. This is likely to change in future versions. Why are you using a stored proc to do a simple query? Far better to just build the query in the calling program and then run the select from there. Your variable names also look like Sybase variables - MySQL does not require the @.
Reply With Quote
Reply

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