日期:2014-05-16  浏览次数:20901 次

MYsql如何像SqlServer一样在查询分析器中执行多行语句
SQL code

use pubs

declare @fid int 
declare @milestone int
declare @count int
declare @val int

set @fid = 1
set @milestone = 1


truncate table milestone

--得到总的milestone数
select  @count = count(1) / 10000
from    thread
where   fid = @fid

--第一个milestone
select top 1
        @val = lastpost
from    thread with ( nolock )
where   fid = @fid
        and lastpost not in ( select top 10000
                                        lastpost
                              from      thread with ( nolock )
                              where     fid = @fid
                              order by  lastpost desc )
order by lastpost desc
insert  into milestone
        ( fid, milestone, lastpostdesc )
values  ( @fid, @milestone, @val )
set @milestone = @milestone + 1

while ( @milestone <= @count ) 
    begin    
        select top 1
                @val = lastpost
        from    thread with ( nolock )
        where   fid = @fid
                and lastpost < @val
                and lastpost not in ( select top 10000
                                                lastpost
                                      from      thread with ( nolock )
                                      where     fid = @fid
                                                and lastpost < @val
                                      order by  lastpost desc )
        order by lastpost desc
        
        insert  into milestone
                ( fid, milestone, lastpostdesc )
        values  ( @fid, @milestone, @val )    
        set @milestone = @milestone + 1
    end





在SqlServer中我们常常合并多行语句然后直接执行,但是在mysql中如何执行以上语句呢?
我是想直接在代码里边执行,例如:
mySqlHelper.ExecuteQuery("这里是上面的sql语句");

------解决方案--------------------
MYSQL中无法实现这种过程式语句,只能在存储过程中使用。
如果是一般的多条SQL语句则可以通过分号来分隔即
select now(); select 1+1 ;
------解决方案--------------------
mysql是不能执行多条语句的。每条语句要分号隔开的。
mysql要实现此功能必须在存储过程中实现的。