ALTER PROCEDURE db_owner.FinanceStat
@year int
AS
declare @i int
declare @raccount float
declare @caccount float
declare @account float 
         declare @sql  varchar(500)
set @i=1
 create table #oo(mon int,raccount float,caccount float,account float)
     while @i<13
     begin 
   
set @raccount=SELECT ISNULL(SUM(CASE type WHEN 0 THEN jine ELSE 0 END), 0) AS raccount where status=0 and year(addtime)=@year  and month(addtime)=@i
    set @caccount=SELECT ISNULL(SUM(CASE type WHEN 1 THEN jine ELSE 0 END), 0) AS raccount  where status=0 and year(addtime)=@year and month(addtime)=@i
set @account=SELECT ISNULL(SUM(CASE type WHEN 0 THEN jine ELSE - jine END), 0) AS account where status=0 and year(addtime)=@year and month(addtime)=@i
  insert into #oo(mon,raccount,caccount,account) values(@i,@raccount,@caccount,@account)
 set @i=@i+1
  end 
 
set @sql='select * from #oo'
exe(@sql)  
drop table #oo

解决方案 »

  1.   

    create PROCEDURE db_owner.FinanceStat
    @year int
    AS
    begin
      declare @i int
      declare @raccount float
      declare @caccount float
      declare @account float 
        declare @sql  varchar(500)
      set @i=1
      create table #oo(mon int,raccount float,caccount float,account float)
        while @i<13
        begin 
          SELECT @raccount=ISNULL(SUM(CASE type WHEN 0 THEN jine ELSE 0 END), 0) where status=0 and year(addtime)=@year  and month(addtime)=@i
            SELECT @caccount=ISNULL(SUM(CASE type WHEN 1 THEN jine ELSE 0 END), 0)  where status=0 and year(addtime)=@year and month(addtime)=@i
          SELECT @account=ISNULL(SUM(CASE type WHEN 0 THEN jine ELSE - jine END), 0) where status=0 and year(addtime)=@year and month(addtime)=@i
          insert into #oo(mon,raccount,caccount,account) values(@i,@raccount,@caccount,@account)
          set @i=@i+1
      end 
     
      set @sql='select * from #oo'
      exec(@sql)  
      drop table #oo
    end
      

  2.   

    lz,请参考语法:
    CREATE PROCEDURE
    创建存储过程,存储过程是保存起来的可以接受和返回用户提供的参数的 Transact-SQL 语句的集合。可以创建一个过程供永久使用,或在一个会话中临时使用(局部临时过程),或在所有会话中临时使用(全局临时过程)。也可以创建在 Microsoft&reg; SQL Server&#8482; 启动时自动运行的存储过程。语法
    CREATE PROC [ EDURE ] procedure_name [ ; number ]
        [ { @parameter data_type }
            [ VARYING ] [ = default ] [ OUTPUT ]
        ] [ ,...n ] [ WITH
        { RECOMPILE | ENCRYPTION | RECOMPILE , ENCRYPTION } ] [ FOR REPLICATION ] AS sql_statement [ ...n ] 
    参数
    procedure_name新存储过程的名称。过程名必须符合标识符规则,且对于数据库及其所有者必须唯一。有关更多信息,请参见使用标识符。要创建局部临时过程,可以在 procedure_name 前面加一个编号符 (#procedure_name),要创建全局临时过程,可以在 procedure_name 前面加两个编号符 (##procedure_name)。完整的名称(包括 # 或 ##)不能超过 128 个字符。指定过程所有者的名称是可选的。;number是可选的整数,用来对同名的过程分组,以便用一条 DROP PROCEDURE 语句即可将同组的过程一起除去。例如,名为 orders 的应用程序使用的过程可以命名为 orderproc;1、orderproc;2 等。DROP PROCEDURE orderproc 语句将除去整个组。如果名称中包含定界标识符,则数字不应包含在标识符中,只应在 procedure_name 前后使用适当的定界符。@parameter过程中的参数。在 CREATE PROCEDURE 语句中可以声明一个或多个参数。用户必须在执行过程时提供每个所声明参数的值(除非定义了该参数的默认值)。存储过程最多可以有 2.100 个参数。使用 @ 符号作为第一个字符来指定参数名称。参数名称必须符合标识符的规则。每个过程的参数仅用于该过程本身;相同的参数名称可以用在其它过程中。默认情况下,参数只能代替常量,而不能用于代替表名、列名或其它数据库对象的名称。有关更多信息,请参见 EXECUTE。 data_type参数的数据类型。所有数据类型(包括 text、ntext 和 image)均可以用作存储过程的参数。不过,cursor 数据类型只能用于 OUTPUT 参数。如果指定的数据类型为 cursor,也必须同时指定 VARYING 和 OUTPUT 关键字。有关 SQL Server 提供的数据类型及其语法的更多信息,请参见数据类型。 说明  对于可以是 cursor 数据类型的输出参数,没有最大数目的限制。
    VARYING指定作为输出参数支持的结果集(由存储过程动态构造,内容可以变化)。仅适用于游标参数。default参数的默认值。如果定义了默认值,不必指定该参数的值即可执行过程。默认值必须是常量或 NULL。如果过程将对该参数使用 LIKE 关键字,那么默认值中可以包含通配符(%、_、[] 和 [^])。OUTPUT表明参数是返回参数。该选项的值可以返回给 EXEC[UTE]。使用 OUTPUT 参数可将信息返回给调用过程。Text、ntext 和 image 参数可用作 OUTPUT 参数。使用 OUTPUT 关键字的输出参数可以是游标占位符。n表示最多可以指定 2.100 个参数的占位符。{RECOMPILE | ENCRYPTION | RECOMPILE, ENCRYPTION}RECOMPILE 表明 SQL Server 不会缓存该过程的计划,该过程将在运行时重新编译。在使用非典型值或临时值而不希望覆盖缓存在内存中的执行计划时,请使用 RECOMPILE 选项。ENCRYPTION 表示 SQL Server 加密 syscomments 表中包含 CREATE PROCEDURE 语句文本的条目。使用 ENCRYPTION 可防止将过程作为 SQL Server 复制的一部分发布。说明  在升级过程中,SQL Server 利用存储在 syscomments 中的加密注释来重新创建加密过程。
    FOR REPLICATION指定不能在订阅服务器上执行为复制创建的存储过程。.使用 FOR REPLICATION 选项创建的存储过程可用作存储过程筛选,且只能在复制过程中执行。本选项不能和 WITH RECOMPILE 选项一起使用。AS指定过程要执行的操作。sql_statement过程中要包含的任意数目和类型的 Transact-SQL 语句。但有一些限制。n是表示此过程可以包含多条 Transact-SQL 语句的占位符。
      

  3.   

    create PROCEDURE db_owner.FinanceStat
    @year int
    AS
    begin
      declare @i int
      declare @sql varchar(500)
      set @i=1
      
      create table #oo(mon int,raccount float,caccount float,account float)
      
      while @i<13
      begin 
        insert into #oo(mon,raccount,caccount,account)
        SELECT 
            @i,
            @raccount=ISNULL(SUM(CASE type WHEN 0 THEN jine ELSE 0 END), 0),
            @caccount=ISNULL(SUM(CASE type WHEN 1 THEN jine ELSE 0 END), 0),
            @account=ISNULL(SUM(CASE type WHEN 0 THEN jine ELSE - jine END), 0)
        where 
            status=0 and year(addtime)=@year and month(addtime)=@i
            
        set @i=@i+1
      end 

      set @sql='select * from #oo'
      exe(@sql)  

      drop table #oo
    end
      

  4.   

    set @raccount=SELECT ISNULL(SUM(CASE type WHEN 0 THEN jine ELSE 0 END), 0) AS raccount where status=0 and year(addtime)=@year  and month(addtime)=@i
    改為SELECT @raccount = ISNULL(SUM(CASE type WHEN 0 THEN jine ELSE 0 END), 0) AS raccount From TableName where status=0 and year(addtime)=@year  and month(addtime)=@iTableName改為你自己的表名,其余的類似
      

  5.   

    ALTER PROCEDURE db_owner.FinanceStat
    @year int
    AS
    declare @i int
    declare @raccount float
    declare @caccount float
    declare @account float 
             declare @sql  varchar(500)
    set @i=1
     create table #oo(mon int,raccount float,caccount float,account float)
         while @i<13
         begin 
       
    set @raccount=SELECT ISNULL(SUM(CASE type WHEN 0 THEN jine ELSE 0 END), 0) AS raccount where status=0 and year(addtime)=@year  and month(addtime)=@i
        SELECT  @caccount=ISNULL(SUM(CASE type WHEN 1 THEN jine ELSE 0 END), 0) AS raccount  where status=0 and year(addtime)=@year and month(addtime)=@i
    SELECT @account=ISNULL(SUM(CASE type WHEN 0 THEN jine ELSE - jine END), 0) AS account where status=0 and year(addtime)=@year and month(addtime)=@i
      insert into #oo(mon,raccount,caccount,account) values(@i,@raccount,@caccount,@account)
     set @i=@i+1
      end 
     
    set @sql='select * from #oo'
    exe(@sql)  
    drop table #oo
      

  6.   

    如果@raccount, @caccount, @account事從同一個表中查詢的話
    SELECT @raccount=ISNULL(SUM(CASE type WHEN 0 THEN jine ELSE 0 END), 0) where status=0 and year(addtime)=@year  and month(addtime)=@i
            SELECT @caccount=ISNULL(SUM(CASE type WHEN 1 THEN jine ELSE 0 END), 0)  where status=0 and year(addtime)=@year and month(addtime)=@i
          SELECT @account=ISNULL(SUM(CASE type WHEN 0 THEN jine ELSE - jine END), 0) where status=0 and year(addtime)=@year and month(addtime)=@i
          insert into #oo(mon,raccount,caccount,account) 
    改為
    insert into #oo(mon,raccount,caccount,account) 
    Select 
    @i
    ISNULL(SUM(CASE type WHEN 0 THEN jine ELSE 0 END), 0),
    ISNULL(SUM(CASE type WHEN 1 THEN jine ELSE 0 END), 0),
    ISNULL(SUM(CASE type WHEN 0 THEN jine ELSE - jine END), 0)
    From TableName 
    where status=0 and year(addtime)=@year  and month(addtime)=@iTableName改為你的表名,你的語句種掉了表名。
      

  7.   

    汗一个,一直都缺少了from的表名。
      

  8.   

    刚才帖错了
    create PROCEDURE db_owner.FinanceStat
    @year int,
    @sql  varchar(500) output
    AS
    declare @i int
    declare @raccount float
    declare @caccount float
    declare @account float  set @i=1
     create table #oo(mon int,raccount float,caccount float,account float)
         while @i<13
         begin 
       
    set @raccount=SELECT ISNULL(SUM(CASE type WHEN 0 THEN jine ELSE 0 END), 0) AS raccount from raccounts where status=0 and year(addtime)=@year  and month(addtime)=@i
        set @caccount=SELECT ISNULL(SUM(CASE type WHEN 1 THEN jine ELSE 0 END), 0) AS raccount from raccounts where status=0 and year(addtime)=@year and month(addtime)=@i
    set @account=SELECT ISNULL(SUM(CASE type WHEN 0 THEN jine ELSE - jine END), 0) AS account from raccounts where status=0 and year(addtime)=@year and month(addtime)=@i
      insert into #oo(mon,raccount,caccount,account) values(@i,@raccount,@caccount,@account)
     set @i=@i+1
      end 
     
    set @sql='select * from #oo'
    exe(@sql)  
    drop table #oo
      

  9.   

    libin_ftsafe(子陌红尘:当libin告别ftsafe) ( ) 信誉:105    Blog  2007-03-22 10:17:30  得分: 0  
     
     
       汗一个,一直都缺少了from的表名。
      
     
    ----------哈哈,現在才發現。
      

  10.   

    trycreate PROCEDURE db_owner.FinanceStat
    @year int,
    @sql  varchar(500) output
    AS
    declare @i int
    declare @raccount float
    declare @caccount float
    declare @account float  set @i=1
     create table #oo(mon int,raccount float,caccount float,account float)
         while @i<13
         begin 
       insert into #oo(mon,raccount,caccount,account) 
    Select 
    @i,
    ISNULL(SUM(CASE type WHEN 0 THEN jine ELSE 0 END), 0),
    ISNULL(SUM(CASE type WHEN 1 THEN jine ELSE 0 END), 0),
    ISNULL(SUM(CASE type WHEN 0 THEN jine ELSE - jine END), 0)
    From account 
    where status=0 and year(addtime)=@year  and month(addtime)=@i  set @i=@i+1
      end 
     
    set @sql='select * from #oo'
    exec(@sql)  
    drop table #oo
      

  11.   

    另外,還有個筆誤,exec寫錯了。