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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 TSQL Function

Author  Topic 

CORRUPT27
Starting Member

4 Posts

Posted - 2008-06-20 : 17:11:08
Hey guys,
I have been trying to get a function to work but have been having a hard time. The tables that are used are a translation and validation tables. But the translation can be from different tables. What i would like the function to do is take in a 'description of a type' and a language code and send back the translated version. The issue is that the table is dynamic and there is my issue with my function.

ALTER FUNCTION [dbo].[Trans]
(
@Trans_description AS NVarchar(200),
@languageid AS nvarchar(10)
)
RETURNS nvarchar(50)
AS
BEGIN
DECLARE
@return AS nvarchar(50),
@TABLENAME AS nvarchar(50),
@sql AS nvarchar(100)
set @TableName = (select top(1) listtable from validationType where validationtypeDesc = @Trans_description)

SET @return =
(SELECT TOP (1) ValidationDesc FROM [translationvalidationlist] WHERE validationpartyid IN
(SELECT validationpartyid FROM @TableName WHERE validationtypePartyid IN
(select validationtypepartyid FROM validationtype WHERE validationtypeDesc = @Trans_description
))AND LANGUAGEID = @languageid)


RETURN @return



END

Is there anyway to do a @return = exec(@sql) of course sql would be the current @return. Right now i get the error Must declare the table variable "@TableName".


Thank you so much for all your help.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-06-20 : 17:13:00
You can use sp_executesql to get data back from dynamic SQL.

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

Subscribe to my blog
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-06-20 : 17:13:16
You can't do that in a function, since functions do not accept dynamic sql.
However, if the number of table names are fixed (always one of for example 5), you can have if statements further down.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

CORRUPT27
Starting Member

4 Posts

Posted - 2008-06-23 : 09:03:38
Their not fixed. But thank you so much for the help
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-23 : 10:01:54
quote:
Originally posted by CORRUPT27

Their not fixed. But thank you so much for the help


Why? cant you try using a procedure for the same?
Go to Top of Page
   

- Advertisement -