I’m trying to write a stored procedure in SQL Server that will eliminate some logic in my C# program. What I’m doing right now is the query is in a view. Then I’m making a list with the view.
List<MyView> listOrdered = new List<MyView>();
Here’s where it gets hairy. The query returns rows that are duplicates. I don’t want to delete the duplicate rows I want to combine them into 1 row. The rows are identical except for 1 column.
Example:
UID Name Age Child
1 John 50 Sally
1 John 50 Steve
2 Joseph 42 Timmy
2 Joseph 42 Billy
So what I’m doing in C# is writing logic that says: (pseudo code)
foreach(item in list)
{
if (UID != UIDCurrent)
{
Build Row
AppendRow to list
}
else
{
Append Child Column to Current Child Column
}
}
Basically it gives me:
UID Name Age Children
1 John 50 Sally, Steve
But instead of doing this logic in C# I would like to do this a stored procedure. So how I can I get SQL Server to combine the children column for each row instead of multiple rows.
If you need anything else to help you help me I will respond.
Oh guys believe me I don’t want to do it this way either. The Database I’m using is huge and complex and doing this with C# was sensible and works but I’ve been asked to turn my function that does this in C# into a stored procedure. I just want to see if this is even possible.
This should work if you want to do this in the database, though you should think about doing it in the front-end.