Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 sendind a variable with the name table to function

Author  Topic 

psfaro
Starting Member

49 Posts

Posted - 2010-05-20 : 14:30:55
Hi

How can this be work
I need to return a value(with a function) from a table , but the name of the table is dynamic .

when i run the code i got the message
Only functions and some extended stored procedures can be executed from within a function.



ALTER FUNCTION [dbo].[RetCFin]
(
@ano varchar(20) )

RETURNS varchar(20)
AS
BEGIN

DECLARE @conta_fin varchar(300)
declare @cmd varchar(300)
declare @tab varchar(20)

--
set @tab='cctpoc'+@ano

set @conta_fin=
'select cfin_pa FROM [' + @tab +']'

EXEC sp_executesql @conta_fin

RETURN @conta_fin

END

Regards.

Pedro Faro

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-05-20 : 14:42:35
You can't use dynamic SQL from within a function. It just isn't possible.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -