views:

147

answers:

1

Hello fellows, I am using AS 400 OLEDB with .NET. It uses '?' instead of '@param to bind parameters with a command Now there is a situation where the command is like

SELECT ...
FROM
   (SELECT ... 
         ROW_NUMBER() OVER(ORDER BY ColumnName) as RowNum
    FROM Employees e
   ) as DerivedTableName
WHERE RowNum BETWEEN @startRowIndex AND (@startRowIndex + @maximumRows) - 1

Now my command becomes

    SELECT ...
FROM
   (SELECT ... 
         ROW_NUMBER() OVER(ORDER BY ColumnName) as RowNum
    FROM Employees e
   ) as DerivedTableName
WHERE RowNum BETWEEN ? AND (? + ?) - 1

Now when I bind parameters like

myCommand.Parameters.Add(new OleDbParameter("?",startRowIndex));
myCommand.Parameters.Add(new OleDbParameter("?", startRowIndex));
myCommand.Parameters.Add(new OleDbParameter("?", MaximumRows));

It throws error

SQL0417: Combination of parameter markers not valid.
Cause . . . . . :   The statement string specified as the object of a PREPARE statement contains a predicate or expression where parameter markers have been used as operands of the same operator.  The following restrictions apply to the use of parameter markers: -- Both the operands in a predicate cannot be parameter markers. For example, specifying predicates of the form:    ? = ?      or    ? = ( SELECT ? FROM x ) are not valid. 

How do I bind parameters in this situation ? I want to avoid sql injection :)

+1  A: 

Try changing the names of the parameters

myCommand.Parameters.Add(new OleDbParameter("@startRowIndex",startRowIndex));
myCommand.Parameters.Add(new OleDbParameter("@startRowIndex2", startRowIndex));
myCommand.Parameters.Add(new OleDbParameter("@MaximumRows", MaximumRows));

but leave the SQL as is.

Chris Diver
Thanks for the reply Chris. In OLEDB the parameters go with index and symbol '?' and not with name '@param'. That works on SQLServer. Any clue?
Popo
chris is right, even if i use access, i use ? as param
Amit Ranjan
He is, but problem is still there because of two parameters inside a single block (?+?). What do you do in access to get away with that ? Check my error description above.
Popo
Bump, anybody with OLEDB experience ?
Popo
"Both the operands in a expression cannot be parameter markers. For example, specifying an expression of the form: ? + ? is not valid." Can't you add the two values instead of doing it in SQL?
Chris Diver
hey I can, but that would require me to concatenate the values with query like where '+(parameter1+parameter2)+'Problem being, I want to bind the parameters in form of parametrized query.
Popo
thanks, got what you meant.
Popo