select @BranchID=select id from branch where BranchName='财务部'
有错误,应该
select @BranchID=[id] from branch where BranchName='财务部'

解决方案 »

  1.   

    declare @BranchID tiny int
    也有错误,应该
    declare @BranchID tinyint
      

  2.   

    试一下这个吧:
    CREATE PROCEDURE GetBranchStatic
    AS
    declare @BranchID tinyint 
    select sum(case  when branchID=@BranchID then 1 else 0 end )AS total
    from employee 
    select @BranchID=[id] from branch where BranchName='财务部'GO
      

  3.   

    CREATE PROCEDURE GetBranchStatic
    AS
    declare @BranchID tinyint 
    select sum(case when branchID=@BranchID then 1 else 0 end )AS total
    from employee select @BranchID=(select id from branch where BranchName='财务部')
      

  4.   

    我想应该是这样写,才能统计财务部:
    CREATE PROCEDURE GetBranchStatic
    AS
    declare @BranchID tinyint 
    select @BranchID=[id] from branch where BranchName='财务部'
    select sum(case  when branchID=@BranchID then 1 else 0 end ) AS total
    from employee 
    GO
      

  5.   

    谢谢两位. wanyingsong(豌豆)的结果是正确的.还有一个小问题:
    如果想统计结果存在一个以年份为开始名称的表中,比如要将现在的结果
    存在
    2004static表中,怎样写这个存储过程?比如到了2005年,统计的结果则存放在
    2005static表中.再次感谢.马上结分.
      

  6.   

    CREATE PROCEDURE GetBranchStatic
    AS
    --表名称
    declare @tableName=substring(cast(getdate() as varchar),0,4)+'static'declare @BranchID tinyint 
    select @BranchID=[id] from branch where BranchName='财务部'
    select sum(case  when branchID=@BranchID then 1 else 0 end ) AS total
    from @tableName 
    GO
      

  7.   

    CREATE PROCEDURE GetBranchStatic
    AS
    --表名称
    declare @tableName=substring(cast(getdate() as varchar(20)),0,4)+'static'declare @BranchID tinyint 
    select @BranchID=[id] from branch where BranchName='财务部'
    select sum(case  when branchID=@BranchID then 1 else 0 end ) AS total
    from @tableName 
    GO