Oracle considers empty strings to be NULL while SQL Server does not - how is this best handled?

前端 未结 11 1787
挽巷
挽巷 2021-01-01 13:35

I have to write a component that re-creates SQL Server tables (structure and data) in an Oracle database. This component also has to take new data entered into the Oracle d

11条回答
  •  离开以前
    2021-01-01 14:19

    My typical solution would be to add a constraint in SQL Server forcing all string values in the affected columns to have a length greater than 0:

    CREATE TABLE Example (StringColumn VARCHAR(10) NOT NULL)
    
    ALTER TABLE Example
    ADD CONSTRAINT CK_Example_StringColumn CHECK (LEN(StringColumn) > 0)
    

    However, as you have stated, you have no control over the SQL Database. As such you really have four choices (as I see it):

    1. Treat empty string values as invalid, skip those records, alert an operator and log the records in some manner that makes it easy to manually correct / re-enter.
    2. Convert empty string values to spaces.
    3. Convert empty string values to a code (i.e. "LEGACY" or "EMPTY").
    4. Rollback transfers that encounter empty string values in these columns, then put pressure on the SQL Server database owner to correct their data.

    Number four would be my preference, but isn't always possible. The action you take will really depend on what the oracle users need. Ultimately, if nothing can be done about the SQL database, I would explain the issue to the oracle business system owners, explain the options and consequences and make them make the decision :)

    NOTE: I believe in this case SQL Server actually exhibits the "correct" behaviour.

提交回复
热议问题