2013-04-24 3 views
0

이진 열을 채우는 :자동이 같은 구조를 가진 테이블이 SQL 서버

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 
    ); 

내가 IDENTITY 재산처럼 자동으로 채워 열로 tb_com_hist_xfer_id 열을 만들고 싶어. 하지만 이진 유형에는 IDENTITY을 사용할 수 없습니다. 나를 대신 할 수있는 것은 무엇입니까?

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 
    ); 

답변

2

이 시도?

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 
+1

'newid()'는'binary (16) '에 편안하게 들어갈 것이므로 왜 열 길이를 변경하겠습니까? –

+0

@Damien_The_Unbeliever - newif() 값을 이진 값으로 변환하면 크기가 더 커지므로 50을 선택했다고 생각했습니다. – AnandPhadke

+0

해당 열을 기준으로 조회를 수행하려는 경우 'newid() '보다는'newsequentialid()'를 제안하십시오. 그래서 그들은 적어도 어느 정도 순서에 있습니다. 특히 당신이 그 칼럼을 클러스터한다면 사실입니다. –

1

당신이 uniqueidentifier를 사용하지 않겠 :

+0

@AnandPhadke 그것은 나를 위해 일했고 나는 바이너리 (16) – vchitta

관련 문제