Skip to main content
2 of 4
added 262 characters in body
Racer SQL
  • 7.5k
  • 14
  • 73
  • 136

Error with FUNCTION ( SQL Server 2008 ) - ''Cannot find either column...'

Friends,

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) values 
    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?

Thanks

EDIT:

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.

Racer SQL
  • 7.5k
  • 14
  • 73
  • 136