基本上我想PRINT
在用户定义的函数中使用语句来帮助我的调试。
但是我收到以下错误;
在函数内的“打印”中无效使用副作用或时间相关的运算符。
这不能做吗?
无论如何帮助我的用户定义函数调试?
基本上我想PRINT
在用户定义的函数中使用语句来帮助我的调试。
但是我收到以下错误;
在函数内的“打印”中无效使用副作用或时间相关的运算符。
这不能做吗?
无论如何帮助我的用户定义函数调试?
提示:产生错误。
declare @Day int, @Config_Node varchar(50)
set @Config_Node = 'value to trace'
set @Day = @Config_Node
您将收到此消息:
将 varchar 值“要跟踪的值”转换为数据类型 int 时转换失败。
不,对不起。SQL Server 中的用户定义函数非常有限,因为它们需要确定性。据我所知,没有办法绕过它。
您是否尝试过使用 Visual Studio 调试 SQL 代码?
我通过暂时将我的函数重写为这样的东西来解决这个问题:
IF OBJECT_ID ('[dbo].[fx_dosomething]', 'TF') IS NOT NULL
drop function [dbo].[fx_dosomething];
GO
create FUNCTION dbo.fx_dosomething ( @x numeric )
returns @t table (debug varchar(100), x2 numeric)
as
begin
declare @debug varchar(100)
set @debug = 'printme';
declare @x2 numeric
set @x2 = 0.123456;
insert into @t values (@debug, @x2)
return
end
go
select * from fx_dosomething(0.1)
过去,我倾向于分两个阶段处理我的功能。第一阶段是将它们视为相当正常的 SQL 查询,并确保我从中得到正确的结果。在我确信它按预期执行后,我会将其转换为 UDF。
使用扩展过程 xp_cmdshell 运行 shell 命令。我用它将输出打印到文件:
exec xp_cmdshell 'echo "mytextoutput" >> c:\debuginfo.txt'
如果文件 debuginfo.txt 不存在,这将创建它。然后它将文本“mytextoutput”(不带引号)添加到文件中。对该函数的任何调用都将写入额外的一行。
您可能需要先启用此 db-server 属性(默认 = 禁用),但我意识到这可能不符合生产环境中 dba 的喜好。
你不能。
function
您可以从 a调用 astored procedure
并调试 a stored procedure
(这将进入function
)
您可以尝试返回要检查的变量。例如我有这个功能:
--Contencates seperate date and time strings and converts to a datetime. Date should be in format 25.03.2012. Time as 9:18:25.
ALTER FUNCTION [dbo].[ufn_GetDateTime] (@date nvarchar(11), @time nvarchar(11))
RETURNS datetime
AS
BEGIN
--select dbo.ufn_GetDateTime('25.03.2012.', '9:18:25')
declare @datetime datetime
declare @day_part nvarchar(3)
declare @month_part nvarchar(3)
declare @year_part nvarchar(5)
declare @point_ix int
set @point_ix = charindex('.', @date)
set @day_part = substring(@date, 0, @point_ix)
set @date = substring(@date, @point_ix, len(@date) - @point_ix)
set @point_ix = charindex('.', @date)
set @month_part = substring(@date, 0, @point_ix)
set @date = substring(@date, @point_ix, len(@date) - @point_ix)
set @point_ix = charindex('.', @date)
set @year_part = substring(@date, 0, @point_ix)
set @datetime = @month_part + @day_part + @year_part + ' ' + @time
return @datetime
END
当我运行它时.. 我得到:Msg 241, Level 16, State 1, Line 1 从字符串转换日期和/或时间时转换失败。
啊!!
那么,我该怎么办?
ALTER FUNCTION [dbo].[ufn_GetDateTime] (@date nvarchar(11), @time nvarchar(11))
RETURNS nvarchar(22)
AS
BEGIN
--select dbo.ufn_GetDateTime('25.03.2012.', '9:18:25')
declare @day_part nvarchar(3)
declare @point_ix int
set @point_ix = charindex('.', @date)
set @day_part = substring(@date, 0, @point_ix)
return @day_part
END
我得到'25'。所以,我差了一个,所以我换成..
set @day_part = substring(@date, 0, @point_ix + 1)
瞧!现在它可以工作了:)
在我看来,每当我想打印或调试一个函数时。我将复制它的内容以作为普通 SQL 脚本运行。例如
我的功能:
create or alter function func_do_something_with_string(@input nvarchar(max)) returns nvarchar(max)
as begin
-- some function logic content
declare @result nvarchar(max)
set @result = substring(@input , 1, 10)
-- or do something else
return @result
end
然后我只是复制并运行这个函数来调试
declare @input nvarchar(max) = 'Some string'
-- some function logic content
declare @result nvarchar(max)
set @result = substring(@input , 1, 10)
-- this line is added to check while debugging
print @result
-- or do something else
-- print the final result
print @result