使用SQL Server CAST([column] AS DATE)函数的唯一约束

时间:2018-12-21 10:27:49

标签: sql sql-server date unique-constraint

我有一个appointments表,其中有一个[personid]和一个[appdate]数据类型的datetime(2)列。我想添加一个约束,以防止任何personid在任何一天都获得多个约会。

我尝试过,但是没有用

ALTER TABLE dbo.tblappointments
    ADD CONSTRAINT SingleApp UNIQUE (PersonID, CAST(Appdate AS DATE))

谢谢。

1 个答案:

答案 0 :(得分:2)

我想您可以创建一个计算列并将其用作索引。

add-symbol-file

如果您不使用["status": "1", "error": false, "data": [ { "order_id": "11", "order_unique_id": "ORDR-1001", "store_name": "24X7", "otp_store": "781103", "product": [ "Product One", "Product Two" ], "qty": [ "1", "3" ], "user_details": { "name": "test", "number": "98145314586", "landmark": "test", "area": "Bow East", "pincode": "E3 9EG", "place": "Home" }, "status": "2", "date": "2018-12-13", "time": "14:37:57" }] struct RunnerStatus: Decodable { var status: String var error: Bool var data: [Runner] } struct Runner: Decodable { var order_id: String var order_unique_id: String var store_name: String var otp_store: String var product: [Product] var qty: [Quantity] var user_details: [UserDetails] var status: String var date: String var time: String } struct Product: Decodable { var ProductName: String } struct Quantity: Decodable { var qty: String } struct UserDetails: Decodable { var name: String var number: String var landmark: String var area: String var pincode: String var place: String } ,则每次访问该列时都会计算该值