Tuesday, April 18, 2017

Create a comma separated list in SQL Query


When we need to get all the value comma separated for specific action. For example I need to get all the values that have status is 1

First thing in mind if we are coding in c# or any other server language we go for loop right ? :) but right now we need to handle in SQL Server
so what first thing will come in mind Cursor right ? :)

There is another way you can get comma separated value without using Cursor see how we can do this

 DECLARE @list_effected_actions VARCHAR(MAX)  
 SELECT @list_effected_actions = COALESCE(@list_effected_actions+',' ,'') + column_name  FROM table where status=1  
 SELECT @list_effected_actions

No comments:

Post a Comment