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

為什么不能將 GETDATE() 用作過程參數的默認值或

Why can#39;t GETDATE() be used as the default value of a procedure parameter or a value in an EXECUTE statement?(為什么不能將 GETDATE() 用作過程參數的默認值或 EXECUTE 語句中的值?) - IT屋-程序員軟件開發技術分享
本文介紹了為什么不能將 GETDATE() 用作過程參數的默認值或 EXECUTE 語句中的值?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

考慮以下存儲過程:

CREATE OR ALTER PROCEDURE MySchema.MyProcedure
   @myDateTimeParam DATETIME = GETDATE()
AS
BEGIN
   -- Do something spectacular
END

調用時,參數聲明失敗并顯示錯誤將數據類型 nvarchar 轉換為日期時出錯".這可以通過如下更改代碼來解決:

Upon invocation, the parameter declaration fails with the error, "Error converting data type nvarchar to date." This can be worked around by changing the code as follows:

CREATE OR ALTER PROCEDURE MySchema.MyProcedure
   @myDateTimeParam DATETIME = NULL
AS
BEGIN

    IF @myDateTimeParam IS NULL
        SET @myDateTimeParam = GETDATE();

   -- Do something spectacular
END

但是,假設 @myDateTimeParam 沒有默認值:

However, assume that @myDateTimeParam did not have a default value:

CREATE OR ALTER PROCEDURE MySchema.MyProcedure
   @myDateTimeParam DATETIME
AS
BEGIN
   -- Do something spectacular
END

在這種情況下,您不能簡單地將 GETDATE() 作為形式參數傳遞如下:

In this case, you cannot simply pass GETDATE() as a formal parameter as follows:

EXEC MySchema.MyProcedure GETDATE()

因為這也會產生可怕的將數據類型 nvarchar 轉換為日期時出錯"的錯誤.唯一的解決方法是先聲明一個變量,然后傳遞該變量:

as this also produces the dreaded "Error converting data type nvarchar to date" error. The only workaround to this is to first declare a variable and then pass the variable:

DECLARE @myDateTimeParam DATETIME = GETDATE();
EXEC MySchema.MyProcedure @myDateTimeParam;

這是為什么?源數據類型和目標數據類型都是 DATETIME.理論上,將GETDATE()的結果作為參數的默認值或形參的值,都不會發生數據類型轉換錯誤.

Why is this? Both the source and target data types are DATETIME. In theory, a data type conversion error should not occur when using the result of GETDATE() as either the default value of a parameter or the value of a formal parameter.

是否有一些技術原因導致這不起作用?MSDN 文檔中沒有任何內容表明它不應該工作.

Is there some technical reason that this does not work? There's nothing in the MSDN documentation that indicates that it should not work.

推薦答案

這在文檔中有所介紹,CREATE PROCEDURE (Transact-SQL),在 default 子標題下>參數部分:

This is covered in the documentation, CREATE PROCEDURE (Transact-SQL), under the default subheading in the arguments section:

參數的默認值.如果為一個定義了默認值參數,程序可以在不指定值的情況下執行對于那個參數.默認值必須是常量,也可以是NULL. 常量值可以是通配符的形式,使得它將參數傳遞給參數時可以使用 LIKE 關鍵字程序.

A default value for a parameter. If a default value is defined for a parameter, the procedure can be executed without specifying a value for that parameter. The default value must be a constant or it can be NULL. The constant value can be in the form of a wildcard, making it possible to use the LIKE keyword when passing the parameter into the procedure.

強調我的.

GETDATE() 不是常量,因此不能用作 DEFAULT 值.因此,為什么您需要使用以下格式,因為 GETDATE() 的值是在運行時確定的:

GETDATE() is not a constant, so cannot be used an a DEFAULT value. Hence why you need to use the format below, as then the value of GETDATE() is determined at run time:

CREATE PROC YourProc @Param date = NULL
AS

    IF @Param IS NULL BEGIN
        SET @Param = GETDATE();
    END;
    ...

這篇關于為什么不能將 GETDATE() 用作過程參數的默認值或 EXECUTE 語句中的值?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
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屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 日韩中文不卡 | 成人激情免费视频 | 国产高清精品一区二区三区 | 婷婷开心激情综合五月天 | 欧美一区二区三区免费电影 | 高清黄色毛片 | 日日摸夜夜添夜夜添精品视频 | 99re国产精品 | 欧美一区视频在线 | 国产精品毛片一区二区在线看 | 免费国产视频在线观看 | 99热热精品| 亚洲日本视频 | 国产精品99久久久久久动医院 | 亚洲不卡av在线 | 亚洲国产精品99久久久久久久久 | 国产精品久久久久久久久久不蜜臀 | 久久免费视频在线 | 国产探花 | 国产欧美日韩精品在线观看 | 99精品一区二区 | 91精品久久久久久久久中文字幕 | 亚洲一区电影 | 国产成人福利在线 | 久久久女女女女999久久 | 中文字幕11页 | 国产精品99久久久久 | 日韩在线观看视频一区 | 久久综合欧美 | 欧美一区二区三区在线观看视频 | 成人免费激情视频 | 久久精品| 亚洲精品女优 | 91丨九色丨国产在线 | 国产高清一二三区 | 欧美精品一区二区三区四区五区 | 日韩国产在线观看 | 九九热免费在线观看 | 国产日韩欧美一区二区 | 久久99国产精品 | 亚洲国产精品久久人人爱 |