久久久久久久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)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 免费黄网站在线观看 | 日韩福利一区 | 91成人精品一区在线播放 | 草草在线观看 | 亚洲小视频在线观看 | 久久久久网 | 成人黄色大片 | 欧美日韩亚洲一区二区三区 | 日本大尺度吃奶做爰久久久绯色 | 日本男人天堂 | 丁香六月激情 | 欧美日韩国产二区 | 黄色大片免费观看 | 黄色在线免费网站 | 国产伦精品一区二区免费 | 日本免费毛片 | 黑森林av| 美国式禁忌14在线 | 国产乱人乱偷精品视频 | 国产精品成人国产乱一区 | 亚洲视频免费 | 一区二区三区视频在线播放 | 国产在线一区二区三区 | 国产网站在线 | 午夜精品久久 | 精品一区二区三 | 日本69视频 | 98在线视频 | 18在线观看免费入口 | 成人在线免费视频 | 久久一区视频 | 成人激情在线观看 | 夜夜操夜夜 | 五月天精品 | 国产成人亚洲精品自产在线 | 激情久久五月天 | 欧美日韩一区在线 | 亚洲综合区 | 成人免费视频视频 | 国产aⅴ爽av久久久久成人 | 久久久一区二区三区 |