I have this Function:
CREATE FUNCTION [dbo].[fn_xxx] ( @Nome VARCHAR(100), @Descricao VARCHAR(500) ) RETURNS @tbDados TABLE ( Qtd INT ) AS BEGIN DECLARE @CMD AS NVARCHAR(MAX); DECLARE @Tabela VARCHAR(100); DECLARE @CampoDescricao VARCHAR(100); --- *** Identifica os campos e tabelas SET @Tabela = 'tbl' + @Nome; SET @CampoDescricao = 'Desc' + @Nome; SET @CMD = ' INSERT INTO @tbDados (Qtd) SELECT @Qtd = COUNT(*) FROM ' + @Tabela + ' WHERE ' + @CampoDescricao + ' = ''' + replace(@Descricao, '''', '`') + ''';'; EXEC sp_executesql @CMD; RETURN END
I'm trying to help developers here, but with no success.
When we execute it (
select dbo.fn_xxx ('processador','teste')) it give us this error:
Cannot find either column "dbo" or the user-defined function or aggregate "dbo.fn_xxx", or the name is ambiguous.
I've been searching through the internet, and found some results, but it was all about the code.
can someone help me how to figure out this error in this function?
If i execute it with:
SELECT * FROM [EasyTablet].[dbo].[fn_xxx] ( 'processador','teste' ) GO
It shows me this error:
Only functions and some extended stored procedures can be executed from within a function.