Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to fetch data from a table comma separated as a single row in a dynamic sql query

What I have tried:

SQL
DECLARE @COLUMNS NVARCHAR(max)
SELECT @COLUMNS = 
  CM.ColumnName
  FROM ICCS_StdReportTable_Master TM                                                                    
  inner join                                                
  ICCS_StdReport_Column_Master  CM                                           
  on CM.TableId=TM.TableId                                           
  where                  
  tm.RecordIdentifierKeyword in (  
   select SUBSTRING(VALUE,TM.StartPosition,TM.Length) from ICCS_MENUS MNU  
   INNER JOIN  
   ICCS_StdReportTable_Master TM   
   ON TM.TableName=SUBSTRING(VALUE,TM.StartPosition,TM.Length) 
   INNER JOIN
   ICCS_SCREENS SC
   ON SC.SCREENID=MNU.SCREENID
   AND MNU.MENUID=117)
Posted
Updated 23-Feb-17 2:55am
v3

Generally speaking it's a poor idea to do this - it's easy to insert the original values, but it complicates everything else from that point on to a massive degree. It's a much better idea to do it properly in the first place, with a separate table and a foreign key back into the original.

But this may help if you must do it: Converting comma separated data in a column to rows for selection[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900