I've run into an issue installing a product that has worked at several customer sites just fine, I believe the issue has to do with the Collation setting on their database server. I have code that looks like this (I changed table and variables names around since the code is proprietary):
using (SqlCommand insertCommand = dbConnection.CreateCommand())
{
insertCommand.CommandText = "INSERT INTO [myTable] ([valueOne] ,[valueTwo] ,[CreationDate]) VALUES (@valueTwo ,@valueTwo ,@creationDate);select IDENT_CURRENT('myTable');";
insertCommand.Parameters.AddWithValue("@valueOne", "Value One");
insertCommand.Parameters.AddWithValue("@valueTwo", "Value Two");
insertCommand.Parameters.AddWithValue("@CreationDate", CreationDate);
dbConnection.Open();
object result = insertCommand.ExecuteScalar();
dbConnection.Close();
}
This works at most sites and on our development and QA machines, but at this one site we receive an error that says "Must declare scalar variable "@creationDate". The main discrepancy I see is that the site has the collation value set to SQL_Latin1_General_CP1_CS_AS and our settings are all SQL_Latin1_General_CP1_CI_AS. I changed this for our database but the server is still set to the Case Sensitive variation. This did resolve another issue we were having with a different table (that one was the table name, not a parameter name), but for some reason it's still a problem for this. Does anyone have any ideas on how I can resolve this a little more quickly than going through and fixing all of the case differences in our code base?
The site is using SQL Server 2005, and our code is written in C# and .NET 3.5.
Thanks -Hollis
The collation of the server controls those, this is by design.
From BOL (emphasis mine):
The collation of an identifier depends on the level at which it is defined. Identifiers of instance-level objects, such as logins and database names, are assigned the default collation of the instance. Identifiers of objects within a database, such as tables, views, and column names, are assigned the default collation of the database. Variables, GOTO labels, temporary stored procedures, and temporary tables can be created when the connection context is associated with one database and then referenced when the context has been switched to another database. Therefore, the identifiers for variables, GOTO labels, and temporary tables are in the default collation of the instance.
The case sensitivity of your variables is controlled by the server level collation, not database level. See this question on the same topic.
"I changed this for our database but the server is still set to the Case Sensitive variation. This did resolve another issue we were having with a different table (that one was the table name, not a parameter name), but for some reason it's still a problem for this. - Dan P 2012-04-03 21:46