2011-08-13 85 views
1

我調試的代碼存儲過程變量不在MySQL中返回期望值?

create procedure create_view (IN t varchar(50)) 
BEGIN 
    prepare stmt from 'select @cnt= count(weight) from test where url = ?;'; 
    execute stmt using @t; 
    set @base = @cnt /4; 
    set @offset = @cnt/2; 
    set @query = concat('create or replace view view_by_url as select url, weight from test where url = ',@t,' order by weight limit ',@base,' , ',@offset,' ;'); 
    select t as 'param'; 
    select cnt as 'count'; 
    select @base as 'base'; 
    select @offset as 'offset'; 
    select @query as 'query'; 
-- prepare stmt from @query; 
-- execute stmt ; 
END; 
call create_view('a'); 

而@t返回 'a' 的結果集,但@cnt,@base和@Offset沒有。我無法解釋自己爲什麼。你能給我一些幫助嗎?

回答

1

試試單SELECT在存儲過程的結束:

SELECT 
     t as 'param', 
     @cnt as 'count', 
     @base as 'base', 
     @offset as 'offset', 
     @query as 'query'; 
+0

不解決問題。不管怎麼說,還是要謝謝你。 – munch

+0

@munch:聲明瞭@ cnt'?我看到你正在使用一個動態的SQL查詢,你有沒有聲明過它?如果沒有 - 嘗試在查詢之前聲明 – sll

0

這個問題似乎是在SELECT。 '='運算符是比較或類似的東西,而在這種情況下要實現所需的行爲':='應該被使用。

prepare stmt from 'select @cnt= count(weight) from test where url = ?;'; 

此更改使整段代碼正常工作。