问题
Im using Sybase ASE 15.7 and facing issue while proxy table insert with Default value. We want migrate data from source table to Target table, where target table is in remote server has Additional Columns with Not null and Default values. Please find the below Tables Details.
Source Table:
CREATE TABLE TABLE_SOURCE
(
COL1 INT,
COL2 INT,
COL3 INT
)
Target Table:
CREATE TABLE TABLE_TARGET
(COL1 INT,
COL2 INT,
COL3 INT,
COL4 INT Default 0 NOT NULL
)
Creating Proxy table in Source DB:
CREATE Proxy_Table TAB_TARGET AT 'TGT_SERVER.DB.DBO.TABLE_TARGET'
In Source we have details of three columns and we framed Query with that.
Script:
INSERT INTO TAB_TARGET (COL1,COL2,COL3) values (1,2,3)
Error:
The column COL4 in table TAB_TARGET does not allow null values., Error 233, Line 1
Though we have declare default costraint for COL4 but it is not accepting that through Proxy table. Is there any way to fix this issue. We have more than 5K Tables where users are migrating data and this issue is there in more than 100 tables.
Is there any way to insert into target table without NOT NULL issue
回答1:
The following shows up under the Usage section of Reference Manual: Commands: create proxy_table:
"Column defaults and rules are not imported by create proxy_table; use create existing table instead, where column properties can be defined."
NOTE: This reference is from the ASE 16 manuals; I could not find this comment in the ASE 15.7 SP100 manuals; I'm assuming this detail was overlooked when the ASE 15.7 manuals were written.
Since you're dealing with a large volume of tables I'm gong to assume you've got some scripting in place to speed up this migration process (and to minimize errors that could arise from manually creating 5K proxy tables and the associated INSERT statements).
If this assumption is true, and you don't want the hassles of creating 5K create existing table commands (complete with all of those pesky column details), I'm wondering if it would be (relatively) easier to to generate the INSERT statements with explicit default values, eg:
INSERT INTO TAB_TARGET (COL1,COL2,COL3,COL4) values (1,2,3,0)
As for how to find the default value in the system tables ...
- If a column has a default value the associated record in syscolumns will have a non-zero value stored in the cdefault column, ie, syscolumns.cdefault > 0.
- The non-zero cdefault value is a pointer to a record in syscomments where information about the default is stored, ie, syscolumns.cdefault = syscomments.id.
- The actual default value is stored in the syscomments.text column in one of two formats depending on if the default was a) defined inline as part of a create/alter table command or b) defined as a standalone default via the create default command.
Example:
-- inline default value of '0' assigned to column 'b'
create table t1 (a int, b int default 0 not null, c int)
go
-- standalone default named 'myzero' with default value of '0'; bound to column 'c'
create default myzero as 0
go
sp_bindefault myzero, 't1.c'
go
sp_help t1
go
...
Column_name ... Default_name
----------- ... --------------
a ... NULL
b ... t1_b_467529718 -- inline default
c ... myzero -- standalone default
...
-- contents of syscolumns.cdefault
select name, cdefault from syscolumns where id = object_id('t1') order by colid
go
name cdefault
---------- -----------
a 0
b 467529718
c 387529433
-- contents of syscomments.text
select id,text from syscomments where id in (467529718,387529433) order by id
go
id text
--------- ---------------------------
467529718 DEFAULT 0 -- inline default
387529433 create default myzero as 0 -- standalone default
-- pulling this all together:
select col.name, com.text
from syscolumns col,
syscomments com
where col.id = object_id('t1')
and col.cdefault = com.id
order by 1
go
name text
---------- ------------------------------
b DEFAULT 0 -- inline default
c create default myzero as 0 -- standalone default
From here you should be able to parse out the default value based on the format of the syscomments.text column and the actual default value (ie, is it numeric? is it a character string, possibly with embedded space? is it a function reference, eg, getdate()).
来源:https://stackoverflow.com/questions/63675834/in-sybase-ase-unable-to-insert-using-proxy-table-command