Click here to Skip to main content
16,020,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a function
GetStudents
, that accepts two parameters. I want to use ordinal values to make the select but I am getting an error. If I manage to do this then I will try to add a where in clause that conditions vary that is why I need to use ordinal values.

using (SqlCommand cd = db.CreateCommand())
                    {
                        cd.CommandType = CommandType.Text;
                        cd.CommandText = "SELECT ClassName FROM GetStudents(?,?)";
                        cd.Parameters.AddWithValue(null, age);
                        cd.Parameters.AddWithValue(null, monthBday);
}


What I have tried:

<pre>using (SqlCommand cd = db.CreateCommand())
                    {
                        cd.CommandType = CommandType.Text;
                        cd.CommandText = "SELECT ClassName FROM GetStudents(?,?)";
                        cd.Parameters.AddWithValue(null, age);
                        cd.Parameters.AddWithValue(null, monthBday);
}
Posted
Updated 27-Feb-18 4:27am

SQL uses named parameters. For a function call, the parameter names don't need to match the names declared in the function:
C#
cd.CommandText = "SELECT ClassName FROM GetStudents(@p0,@p1)";
cd.Parameters.AddWithValue("@p0", age);
cd.Parameters.AddWithValue("@p1", monthBday);
 
Share this answer
 
Comments
datt265 27-Feb-18 10:26am    
Yes like this works, now If I want to add a where 'country' but county names can change how do I do it?

SELECT ClassName FROM GetStudents(?,?) where country in ('USA', 'UK')
Richard Deeming 27-Feb-18 10:32am    
string[] countries = { "USA", "UK" };
string[] parameterNames = countries.Select((_, i) => "@C" + i).ToArray();

cd.CommandText = string.Format(
    "SELECT ClassName FROM GetStudents(@p0,@p1) WHERE country In ({0})",
    string.Join(", ", parameterNames));

// ==> "SELECT ClassName FROM GetStudents(@p0,@p1) WHERE country In (@C0, @C1)"

cd.Parameters.AddWithValue("@p0", age);
cd.Parameters.AddWithValue("@p1", monthBday);

for (int index = 0; index < countries.Length; index++)
{
    cd.Parameters.AddWithValue(parameterNames[index], countries[index]);
}
I think your approach is all wrong. You can calculate their age with some SQL date math code. Beyond that, your GetStudents function should not even exist. Whatever query is in the function can be put into the query that's calling the function. Calling a function in a query is very expensive in terms of performance, and should be avoided if possible. So, assuming you want the classname for all students of a certain age:

SQL
SELECT [ClassName] 
FROM Students 
WHERE @age = DATEDIFF(YEAR, CONVERT(DATE,GETDATE()), CONVERT(DATE,[DateOfBirth]))
 
Share this answer
 
v2
Comments
Richard Deeming 27-Feb-18 10:35am    
"Calling a function in a query is very expensive in terms of performance, and should be avoided if possible."

True for a scalar function, or a multi-statement TVF; but an inline TVF typically has the same performance as putting the whole query inline.

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