久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

檢查具有多個輸入?yún)?shù)的約束 UDF 不起作用

Check constraint UDF with multiple input parameters not working(檢查具有多個輸入?yún)?shù)的約束 UDF 不起作用)
本文介紹了檢查具有多個輸入?yún)?shù)的約束 UDF 不起作用的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試對表實施檢查約束,這樣就無法在存在兩列(Int_1"和Int_2")已經(jīng)具有我們想要的值的記錄的情況下插入記錄試圖插入例如:

I'm trying to implement a check constraint on a table such that records can't be inserted where there exists a record for which two of the columns ("Int_1" and "Int_2") already have the value we're trying to insert E.g.:

ID     Name     Int_1     Int_2
1      Dave       1         2

將 (2, Steve, 2, 2) 插入上表是可以的,就像 (3, Mike, 1, 3) 一樣,但不允許插入已經(jīng)存在 Int_1 AND Int_2 的值,即 (4,Stuart, 1, 2) 是非法的.

Inserting (2, Steve, 2, 2) into the table above would be okay, as would (3, Mike, 1, 3), but inserting values where Int_1 AND Int_2 already exist is not allowed, i.e. (4, Stuart, 1, 2) is illegal.

我認為這樣定義我的表格會起作用:

I thought defining my table thus would work:

CREATE TABLE [Table](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](255) NOT NULL,
    [Int_1] [int] NOT NULL,
    [Int_2] [int] NOT NULL,
    CONSTRAINT [chk_Stuff] CHECK (dbo.chk_Ints(Int_1, Int_2)=1))

其中:dbo.chk_Ints 的定義:

where: dbo.chk_Ints is defined:

CREATE FUNCTION [dbo].[chk_Ints](@Int_1 int,@Int_2 int)
RETURNS int
AS
BEGIN

DECLARE @Result int

IF NOT EXISTS (SELECT * FROM [Table] WHERE Int_1 = @Int_1 AND Int_2 = @Int_2)
BEGIN
    SET @Result = 1
END
ELSE 
BEGIN
    SET @Result = 0
END

RETURN @Result
END

GO

當使用上面的組合時,如果我嘗試插入任何記錄,SQL 會告訴我我違反了檢查約束.我可以從表中刪除所有行并嘗試插入第一條記錄,SQL 告訴我我已經(jīng)打破了我的約束,這是我不可能做到的!

When using the combo above, if I try to insert any record whatsoever, SQL tells me I've broken my check constraint. I can remove all rows from the table and try to insert a first record, and SQL tells me I've broken my constraint, which I can't possibly have done!

我已經(jīng)在互聯(lián)網(wǎng)上搜索了一段時間,現(xiàn)在正在尋找 UDF 依賴于多個表列的檢查約束示例,但無濟于事.關于為什么這可能不起作用的任何想法?

I've scoured the internet for quite a while now looking for examples of check constraints where the UDF depends on multiple table columns, but to no avail. Any ideas as to why this might not work?

提前致謝:)

推薦答案

是的,這可能看起來令人費解,直到您意識到發(fā)生了什么,此時它變得非常明顯.

Yes, this may seem baffling until you realise what's going on, at which point it becomes quite obvious.

為您嘗試插入的行中的值調(diào)用該函數(shù).但是想想函數(shù)是如何被調(diào)用的.調(diào)用它的是一個檢查約束.

The function is called for the values that are in the row you are trying to insert. But think of how the function is being called. It is a check constraint that calls it.

接下來,考慮傳遞的參數(shù).他們來自哪里?根據(jù)定義,檢查約束從 Int_1Int_2 列中獲取它們.

Next, think of the parameters being passed. Where do they come from? According to the definition, the check constraint takes them from columns Int_1 and Int_2.

因此,它將它們作為列值傳遞.但是列值必須屬于一行.在這種情況下是哪一行?您要插入的那個!

So, it passes them as column values. But column values must belong to a row. Which row is it in this case? The one you are trying to insert!

這意味著此時您的行插入,只有交易仍在等待中.然而,該行在表中這一事實至關重要,因為這就是函數(shù)通過 1 結(jié)果發(fā)現(xiàn)和報告的內(nèi)容.

That means your row is inserted at this point, only the transaction is still pending. And yet the fact that the row is in the table is crucial, because that's what the function finds and reports on with the 1 result.

因此,發(fā)生的事情是這樣的:

Thus, what's happening is this:

  • 您正在嘗試插入一行,

  • you are trying to insert a row,

函數(shù)看到該行并說具有給定參數(shù)的行已經(jīng)存在,

the function sees that row and says that a row with the given parameters already exists,

檢查約束通過禁止插入相應地反應",

the check constraint "reacts" accordingly by prohibiting the insert,

插入回滾.

當然,既然您意識到了這一切,就很容易想出不同的檢查重復項的邏輯.基本上,您的函數(shù)應該記住"新行已經(jīng)在表中,因此它應該嘗試確定它在表中的存在是否違反了您想要建立的任何規(guī)則.例如,您可以計算與給定參數(shù)匹配的行數(shù),并查看結(jié)果是否不大于 1:

Of course, now that you realise all that, it is easy to come up with a different logic of checking for duplicates. Basically, your function should "keep in mind" that the new row is already in the table, and so it should try and determine whether its presence in the table violates any rules that you want to establish. You could, for instance, count the rows matching the given parameters and see if the result is not greater than 1:

IF (SELECT COUNT(*) FROM [Table] WHERE Int_1 = @Int_1 AND Int_2 = @Int_2) < 2
BEGIN
    SET @Result = 1
END
ELSE 
BEGIN
    SET @Result = 0
END

但是,在此作業(yè)的檢查約束中使用函數(shù)的整個想法遠不如僅在兩列上添加唯一約束,如由@a_horse_with_no_name 建議.這樣做:

However, the entire idea of using a function in a check constraint for this job is very much inferior to just adding a unique constraint on the two columns, as suggested by @a_horse_with_no_name. Do this:

ALTER TABLE [Table]
ADD CONSTRAINT UQ_Table_Int1_Int2 UNIQUE (Int_1, Int_2);

你可以忘記重復.

這篇關于檢查具有多個輸入?yún)?shù)的約束 UDF 不起作用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉(zhuǎn)換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創(chuàng)建視圖?)
Create calculated value based on calculated value inside previous row(根據(jù)前一行內(nèi)的計算值創(chuàng)建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發(fā)技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉(zhuǎn)換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 日本黄视频在线观看 | 男人天堂免费在线 | 久久免费精品 | caoporn地址| 91玖玖| 欧美一区二区大片 | 狠狠入ady亚洲精品经典电影 | 欧美一二三 | 亚洲精品久久久久久下一站 | 精品在线视频播放 | 在线观看中文视频 | 日韩国产在线 | 日韩一区二区在线播放 | 午夜成人在线视频 | 亚洲欧美成人影院 | 九色在线视频 | 中文字幕亚洲精品 | 久久久久久国产精品免费免费 | 五月综合久久 | 99热电影| 欧美成人激情视频 | 成人二区 | 日本欧美在线视频 | 中文字幕在线观看日韩 | 国产高清一区二区三区 | 久久在线免费 | 欧美激情国产日韩精品一区18 | 亚洲精品久久久一区二区三区 | 91极品欧美视频 | 国产在线观看一区二区三区 | 国产传媒在线观看 | 一区二区视频在线 | 狠狠操狠狠干 | 天天操天天射天天舔 | 国产精品久久视频 | 精品福利在线视频 | 国产精品久久久久无码av | 91久久网站 | 日韩在线视频播放 | 国产精品九九九 | 欧美一区二区三 |