USE [beone]
GO
/****** 对象:  StoredProcedure [dbo].[empCusInd]    脚本日期: 03/04/2011 16:26:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: GM
-- Create date: 2011-2-21
-- Description: 客户行业统计
-- =============================================
CREATE PROCEDURE [dbo].[empCusInd](
@sqlAppend  varchar(8000)
)
AS
BEGIN
--申明字符串变量,以供动态拼装
declare @sql varchar(max),@sql1 varchar(8000)
--将客户行业列放入临时表
set @sql1='select typ_name as sta_name into ##tempCusInd
from cus_cor_cus left join type_list on cor_ind_id=typ_id where cor_isdelete=''1'' group by typ_name order by typ_name desc'

if object_id('tempdb..##tempCusInd') is not null
begin
drop table ##tempCusInd
end
exec(@sql1)--拼装SQL命令
set @sql = 'select se_no,max(se_code),max(se_name) as head'
--将客户行业旋转为表头
select @sql = @sql + 
case when sta_name is not null then 
', sum(case typ_name when '''+sta_name+''' then 1 else 0 end) ['+sta_name+']'
else
', sum(case when typ_name is null then 1 else 0 end) as 未选择' 
end from (select sta_name from ##tempCusInd)as a 
--加上合计
select @sql = @sql+',
count(cor_code)as 合计 
from cus_cor_cus inner join sal_emp on cor_se_no=se_no left join type_list on cor_ind_id=typ_id 
where cor_isdelete=''1'' ' + @sqlAppend
+ ' group by se_no with rollup'
--print(@sql)
exec(@sql)
END  
还请大神们多多指教!有哪些注意事项,谢谢存储过程 sqlmysql

解决方案 »

  1.   

    建议参考MYSQL的官方免费手册,先自己尝试修改一下,然后有什么具体的问题可将你自己写的语句和错误信息一同贴出以供讨论。
      

  2.   

    我是菜鸟啊....我也不知道如何改成Mysql的存储过程的!还希望谁会的帮个忙!非常感谢
      

  3.   

    自己看下mysql的语法  慢慢改
      

  4.   

    扫描一遍脚本之后,建议参照MYSQL动态SQL + 预处理语句整改case 与  MYSQL动态SQL使用实例 :http://my.csdn.net/my/code/detail/58988