简体   繁体   中英

T-SQL to Oracle: Complicated Table Column

I'd like to convert the following table to PL-SQL.

CREATE TABLE [SchemaName].[TableName] (
    [value]         VARBINARY (2000) NULL,
    [valueAsString] AS               (CONVERT([nvarchar](1000),[value],0))
);

Has anyone an idea on how to recreate the valueAsString pseudo column in PL?

One possible solution would be

HUSQVIK@hq_pdb_tcp> CREATE OR REPLACE FUNCTION convert_raw_to_nvarchar(bin RAW) RETURN NVARCHAR2 DETERMINISTIC
  2  IS
  3  BEGIN
  4     RETURN utl_raw.cast_to_nvarchar2(bin);
  5  END;
  6  /

Function created.

HUSQVIK@hq_pdb_tcp> CREATE TABLE SchemaName.TableName (
  2      value         RAW(2000) NULL,
  3      valueAsString AS (convert_raw_to_nvarchar(value))
  4  );

Table created.

HUSQVIK@hq_pdb_tcp> insert into tablename (value) values (utl_raw.cast_to_raw(n'123344čřžžýřžý'));

1 row created.

HUSQVIK@hq_pdb_tcp> select * from tablename;

VALUE                                                        VALUEASSTRING
------------------------------------------------------------ --------------------
00310032003300330034003400630072007A007A00EC0072007A00EC     123344cržžýržý

If the limit of the binary column is 2000 bytes BLOB is not needed.

utl_raw.cast_to_nvarchar2 is not deterministic unfortunately so it needs to be wrapped into deterministic function to be usable in table definition.

But I don't like such solution. A view can solve this without schema level function. Also data types suggest you store information using inappropriate data type when you want to always convert binary data into string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM