我有以下表格:
tblengineeringlookupcolumnmaster
tbl工程查找详细信息
elccolumnid | elclookupcode | elccolumnname | elcis 必填
1 | 64 | FirstName | 1 2 | 64 | LastName | 1
字段记录 ID | 字段查找代码 |字段查找序列 |字段查找值 | 字段查找值描述
245 | 64 | 0 | Red | Aravinth,Arumugam
246 | 64 | 1 | Blue | Santhosh,Chandran
247 | 64 | 2 | Green | Karthik,Balasubramanian
我需要输出为:
elcLookupCode | eldRecordId | FirstName | LastName
-------------------------------------
64 | 245 | Aravinth | Arumugam
64 | 246 | Santhosh | Chandran
64 | 247 | Karthik | Balasubramanian
这里的值eldlookupvaluedescription
是表FirstName,LastName
中elcColumnName
的值tblengineeringlookupcolumnmaster
。所以我必须根据elccolumnname
行拆分。如果该表中有 3 行,那么我必须eldlookupvaluedescription
相应地拆分 的值。还需要处理空值。
我试图拆分像这样的值。
declare @sqlstr nvarchar(max);
--Select the initial values
select @sqlstr = 'a,b,c,d,e,f';
--Replace the comma so the string becomes a','b','c','d','e','f
select @sqlstr = REPLACE(@sqlstr, ',', ''',''')
--Add select to the beginning and add leading and trailing ' around the select values
select @sqlstr = 'Select ''' + @sqlstr + ''''
--execute the dynamic sql of select 'a','b','c','d','e','f'
exec sp_executesql @sqlstr
我怎样才能做到这一点?
注意:如果使用表变量而不是 TempTables,那就太好了。