无法在Microsoft SQL Server中插入日期时间值

时间:2017-07-08 15:59:23

标签: sql-server

我正在创建一个数据库,如果没有插入日期值,我希望列接受当前日期。代码有效,但插入日期并没有。

我在创建表格时使用了default约束和OrderDate datetime null check (OrderDate <= getdate()) default getdate()

create table Transactions.OrderDetails 
(
    PurchaseOrderID int identity(1, 1) primary key
  , EmployeeID int not null
  , ItemID int not null
  , OrderDate datetime null check (OrderDate <= getdate()) default getdate()
  , ReceivingDate datetime null --check (ReceivingDate > OrderDate)
  , QO int not null --check (QO > QR)
  , QR int null check (QR > 0)
  , UnitPrice money not null check (UnitPrice > 0)
  , ShippingMethod char(6) not null
  , OrderStatus varchar(50) not null
 )

这是用于创建表格的代码:

import Foundation
import Accelerate
let N = 16
var xdtar = UnsafeMutablePointer<Double>.allocate(capacity: N)
var xdtai = UnsafeMutablePointer<Double>.allocate(capacity: N)
xdtar.initialize(to: 0.0, count: N )
xdtai.initialize(to: 0.0, count: N )
var x = DSPDoubleSplitComplex(realp: xdtar, imagp: xdtai)
var ydtar = UnsafeMutablePointer<Double>.allocate(capacity: N)
var ydtai = UnsafeMutablePointer<Double>.allocate(capacity: N)
ydtar.initialize(to: 0.0, count: N )
ydtai.initialize(to: 0.0, count: N )
var y = DSPDoubleSplitComplex(realp: ydtar, imagp: ydtai)
for i in 0..<N {
    xdtar[i] = Double(i)
}
let setup = vDSP_DFT_zop_CreateSetupD(nil, vDSP_Length(N),
       vDSP_DFT_Direction.FORWARD)
vDSP_DFT_ExecuteD(setup!, x.realp, x.imagp, y.realp, y.imagp)
vDSP_DFT_DestroySetupD(setup)
for i in 0..<N {
    print(String(format: "%2d \t-> in \t(%5.4f, %5.4fi)\t out \t(%5.4f, %5.4fi)",
                i, xdtar[i], xdtai[i], ydtar[i], ydtai[i]))
}
xdtar.deinitialize(count: N); xdtar.deallocate(capacity: N)
xdtai.deinitialize(count: N); xdtai.deallocate(capacity: N)
ydtar.deinitialize(count: N); ydtar.deallocate(capacity: N)
ydtai.deinitialize(count: N); ydtai.deallocate(capacity: N)

1 个答案:

答案 0 :(得分:2)

创建列getdate(),并为getutcdate()数据类型添加datetimecreate table Orders ( id int , OrderDate_notnull datetime not null default getdate() , OrderDate_null datetime null default getdate() ); /* nullable column with default not specified in insert uses default*/ insert into Orders (id) values (1); /* nullable column with default specified in insert remains null */ insert into Orders (id,OrderDate_null) values (2,null); /* non-nullable column specified with null value throws an error */ --insert into Orders (id,OrderDate_notnull) values (3,null); select * from Orders; 的默认约束。

+----+---------------------+---------------------+
| id |  OrderDate_notnull  |   OrderDate_null    |
+----+---------------------+---------------------+
|  1 | 2017-07-08 18:10:50 | 2017-07-08 18:10:50 |
|  2 | 2017-07-08 18:10:50 | NULL                |
+----+---------------------+---------------------+

rextester演示:http://rextester.com/CCNT4591

返回:

val salary = sc.textFile("File1.csv").map(_.split(",").map(_.trim))
val location = sc.textFile("File2.csv").map(_.split(",").map(_.trim))
val joined = salary.map(e=>(e(0),e(3).toInt)).join(location.map(e=>(e(0),e(1))))
val locSalary = joined.map(v => (v._2._2, v._2._1))
val averages = locSalary.aggregateByKey((0,0))((t,e) => (t._1 + 1, t._2 + e),
        (t1,t2) => (t1._1 + t2._1, t1._2 + t2._2)).mapValues(t => t._2/t._1)