【问题标题】:How to Copy a Timestamp Datatype如何复制时间戳数据类型
【发布时间】:2019-02-11 10:06:18
【问题描述】:

我有一张带有TIMESTAMP 列的表格:

create  table dbo.EX_EMPLOYEE (
    NAME_X            varchar(10) null,
    RECORD_TIME_STAMP timestamp   null
    )

当我将行从一个表复制到另一个表时,使用:

SELECT * INTO EX_EMPLOYEE_T 
    FROM EX_EMPLOYEE 
    WHERE 1=0

或:

INSERT INTO EX_EMPLOYEE_T
    SELECT * 
        FROM EX_EMPLOYEE

我收到此警告:

警告:用户不能将非空值插入TIMESTAMP 列。数据库时间戳值已被插入到 TIMESTAMP 字段中。

目标表中的TIMESTAMP 列替换为当前数据库时间戳。

我的问题
如何复制带有TIMESTAMP 列的行,同时保留源表中的TIMESTAMP 值?

(有没有类似SET IDENTITY ON/OFF的设置)

我的场景
我有 2 个表,一个用于“实时”数据,另一个用于“备份”,所以我需要复制带有 TIMESTAMP 完整的行。我需要它完好无损,以便检测“实时”行是否发生了变化。

【问题讨论】:

  • 现在无法测试,但我认为 bcp 能够复制到时间戳列中
  • @PerformanceDBA 在我的问题中解释了创建表。 SELECT * INTO EX_EMPLOYEE_T FROM EX_EMPLOYEE WHERE 1=0 .. markp 提供的答案很好,但如果我在 insert INSERT INTO EX_EMPLOYEE_T SELECT * FROM EX_EMPLOYEE 中不指定列,则会导致警告
  • 1) 所以指定列。 2) @markp 明确声明您不能使用 SELECT-INTO;您必须使用 INSERT-SELECT;必须指定列。 3) SELECT-INTO 无论如何都不是一件好事,在生产系统中是不可接受的。将其视为仅用于开发的便利。 4)你不想“禁用时间戳”,你想原封不动地复制它。

标签: sybase sap-ase


【解决方案1】:

Sybase(现在是 SAP)复制服务器 (SRS) 可以在 Sybase/SAP ASE 表之间复制时间戳值,即 SRS 维护用户可以将显式值插入到类型为 timestamp 的列中。

这怎么可能?有几个要求:

  • 执行插入(到timestamp 列)的用户必须具有replication_role 角色(并且必须处于活动状态)
  • 您需要发出set timestamp_insert on 命令(注意:如果您的用户没有replication_role,这将产生错误)
  • 您需要在插入语句中显式列出目标表的列

设置:

exec sp_displaylogin
go
...
Configured Authorization:
        ...
        replication_role (default ON)      <<<=== verify role assigned and active
        ...

create table EX_EMPLOYEE
(NAME_X                 varchar(10)     NULL
,RECORD_TIME_STAMP      timestamp       NULL
)
go

insert into EX_EMPLOYEE (NAME_X) values ('Larry')
insert into EX_EMPLOYEE (NAME_X) values ('Mo')
insert into EX_EMPLOYEE (NAME_X) values ('Curly')
go

select * from EX_EMPLOYEE
go

 NAME_X     RECORD_TIME_STAMP
 ---------- ------------------
 Larry      0x00000000ec4304fa
 Mo         0x00000000ec4304fd
 Curly      0x00000000ec430501

select * into EX_EMPLOYEE_T FROM EX_EMPLOYEE where 1=2
go

现在进行一些插入测试...

-- haven't issued the 'set timestamp_insert on' commmand, yet

insert into EX_EMPLOYEE_T
select * from EX_EMPLOYEE
go

Warning: A non-null value cannot be inserted into a TIMESTAMP column by the user. The database timestamp value has been inserted into the TIMESTAMP field instead.

-- received the *WARNING*, ie, rows are inserted but they receive new timestamp values

select * from EX_EMPLOYEE_T
go

 NAME_X     RECORD_TIME_STAMP
 ---------- ------------------
 Larry      0x00000000ec430548       <<<=== different from what's in EX_EMPLOYEE
 Mo         0x00000000ec43054a       <<<=== different from what's in EX_EMPLOYEE
 Curly      0x00000000ec43054c       <<<=== different from what's in EX_EMPLOYEE

-- enable direct insert of timestamp values

set timestamp_insert on
go

truncate table EX_EMPLOYEE_T
go

-- w/out explicitly listing target columns ...

insert into EX_EMPLOYEE_T
select * from EX_EMPLOYEE
go

-- no warning message is generated, insert succeeds, but new timestamp values are generated

select * from EX_EMPLOYEE_T
go

 NAME_X     RECORD_TIME_STAMP
 ---------- ------------------
 Larry      0x00000000ec430555       <<<=== different from what's in EX_EMPLOYEE
 Mo         0x00000000ec430557       <<<=== different from what's in EX_EMPLOYEE
 Curly      0x00000000ec430559       <<<=== different from what's in EX_EMPLOYEE

truncate table EX_EMPLOYEE_T
go

-- this time we'll explicitly list the target table's columns ...

insert into EX_EMPLOYEE_T (NAME_X, RECORD_TIME_STAMP)
select * from EX_EMPLOYEE
go

-- and now we see the timestamp values copied from the source

select * from EX_EMPLOYEE_T
go

 NAME_X     RECORD_TIME_STAMP
 ---------- ------------------
 Larry      0x00000000ec4304fa       <<<=== same as what's in EX_EMPLOYEE
 Mo         0x00000000ec4304fd       <<<=== same as what's in EX_EMPLOYEE
 Curly      0x00000000ec430501       <<<=== same as what's in EX_EMPLOYEE

以上是在 ASE 15.7 SP138 数据服务器上测试的。

【讨论】:

  • 我收到类似这样的错误 insert into EX_EMPLOYEE_T select * from EX_EMPLOYEE 但是如果添加它得到修复的列你知道为什么吗?
  • 我已经澄清并清理了这个问题。您可能希望减少答案;删除现在多余的参考资料和示例;等
猜你喜欢
  • 2018-06-24
  • 2012-02-17
  • 2019-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-19
  • 2012-08-29
  • 1970-01-01
相关资源
最近更新 更多