冷眼 - 技術(shù)專欄

          態(tài)度決定高度,責(zé)任勝于能力;思想比知識(shí)更重要,成長(zhǎng)比成功更重要!

             :: 首頁(yè) ::  :: 聯(lián)系 ::  :: 管理 ::
            14 隨筆 :: 13 文章 :: 0 評(píng)論 :: 0 Trackbacks

          在MySQL中執(zhí)行動(dòng)態(tài)拼接的SQL語(yǔ)句,有點(diǎn)麻煩,以下是一個(gè)簡(jiǎn)單的例子,經(jīng)過(guò)測(cè)試沒(méi)有問(wèn)題。

          mysql > ?delimiter? //
          mysql
          >??create?procedure?sp_tests(out?itotal?int,?in?scondition?varchar(50))
          ????
          ->?? begin
          ????
          ->?????if?scondition?=?''?then
          ????
          ->?????????set?@sql?=?'select?count(*)?into?@total?from?users';
          ????
          ->?????else
          ????
          ->?????????set?@sql?=?concat('select?count(*)?into?@total?from?users?where?',scondition);
          ????
          ->?????end?if;
          ????
          ->
          ????
          ->?????prepare?s1?from?@sql;
          ????
          ->?????execute?s1;
          ????
          ->?????set?itotal?=?@total;
          ????
          ->??end?//
          ?
          mysql
          >?delimiter?;
          mysql
          >?call?sp_tests(@itotal,'userid=1');
          mysql
          >?select?@itotal;
          posted on 2008-05-16 13:22 冷眼 閱讀(390) 評(píng)論(0)  編輯  收藏 所屬分類: MySQL
          主站蜘蛛池模板: 武陟县| 宜昌市| 徐州市| 周口市| 曲麻莱县| 宝鸡市| 金阳县| 黄梅县| 隆化县| 东明县| 海阳市| 斗六市| 安乡县| 临猗县| 合山市| 九龙县| 宽甸| 林芝县| 石柱| 吴桥县| 乌恰县| 嵊泗县| 旅游| 互助| 呼图壁县| 临潭县| 星座| 惠州市| 商丘市| 锡林浩特市| 榕江县| 贵德县| 兴仁县| 沙洋县| 本溪市| 许昌市| 扎囊县| 忻州市| 德安县| 楚雄市| 察隅县|