(Related to this post, but not the same.)
Is there any way to use a SqlParameter to specify that a value should be obtained by calling a function at the server? E.g.
private void Test()
{
var cmd = new SqlCommand("update Table set ADateField = @p1 where Id = @id");
cmd.Parameters.AddWithValue("@id", 42);
cmd.Parameters.AddWithValue("@p1", "sysutcdatetime()"); // WRONG!!
cmd.ExecuteNonQuery();
}
I can embed the call in the CommandText string, but sometimes I want to set an explicit value rather than call the function so it gets a bit messy.
I do not want to get the date on the client machine as this may be different than the date at the server due to time zone differences.
From the docs I can’t see how it can be done but thought I would check with you guys before giving up. Thanks!
I don’t believe this can be done. Two options are to either rewrite your SQL statement depending on whether you will be passing an exact date or not, or embed the SQL inside a stored procedure which can then check the passed in parameter to see if it is null or something and substitute the correct function calls: