は、私が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の環境設定があるということです制約が定義されています。
おかげで、
SrvName列に制約がありますか?そのコードはうまくいくはずです。 –
テーブルにトリガーがありますか? –
私はちょうど我々のSQL 2016 devのサーバーのコードを実行し、それは働いた。コードが実行される前にテーブルが削除されたため、トリガはありません。 – codingguy3000