I have the following tables:
tblengineeringlookupcolumnmaster
tblengineeringlookupdetail
elccolumnid | elclookupcode | elccolumnname | elcisrequired
1 | 64 | FirstName | 1 2 | 64 | LastName | 1
eldrecordId | eldlookupcode |eldlookupsequence |eldlookupvalue | eldlookupvaluedescription
245 | 64 | 0 | Red | Aravinth,Arumugam
246 | 64 | 1 | Blue | Santhosh,Chandran
247 | 64 | 2 | Green | Karthik,Balasubramanian
I need the output as:
elcLookupCode | eldRecordId | FirstName | LastName
-------------------------------------
64 | 245 | Aravinth | Arumugam
64 | 246 | Santhosh | Chandran
64 | 247 | Karthik | Balasubramanian
Here the values in the eldlookupvaluedescription
is FirstName,LastName
of the elcColumnName
in tblengineeringlookupcolumnmaster
table. So I have to split according to the elccolumnname
rows. If there are 3 rows in that table then I have to split the values of eldlookupvaluedescription
accordingly. Need to handle the empty values also.
I have tried to splitting the values like.
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
How can I achieve this?
Note: Will be great if table variables are used instead of TempTables.