BEGIN SET @baseStr= "SELECT * FROM tbName WHERE 1=1 "; SET @_where=""; IF 1=1 THEN SET @_where= CONCAT(@_where," AND sourcedomain=\"www.baidu.com\" "); END IF; IF 2=2 THEN SET @_where=CONCAT(@_where," AND userId =4444"); END IF; SET @sentence =CONCAT(@baseStr,@_where) ; -- 連接字符串生成要執行的SQL語句 prepare stmt from @sentence; -- 預編釋一下。 “stmt”預編釋變量的名稱, execute stmt; -- 執行SQL語句 deallocate prepare stmt; -- 釋放資源 END
2.out賦值
SET @_rowCount=0; -- 變量 SET @_count =CONCAT("SELECT COUNT(*) INTO @_rowCount ",@baseStr,@_where); prepare stmt from @_count; execute stmt; deallocate prepare stmt; SET _pageCount=@_rowCount;