次のテーブルがあります。
tblengineeringlookupcolumnmaster
tblengineeringlookup詳細
elccolumnid | elclookupcode | elccolumnname | elcis必須
1 | 64 | FirstName | 1 2 | 64 | LastName | 1
ldrecordId | eldlookupcode |eldlookupsequence |eldlookupvalue | フィールドルックアップ値説明
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 の代わりにテーブル変数を使用すると便利です。