declare @a varchar(100),@s varchar(1000)
set @a = '''北京'',''天津'',''上海'',''重庆'''select @s = replace(@a,''',''' ,''' union all select ''')exec('select '+@s)
---- 
北京
天津
上海
重庆

解决方案 »

  1.   

    -- TRY:
    DECLARE @SQL varchar(8000),@STR varchar(100)
    SET @STR = '北京,天津,上海,重庆'
    SET @SQL = 'SELECT '''
    SET @STR = REPLACE(@STR,',','''address UNION ALL SELECT ''')
    SET @SQL = @SQL + @STR + ''''
    PRINT @SQL
    EXEC (@SQL)
    /*
    address 
    ------- 
    北京
    天津
    上海
    重庆
    */
      

  2.   

    select SUBSTRING ( '北京','天津','上海','重庆' , 2 , 4 ) 
    union all
    select SUBSTRING ( '北京','天津','上海','重庆' , 8 , 4 ) 
    union all
    select SUBSTRING ( '北京','天津','上海','重庆' , 14 , 4 )
    union all
    select SUBSTRING ( '北京','天津','上海','重庆' , 20 , 4 )
      

  3.   

    select 城市名=substring(aa,id+1,2)
    from(
    select aa='''北京'',''天津'',''上海'',''重庆'''
    )a join(
    select id=a.id+b.id+c.id+d.id
    from(
    select id=0 union all select 1
    union all select id=2 union all select 3
    union all select id=4 union all select 5
    union all select id=6 union all select 7
    union all select id=8 union all select 9
    ) a,(
    select id=0 union all select 10
    union all select id=20 union all select 30
    union all select id=40 union all select 50
    union all select id=60 union all select 70
    union all select id=80 union all select 90
    ) b,(
    select id=0 union all select 100
    union all select id=200 union all select 300
    union all select id=400 union all select 500
    union all select id=600 union all select 700
    union all select id=800 union all select 900
    ) c,(
    select id=0 union all select 1000
    union all select id=2000 union all select 3000
    union all select id=4000 union all select 5000
    union all select id=6000 union all select 7000
    union all select id=8000 union all select 9000
    ) d
    )b on substring(','+aa,id,1)=','
    order by id
      

  4.   


    alter function f_test(@sql sysname)
    returns @t table(name char(20))
    begin
    declare @i int
    declare @j char(20)
    set @i = charindex(',',@sql,1)
    while @i >0 
    begin 
    set @j = substring(@sql,1,@i-1)
    set @sql = substring(@sql,@i+1,len(@sql) - @i)
    insert @t(name) values(@j)
    set @i = charindex(',',@sql,1)
    end
    insert @t(name) values(@j)
    return
    enddeclare @sql sysname
    set @sql = '北京'+','+'天津'+','+'上海'+','+'重庆'select * from dbo.f_test(@sql)
      

  5.   

    create  function StringIndex(@ReturnValue varchar(8000),@Spliter varchar(10),@i integer)
    RETURNS varchar(8000)
    as
    begin
    declare @ integer
    set @ = 0
    while (charindex(@Spliter,@ReturnValue) > 0) 
    begin
       set @=@+1
       if @ > @i
          break
       if @ > 1
          set @ReturnValue = right(@ReturnValue ,len(@ReturnValue)-charindex(@Spliter,@ReturnValue))
    end
    return case when charindex(@Spliter,@ReturnValue)>1 
                     then left(@ReturnValue, charindex(@Spliter,@ReturnValue)-1)  
                else 
                     null 
                 end
    endgo
    declare @a varchar(100)
    set @a='avvvvvv,bc,def,ghij'select dbo.StringIndex(@a,',',b.id) as f
    from  
    (
    select 1 as id
    union all
    select 2
    union all
    select 3
    union all
    select 4
    union all
    select 5
    union all
    select 6
    union all
    select 7
    union all
    select 8
    ) b
    where dbo.StringIndex(@a,',',b.id) is not null