字符不够长!你不要浪费字符:CREATE        PROCEDURE pro_tanwei_sel_total_sum_test   @tanweileixing char(8)  AS DECLARE @sql varchar(8000)
SET @sql = 'select '
   SELECT @sql = @sql + 'sum(tanweishunum) as 合计 '      SELECT @sql = @sql + ',sum(case qishu when''一期'' then tanweishunum else 0 end) as 一期合计'                        
                                                                                         
                             
 
   SELECT @sql = @sql + ',sum (case  when zhanqu=''' + zhanqu + ''' and tanweileixing<>''按面积计算'' then tanweishunum when zhanqu=''' + zhanqu + ''' and tanweileixing=''按面积计算'' then pingfangmi else 0 end) ['+ zhanqu+'('+qishu+')]'
        FROM (SELECT DISTINCT zhanqu,qishu FROM department where  qishu='一期') a order by qishu desc
  SELECT @sql = @sql + ',sum(case qishu when''二期'' then tanweishunum else 0 end) as 二期合计'  SELECT @sql = @sql + ',sum (case  when zhanqu=''' + zhanqu + ''' and tanweileixing<>''按面积计算'' then tanweishunum when zhanqu=''' + zhanqu + ''' and tanweileixing=''按面积计算'' then pingfangmi else 0 end) as ['+ zhanqu + '('+qishu+')]'
  FROM (SELECT DISTINCT zhanqu,qishu  FROM department where   qishu='二期') b order by qishu desc
  SELECT @sql = @sql + ',sum(case qishu when''新馆试用'' then tanweishunum else 0 end) as 新馆试用合计'  SELECT @sql = @sql + ',sum (case  when zhanqu=''' + zhanqu + ''' and tanweileixing<>''按面积计算'' then tanweishunum when zhanqu=''' + zhanqu + ''' and tanweileixing=''按面积计算'' then pingfangmi else 0 end) as ['+ zhanqu + '('+qishu+')]' FROM (SELECT DISTINCT zhanqu,qishu
                FROM department where   qishu='新馆试用') c order by qishu desc
 
   
print @sql + '  from department where  tanweixingzhi=''' + @tanweileixing +''''
exec(@sql + '  from department where  tanweixingzhi=''' + @tanweileixing +'''')GO