2017-12-18 13 views
0

は、私がnullではない列を定義し、最後の3つの列(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. 

私の推測では、デフォルトNOT NULL列に値を指定しないことを可能にする、SQL Serverの環境設定があるということです制約が定義されています。

おかげで、

+1

SrvName列に制約がありますか?そのコードはうまくいくはずです。 –

+0

テーブルにトリガーがありますか? –

+0

私はちょうど我々のSQL 2016 devのサーバーのコードを実行し、それは働いた。コードが実行される前にテーブルが削除されたため、トリガはありません。 – codingguy3000

答えて

0

は、私はそれを考え出しました。

問題は、以下の文です...

@@servername 

この文は、nullを返していました。私はsp_addserverを介してローカルサーバーを追加しました。この問題を解決するには

...

sp_addserver 'LT9LHJKV1\SQLSERVERROCKS', local 

は、それから私は、SQL Serverを再起動する必要がありました。

その後、@@ servernameが値を返し、私のinsert文が機能します。

関連する問題