Tuesday, July 23, 2019

Debug Message in Sql Server Function

Add below scripts into Function(Need relative permission) then it would send the debug message to specified file.


 -- Enable advanced options to be changed.
EXEC SP_CONFIGURE 'show advanced options', 1
GO
RECONFIGURE
GO
-- Enable xp_cmdshell option.
EXEC SP_CONFIGURE N'xp_cmdshell', 1
GO
RECONFIGURE
GO

declare @Result1 decimal (38, 10) = 12.1
declare @a varchar(500) = 'echo "' + cast(@Result1 as varchar(100)) + '" >> \\cnshdbqin01\Test\debuginfo.txt'
exec xp_cmdshell 'net use \\cnshdbqin01\Test "Winter_02" /USER:infor\bqin'
exec xp_cmdshell @a

-- Disable xp_cmdshell option. 
EXEC SP_CONFIGURE 'xp_cmdshell', 0
GO
RECONFIGURE
GO
-- Disable advanced options to be changed.
EXEC SP_CONFIGURE 'show advanced options', 0
GO
RECONFIGURE
GO

No comments:

Post a Comment