久久久久久久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ā)技術(shù)分享
本文介紹了為什么不能將 GETDATE() 用作過程參數(shù)的默認值或 EXECUTE 語句中的值?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

考慮以下存儲過程:

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

調(diào)用時,參數(shù)聲明失敗并顯示錯誤將數(shù)據(jù)類型 nvarchar 轉(zhuǎn)換為日期時出錯".這可以通過如下更改代碼來解決:

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

但是,假設(shè) @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()

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

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()的結(jié)果作為參數(shù)的默認值或形參的值,都不會發(fā)生數(shù)據(jù)類型轉(zhuǎn)換錯誤.

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.

是否有一些技術(shù)原因?qū)е逻@不起作用?MSDN 文檔中沒有任何內(nèi)容表明它不應(yīng)該工作.

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 關(guān)鍵字程序.

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.

強調(diào)我的.

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;
    ...

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

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

相關(guā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)換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 亚洲欧美日韩激情 | 国产精品亚洲第一区在线暖暖韩国 | 国产免费自拍 | 97精品国产97久久久久久免费 | 国产免费av在线 | 久久88 | 亚洲欧美日韩一区二区 | 亚洲欧美激情精品一区二区 | 波多野结衣精品在线 | 国产成人精品一区二区三区视频 | 国产露脸对白88av | 精品欧美乱码久久久久久 | 天天操天天干天天曰 | 中文字字幕一区二区三区四区五区 | 国产中文字幕网 | 国产成人精品免费 | 亚洲视频一区在线 | 日韩av免费在线观看 | 国产二区在线播放 | 久久这里只有精品首页 | 美女视频一区二区三区 | 国产精品一区二 | 成人精品一区亚洲午夜久久久 | 欧美国产日韩在线观看成人 | 久久久成人精品 | 国产精品18久久久久久白浆动漫 | 亚洲精品专区 | 免费在线观看一级毛片 | 日韩另类| 日韩免费一区二区 | 日本久久久久久 | 午夜欧美| 欧美日韩高清 | 国产99精品 | 欧美不卡 | 国产99热| 久久久久久天堂 | 久久精品国产清自在天天线 | 午夜av影院| 国产欧美一区二区三区久久 | 亚洲精品1区 |