【问题标题】:insert or update the date value as sysdate via stored procedure通过存储过程将日期值插入或更新为 sysdate
【发布时间】:2015-01-17 09:02:40
【问题描述】:

我有一个程序:

create proc proc_ins_upd_exp
    @id             int = 0,
    @itemname       nvarchar(200),
    @price          decimal,
    @dateofbilling  date
as
begin
    set nocount on;

    if (@id = 0)
       insert into exp_tbl (itemname, price, dateofbilling) 
       values (@itemname, @price, @dateofbilling)
    else
       update exp_tbl
       set itemname = isnull (@itemname, itemname),
           price = isnull (@price, price),
           dateofbilling = isnull (@dateofbilling , dateofbilling)
       where id = @id

   set nocount off;
end;

在表exp_tbl中,列dateofbilling的数据类型为date

如何通过存储过程将dateofbilling 的值作为getdate 插入,或者当该值为NULL 时使用sysdate 更新dateofbilling 的值?

当我尝试执行存储过程时,我得到了错误。

execute proc_ins_upd_exp 
     @itemname = 'test', @price = 100, @dateofbilling = getdate()

【问题讨论】:

  • 哪里出错了??

标签: sql sql-server tsql stored-procedures


【解决方案1】:

您可以将文字值或变量传递给执行... 一般来说(除了少数例外)你不能有任何形式的复杂表达式或函数调用

Declare @currentDate date = getdate()
execute proc_ins_upd_exp @itemname = 'test', @price = 100, @dateofbilling = @currentDate

【讨论】:

  • 声明@curentdate date = getdate()
【解决方案2】:
ALTER Procedure proc_ins_upd_exp
    @id             int = 0,
    @itemname       nvarchar(200),
    @price          decimal,
    @dateofbilling  date = null
AS
IF @dateofbilling is null
SET @dateofbilling = getdate()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    • 2017-12-02
    • 1970-01-01
    相关资源
    最近更新 更多