儲存過程引數的時間預設值解決方法

2021-04-21 09:49:46 字數 1062 閱讀 2887

在資料庫中建立儲存過程的時候,引數的預設值是必須為常量或null的,因此對於希望將時間型別引數的預設值設為當前時間的朋友來說,就會出現操作不當的情況了。

解決方法很簡單,只需將其預設值設定延後就可以了。

如:create procedure test

@testdate datetime=null

asif(@testdate is null)

set @testdate=getdate()

...do something what you want

...go

參考了starspeak的文章 http://www.cnblogs.com/starspeak/archive/2008/05/27/1208400.html

示例:if object_id ( 'dbo.usp_test', 'p' ) is not null

drop procedure dbo.usp_test;

goset ansi_nulls on

goset quoted_identifier on

gocreate procedure [dbo].[usp_test]

@datetime datetime = null

asbegin

set nocount on;

if(@datetime is null)

set @datetime = getdate();

declare @datestr varchar(20); --日期字串,短格式,yymmdd 即080715

declare @hour int; --小時數

set @datestr = convert(char(6),dateadd(hh,-1,@datetime),12); 

set @hour = datepart(hh,@datetime);

print @datestr;

print @hour;

endgo

參考:starspeak http://www.cnblogs.com/starspeak/archive/2008/05/27/1208400.html

方法引數 預設值 Python引數的預設值陷阱!

今日分享 引數的預設值陷阱 下面定義的函式f,其引數d是乙個預設引數,且為字典型別 def f a,d print f a print f d do some process return d 最後返回字典d,下面呼叫函式f ret dict f 1 第二個引數d使用預設值 ret dict b 2...

方法引數 預設值 引數的預設值陷阱!

今日分享 引數的預設值陷阱 下面定義的函式f,其引數d是乙個預設引數,且為字典型別 def f a,d print f a print f d do some process return d 最後返回字典d,下面呼叫函式f ret dict f 1 第二個引數d使用預設值 ret dict b 2...

lua 函式 預設值 定義函式引數的預設值

如果你想要命名引數和預設值,如php或python,你可以使用表構造函式呼叫你的函式 myfunction 函式本身可以有這樣的簽名 function myfunction t setmetatable t,local a,c t 1 or t.a,t 2 or t.b,t 3 or t.c fun...