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

---------------Replace 的用法 -----------------------------------
 declare @StrSql  varchar(2000)
 declare @ContractIds  varchar(2000)
set @ContractIds='123,3,66|444,33,666'

SELECT @StrSql = ';WITH [temp_tab] AS (SELECT '
                                 + Replace( Replace(@ContractIds, ',', ' AS ContractId,'), '|', ' AS ClassScheduleId union all SELECT ' )
                                 + ' AS ClassScheduleId) '


 print @StrSql 

--实际输出 ;WITH [temp_tab] AS (SELECT 123 AS ContractId,3 AS ContractId,66 AS ClassScheduleId union all SELECT 444 AS ContractId,33 AS ContractId,666 AS ClassScheduleId) 

--按照自己的理解,应该是下面这样的呀!

Replace( Replace(@ContractIds, ',', ' AS ContractId,'), '|', ' AS ClassScheduleId union all SELECT ' ) + ' AS ClassScheduleId
'123 AS ContractId,3,66 AS ClassScheduleId union all SELECT 444,33,666 AS ClassScheduleId'


求讲解。。。
------解决方案--------------------
 + 'Replace( Replace(@ContractIds, '','', '' AS ContractId,''), ''
------解决方案--------------------
'', '' AS ClassScheduleId union all SELECT '' )'

 Replace( Replace(@ContractIds, ',', ' AS ContractId,'), '
------解决方案--------------------
', ' AS ClassScheduleId union all SELECT ' )

replace 需要用引号引起来,你的那个在select赋值的时候已经使用了