从变量中分隔逗号分隔值

时间:2016-08-10 08:37:51

标签: sql-server

我需要预期输出中显示的脚本,但无论我做什么,无论我使用什么,我最终得到当前输出中显示的脚本。

mytest.specialities[0].name 

当前输出

TypeError: Cannot read property 'name' of undefined

预期输出

mytest.specialities.length > 0 ? mytest.specialities[0].name : '(some default value)';

5 个答案:

答案 0 :(得分:1)

适用于以逗号分割的任何数据,并按照您的意愿行事。

DECLARE @query varchar(max),
    @col  varchar(250) = 'd1,d2,d3', @word varchar(250) =''


    Set @query = 'INSERT INTO [dbo].[MYTABLE](
                '+@col+'
                )'
    while CHARINDEX( ',', @col)> 1
    BEGIN
    set @word = SUBSTRING(@col, 0,CHARINDEX(',', @col))
    set @col = SUBSTRING(@col,CHARINDEX( ',', @col)+1, LEN(@col))
    set @query = @query +


                ' SELECT  ''~N'' AS ['+@word+']'
    END
    set @query = @query +


                ' SELECT  ''~N'' AS ['+@col+']'

    print @query

答案 1 :(得分:1)

您的表输出错误,此类解决方案无法扩展,请尝试简化您的架构以避免此问题。

在高水平,这可以通过以下方式实现 1.splitting逗号分隔值将返回列列表 2.然后将列转换为行..

演示:

我使用了其中一个分割字符串functions from here ..

--create table #temp
--(
--d1 varchar,
--d2 varchar,
--d3 varchar
--)


declare @q varchar(10)
set @q='d1,d2,d3'

;with cte
as
(select * from
[dbo].[SplitStrings_Numbers](@q,',')
)
insert into #temp
select 
max(case when item='d1' then item end)  as d1,
max(case when item='d2' then item end)  as d2,
max(case when item='d3' then item end)  as d3
from cte

答案 2 :(得分:1)

    DECLARE @query varchar(max),
`@col  varchar(250) = 'd1,d2,d3',`
`@col1  varchar(250) = ''`

`Set @query = 'INSERT INTO [dbo].[MYTABLE](`
                `   '+@col+'`
              `     )'    `          


`SELECT @col=COALESCE(@col+',','')`
` WHILE(CHARINDEX(',',@col) > 0)`
<br/>
 `BEGIN`


`SET @col1= left(@col, charindex(',', @col+',')-1)`
<br/>
`set  @query=@query+'SELECT  ''~N'' AS ['+@col1+'],'`
<br/>
`IF(CHARINDEX(',',@col) > 0 )`
<br/>
`BEGIN`
<br/>
    `SET @col = STUFF(@col, 1, CHARINDEX(',', @col+''), '') `
<br/>       
`END`
<br/>
`ELSE`<br/>
`IF(LEN(@col)>0)`<br/>
`BEGIN`<br/>
    `SET @col = STUFF(@col, 1,LEN(@col) , '')   `   <br/>

`END`<br/>
`END`<br/>
`SET @query = STUFF(@query, len(@query), 1, '') `<br/>  
`print @query`

答案 3 :(得分:0)

我的回答

Declare @SQLString varchar(max)
Declare @SQLString2 varchar(max)
DECLARE @col_by_dim varchar(8000) = 'd1,d2,d3,d4',
@dim_name1 varchar(250) = 'CarrierEnrollment'
Set @SQLString = 'INSERT INTO [dbo].[zzz_'+@dim_name1+'](
                   '+@col_by_dim+'
                   )
Select '


DECLARE @pos INT
DECLARE @len INT
DECLARE @value varchar(8000)

set @pos = 0
set @len = 0

WHILE CHARINDEX(',', @col_by_dim, @pos+1)>0
BEGIN
    set @len = CHARINDEX(',', @col_by_dim, @pos+1) - @pos
    set @value = SUBSTRING(@col_by_dim, @pos, @len)

    set @SQLString2 = '
    ''~N'' AS ['+@value+'],'

    set @pos = CHARINDEX(',', @col_by_dim, @pos+@len) +1
    Set @SQLString = @SQLString + @SQLString2 
END
print @SQLString + ' 
    ''~N'' AS '+ '['+SUBSTRING(@col_by_dim, @pos, @len)+']'

答案 4 :(得分:0)

DECLARE @query varchar(max),
@col  varchar(250) = 'd1,d2,d3',@value varchar(max)


set @query='INSERT INTO [dbo].[MYTABLE](
                   '+@col+'
                   )
                   SELECT   '

declare c cursor for
select val from Split(@col, ',')
open c
fetch next from c into @value
while @@FETCH_STATUS=0
begin

Set @query = @query+ '''~N'''+' AS ['+@value+']'+','+CHAR(13)+char(9)+char(9)+char(9)+char(9)+char(9)+char(9)+char(9)
fetch next from c into @value
end
close c
deallocate c


set @query= substring(@query,1,len(@query))
print @query