2017-12-18 14 views
0

내가 널이 아닌 열을 정의하고 마지막 세 개의 열 (UTCInserted, UTCUpdated, & SRVNAME) 나는 기본 제약 조건을 만드는거야위한거야열에 기본 제약 조건이있는 경우 SQL Server 2012 열 가져 오기는 null 오류 일 수 없습니다.

create table dbo.dg_world_records(
            WorldRecordId int   not null identity(1,1) 
            , GameTypeId  int   not null 
            , PlayerId  int   not null 
            , NumberOfValues int   not null 
            , TotalTime  int   not null 
            , DateOfRecord  datetime  not null 
            , GameId   int   not null 
            , UTCInserted  datetime  not null 
            , UTCUpdated  datetime  not null 
            , SrvName   varchar(30) not null 
           ) 


go 
alter table dg_world_records add constraint dg_world_records_worldrecordid_pk primary key (worldrecordid) 
go 
alter table dg_world_records add constraint dg_world_records_utcinserted_df default getutcdate() for utcinserted 
go 
alter table dg_world_records add constraint dg_world_records_utcupdated_df default getutcdate() for utcupdated 
go 
alter table dg_world_records add constraint dg_world_records_srvname_df default @@servername for srvname 
go 

... 다음 스키마를 고려하십시오.

저는 많은 시간을 회사 개발 서버에서 수행했으며 모든 것이 잘 작동합니다.

그러나 SQL Server 2012의 로컬 인스턴스에서 다음 삽입 문이 오류를 발생시킵니다. 여기

insert into dbo.dg_world_records(gametypeid, playerid, numberofvalues, totaltime, dateofrecord, gameid) 
values(11, 1, 365, 430000, getdate(), -1) 

오류 메시지입니다 ...

Msg 515, Level 16, State 2, Line 1 
Cannot insert the value NULL into column 'SrvName', table 'dateguess.dbo.dg_world_records'; column does not allow nulls. INSERT fails. 
The statement has been terminated. 

내 생각 엔 당신이 null이 아닌 열 경우 디폴트 값을 지정하지 수 있도록 SQL Server의 환경 설정이 있다는 것입니다 제한이 정의됩니다.

감사합니다.

+1

SrvName 열에 제약 조건이 있는지 100 % 확신합니까? 이 코드는 잘 작동합니다. –

+0

테이블에 방아쇠가 있습니까? –

+0

방금 ​​SQL 2016 dev server라는 코드를 실행했고 작동했습니다. 내 코드를 실행하기 전에 테이블이 삭제 된 트리거가 없습니다. – codingguy3000

답변

0

알아 냈습니다.

문제는

@@servername 

이 문

가 null 반환되었다 ... 다음 문입니다.

sp_addserver 'LT9LHJKV1\SQLSERVERROCKS', local 

은 그 때 나는 SQL Server를 다시 시작했다 ... 나는 sp_addserver를 통해 로컬 서버를 추가했다이 문제를 해결합니다.

@@ servername이 값을 반환하면 내 insert 문이 작동합니다.