How to call a stored procedure from a user defined function in SQL Server 2000
I recently had a similar issue. In fact the error message is not properly formatted since sp_executesql is an extended stored procedure as you can check by the following script:
select objectproperty(object_id('sp_executesql'),'IsExtendedProc')
returns 1
Since we can’t use sp_executesql even it’s an XP, I had to find another workaround by using sp_OAMethod. My scenario was how to find the number of rows dynamically in a table according to some criteria (null values in my scenario). Using sp_OAMethod I built the following function:
IF object_id(N'dbo.fc_ContaRegistros_x_Criterio') is not null DROP FUNCTION [dbo].[fc_ContaRegistros_x_Criterio]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE FUNCTION dbo.fc_ContaRegistros_x_Criterio (
@str_TBName VARCHAR(100),
@str_Criter VARCHAR(500)
)
RETURNS BIGINT
AS
BEGIN
-- Objetivo : Contar numero de registros de uma determinada tabela de acordo com o critério passado
-- Criação : Josué Monteiro Viana - 09/07/09
/*
Exemplo:
DECLARE @count INT
SET @count = dbo.fc_ContaRegistros_x_Criterio('master.dbo.sysobjects', '')
PRINT @count
SET @count = dbo.fc_ContaRegistros_x_Criterio('crk.dbo.acao', 'where cod_acao is null')
PRINT @count
*/
DECLARE
@int_objSQL INT,
@int_erros INT,
@int_objSelectCountResult INT,
@bint_SelectCount BIGINT,
@sql NVARCHAR(2000)
EXEC @int_erros = sp_OACreate 'SQLDMO.SQLServer', @int_objSQL OUTPUT
EXEC @int_erros = sp_OASetProperty @int_objSQL, 'LoginSecure', TRUE
EXEC @int_erros = sp_OAMethod @int_objSQL, 'Connect', null, '.'
--SET @sql = 'SELECT count(*) FROM ' + @str_TBName + ' WHERE ' + @str_Criter
SET @sql = 'SELECT count(*) FROM ' + @str_TBName + ' ' + @str_Criter
SET @sql = 'ExecuteWithResults("' + @sql + '")'
EXEC @int_erros = sp_OAMethod @int_objSQL, @sql, @int_objSelectCountResult OUTPUT
EXEC @int_erros = sp_OAMethod @int_objSelectCountResult, 'GetRangeString(1, 1)', @bint_SelectCount OUT
EXEC @int_erros = sp_OADestroy @int_objSQL
-- debug info: not valid inside a fc
--if @int_erros <> 0 EXEC sp_OAGetErrorInfo @int_objSQL else print 'ok'
if @int_erros <> 0 SET @bint_SelectCount = @int_erros
RETURN @bint_SelectCount
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
I know your case is a little different, but I’m sure you can use this udf as a guideline to help you.
Best wishes, Josue Monteiro Viana