Error 42P18: could not determine data type of parameter $1
Posted: Thu 04 Oct 2018 20:46
We are attempting to execute the following query using a PgSqlCommand object:
However, regardless of the parameter type or value, we get ErrorCode 42P18, Message "42P18: could not determine data type of parameter $1".
We are using Devart.Data.PostgreSql 7.11.1202.0
The following is a unit test method that demonstrates this problem:
In practice, we intend to always pass a string as the parameter, or a null value. In all cases, we expect to get a table with one row and one column. When the parameter is null, we expect that row's column to be null.
If we change the query by casting the parameter as text (as follows) then it works without error:
However, we should not have to change the SQL to get it to respect our parameters.
Any help you can provide would be appreciated.
Bill.
Code: Select all
select case
when :ANALYZER_VALUE_1 is not null then
'and ana.analyzer_id in ('||:ANALYZER_VALUE_1||')'
end
We are using Devart.Data.PostgreSql 7.11.1202.0
The following is a unit test method that demonstrates this problem:
Code: Select all
[TestMethod()]
public void Error42P18_Test()
{
using (var connection = new PgSqlConnection())
{
var builder = new PgSqlConnectionStringBuilder();
builder.Host = "HOST";
builder.Database = "DATABASE";
builder.Port = 5432;
builder.UserId = "USER";
builder.Password = "PASSWORD";
connection.ConnectionString = builder.ToString();
connection.Open();
const string PARAM_NAME = "ANALYZER_VALUE_1";
string sql = $@"
select case
when :{PARAM_NAME} is not null then
'and ana.analyzer_id in ('||:{PARAM_NAME}||')'
end";
try
{
using (var cmd = new PgSqlCommand(sql, connection) { ParameterCheck = true })
{
cmd.Parameters.Add(new PgSqlParameter(PARAM_NAME, string.Empty));
var reader = cmd.ExecuteReader();
}
}
finally
{
connection.Close();
}
}
}
If we change the query by casting the parameter as text (as follows) then it works without error:
Code: Select all
select case
when :ANALYZER_VALUE_1::text is not null then
'and ana.analyzer_id in ('||'ANALYZER_VALUE_1'||')'
end
Any help you can provide would be appreciated.
Bill.