Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
How can I convert the bellow code to SQL Query(Stored Procedure) ?

string[] strORCarriers = IW.GetORCarriers();

if (strORCarriers.Length != 0)
{
for (int i = 0; i < strORCarriers.Length; i++)
{
sb.Append("'" + strORCarriers[i].ToString().ToUpper() + "'");
if (i != (strORCarriers.Length - 1))
{
sb.Append(",");
}
}
}

What I have tried:

I need to convert the c# code(The code contain DB2 Query) to Stored Procedure
Posted
Updated 15-Apr-16 9:06am
Comments
Herman<T>.Instance 13-Apr-16 5:20am    
What stops you?
CHill60 13-Apr-16 9:31am    
That's the strangest looking DB2 query I've ever seen - looks more like C# to me. :-p
Do you want the DB2 query converting to SQL? In which case we need to see that.
Basically you have a function that is returning a string[] and you want some code that would do the same with the results of a sql query??

1 solution

If you want to concatenate string values from multiple rows in SQL, use one of the methods described in this artice:
Concatenating Row Values in Transact-SQL[^]

For example:
SQL
SELECT 
    p1.CategoryId,
    STUFF( 
        (
            SELECT ',' + ProductName
            FROM Northwind.dbo.Products p2
            WHERE p2.CategoryId = p1.CategoryId
            ORDER BY ProductName
            FOR XML PATH(''), TYPE
        ).value('.', 'varchar(max)')
    , 1, 1, '')
    As Products
FROM 
    Northwind.dbo.Products p1
GROUP BY 
    CategoryId
;



If that's not what you want to do, then you'll need to update your question to explain the problem more clearly. If possible, create a SQL Fiddle[^] with some sample data to demonstrate the problem.
 
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