How do I set a stored procedure? How can I process following query structure in drupal 7.

set @num := 0, @termid := 0;



SELECT  * FROM (SELECT tid,nid,created,
           @num:=IF(@termid = tid, @num + 1, 1) AS row_number,
           @termid:=tid AS termid .......
It has been solved using.

function  hook_install(){

db_query("CREATE PROCEDURE xyz()

BEGIN
    set @num := 0, @termid := 0; 
    ......................................");