Thursday 15 January 2015

search - MySQL stored procedure - pass SELECT as a parameter -


Text after "

Does the SELECT statement ready through this stored procedure pass and it is possible to execute? Appropriately - is it possible to create a position of WHERE dynamically in the SELECT statement in a stored MySQL process?

We want to enable variable search in 40 columns. This means there are 40 * 40 combinations, which we can do hardcodes (and can get some solution), but I also think that the data set of cruel force is around thousands of records.

It is entirely possible here that includes the example of what you are talking about and Uses updates, not selects, but this is the basic form of things you need to do. The purpose of this process is to dynamically insert or update the logic for this query whether the insert or update statement will be either. I hope this example helps you to tell that what you want to do is possible:

  'test' when the DROP process is present. 'Upsert_event`; Delimiter $$ create process `test`.`upsert_event` (UPDATE_PARAM VARCHAR (10240), INSERT_PARAM in VARCHAR (in 10240), REMOTE_ID_STRING in VARCHAR (255)) announcement event_id_value INT (12) default 0; DECLARE id_for_update INT (12) DEFAULT 0; # This temporary table allows results not to be given and if temp table creation does not exist, then result_set (event_id integer (12) default 0, is_inserted tinyint (1) default 0) engine = memory in my version of mysql A bug gets around; Select `id_for_update test events` from` `events````````````` event` .`remote_id` = REMOTE_ID_STRING limit1; # Variable id_for_update Announcement if need be! = 0 Then the #Update section that you need is set @query_as_string = concat ('update `events` set', UPDATE_PARAM, 'where` events'.'remote_id` =' build, REMOTE_ID_STRING); Prepare the statement sent from @query_as_string; EXECUTE statement_1; DEALLOCATE statement statement; Enter `result_ size` (event_id, i-indented) values ​​(id_for_update, 0); Else insert section is that you need to set @query_as_string = concat ('insert in `` events`, INSERT_PARAM) #build; Prepare the statement sent from @query_as_string; EXECUTE statement_1; DEALLOCATE statement statement; # Set the value for the update / insert ID and select it as the SELECT LAST_INSERT_ID () INTO event_id_value; Insert values ​​in `result_ir` (event_id, is_inserted) (event_id_value, 1); end if; Choose 'result_set' where 1 border1; Drop table result_set; END $$ DELIMITER;    

No comments:

Post a Comment