自动填充SQL Server中的二进制列

时间:2013-04-24 11:49:06

标签: sql-server

我的表格结构如下:

CREATE TABLE tb_comm_hist_xfer (
        tb_comm_hist_xfer_id binary(16) NOT NULL,
        tb_old_customer_id int NOT NULL,
        tb_customer_id int NULL,
        date_entered datetime NOT NULL
    );

我想将tb_com_hist_xfer_id列设为自动填充列,例如IDENTITY属性。但我不能将IDENTITY用于二进制类型。对我来说有什么选择?

2 个答案:

答案 0 :(得分:3)

试试这个:

CREATE TABLE tb_comm_hist_xfer (
        tb_comm_hist_xfer_id binary(50) default CONVERT(varbinary(50),NEWID()) NOT NULL,
        tb_old_customer_id int NOT NULL,
        tb_customer_id int NULL,
        date_entered datetime NOT NULL
    );

答案 1 :(得分:1)

您确定不想使用uniqueidentifier吗?

CREATE TABLE tb_comm_hist_xfer (
        tb_comm_hist_xfer_id uniqueidentifier default newid() NOT NULL,
        tb_old_customer_id int NOT NULL,
        tb_customer_id int NULL,
        date_entered datetime NOT NULL
    );
GO

insert into tb_comm_hist_xfer (tb_old_customer_id, tb_customer_id, date_entered) values (1, 2, getdate())

select cast(tb_comm_hist_xfer_id as binary(16)), * from tb_comm_hist_xfer