Правильное использование переменных связывания с датами в Oracle?

StackOverflow https://stackoverflow.com/questions/9020405

  •  14-11-2019
  •  | 
  •  

Вопрос

Я озадачен по правильному использованию переменных связывания с <сильными> датами в Oracle.Это не в базе данных или при использовании PL / SQL, а скорее при взаимодействии с Oracle через интерфейс OCI, где дата должна быть передана в строку, используя функцию TO_DATE

Я бы полагал, что правильный подход, чтобы обеспечить правильное использование переменных связывания - сделать следующее:

to_date(:my_date, :my_date_format)
.

Однако я видел подходы, в которых формат даты не выполнен с использованием связей, поэтому я немного запутался.

Может кто-нибудь подтвердить это или предложить лучший подход?

Это было полезно?

Решение

Is the date format a constant? Or does it change at runtime?

Normally, you know what format the string is (at least expected) to be in so the date format would be a constant. If something is a constant, it is not necessary to make it a bind variable, it can just be hard-coded as part of the statement. In this case, it wouldn't matter either way but there are cases where you'd rather the value be hard-coded in the SQL statement because you want to give the optimizer more information (think of a column with highly skewed data where you're always looking for a particular hard-coded value).

On the other hand, if the date format changes at runtime because someone is passing both the string representation of the date and the format the string is in to your procedure, it would make sense for the date format to be a bind variable.

Другие советы

The answer to your question is it depends...

If you're dynamically creating your date_format then you ought to use a bind variable to make yourself SQL-injection safe. If you're not dynamically creating the date-format then it's already hard-coded and there's very little point.

select to_date(:my_date,'yyyymmdd') from dual

is safe anyway but:

select to_date(:my_date,:my_date_format) from dual

should really be a bind.

This is all assuming that :my_date is not a column, in which case it cannot be a bind variable at all.

If you're binding :my_date though you're passing a static date to Oracle and not using a column then can't OCI work this out for you without going to Oracle ( I don't know for sure, never used it ).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top