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

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

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

問題描述

考慮以下存儲過程:

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

調用時,參數(shù)聲明失敗并顯示錯誤將數(shù)據(jù)類型 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() 作為形式參數(shù)傳遞如下:

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

EXEC MySchema.MyProcedure GETDATE()

因為這也會產生可怕的將數(shù)據(jù)類型 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;

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

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 子標題下>參數(shù)部分:

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

參數(shù)的默認值.如果為一個定義了默認值參數(shù),程序可以在不指定值的情況下執(zhí)行對于那個參數(shù).默認值必須是常量,也可以是NULL. 常量值可以是通配符的形式,使得它將參數(shù)傳遞給參數(shù)時可以使用 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() 用作過程參數(shù)的默認值或 EXECUTE 語句中的值?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

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?(如何從多個表創(chuàng)建視圖?)
Create calculated value based on calculated value inside previous row(根據(jù)前一行內的計算值創(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)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: av第一页 | 精品91久久| 国产精品毛片一区二区在线看 | 日韩欧美一区二区在线播放 | 国产蜜臀97一区二区三区 | 亚洲天堂男人的天堂 | 免费网站国产 | jav成人av免费播放 | 中文字幕欧美日韩 | 亚洲精品丝袜日韩 | 欧美日韩国产高清 | 欧美在线激情 | 国产精品久久久久免费 | 99影视 | 亚洲麻豆| 久久久久亚洲精品 | 免费播放一级片 | 久久久www | 欧美日韩在线播放 | 一区二区福利视频 | 四虎影音| 成人国产一区二区三区精品麻豆 | 亚洲精品9999 | 婷婷激情综合 | 中文在线播放 | 日韩精品久久 | 正在播放国产精品 | 97精品超碰一区二区三区 | 久久久一区二区三区四区 | 又黄又爽的网站 | 夜夜操av | 久久逼逼 | 亚洲欧美一区二区三区国产精品 | 国产综合久久久 | 91精品国产91久久综合桃花 | 午夜影院在线观看免费 | 国产成人精品一区二区三 | 皇色视频在线 | 亚洲视频在线播放 | 夜夜骑首页| 天天综合久久 |