【问题标题】:Unable to get convert() function to provide correct parameter to a stored procedure无法获取 convert() 函数以向存储过程提供正确的参数
【发布时间】:2014-03-26 15:59:54
【问题描述】:

我有以下功能:

create procedure log_message
  @p as varchar(20),
  @r as varchar(20),
  @l as varchar(20),
  @m as varchar(max)
as
begin
  insert into logs (time_stamp, program, routine, location, [message])
  values (getdate(), @p, @r, @l, @m)
end

我可以通过以下方式轻松记录消息:

exec dbo.log_message 'test code', 'this routine', 'right here', 'some message'

这很好用。

但是,当我尝试此操作时出现错误:

declare @x int;
set @x = 3
exec dbo.log_message 'test code', 'this routine', 'right here', convert(varchar(10), @x)

我可以像这样调用它来解决这个问题:

declare @x int;
declare @m varchar(10);
set @x = 3;
set @m = convert(varchar(10), @x)
exec dbo.log_message 'test code', 'this routine', 'right here', @m

但这似乎非常迟钝,我很难相信人们不能以更自然的方式编写它。我错过了什么?

更糟!当我在玩这个试图弄清楚什么有效,什么无效时,我意识到这不起作用:

exec dbo.log_message 'test code', 'this routine', 'right here', 'a' + 'b'

我的第一个想法是......它不可能像这样工作。是否有另一种方法可以像普通编程语言一样调用 SQL 中的存储过程?

【问题讨论】:

标签: sql-server tsql stored-procedures parameter-passing


【解决方案1】:

抱歉,您不能将 EXPRESSION 作为参数传递给 SP。您只能使用 VALUES

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-24
    相关资源
    最近更新 更多