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 2000 Forums
 Transact-SQL (2000)
 user definition function in database name uses

Author  Topic 

erdem12345
Starting Member

8 Posts

Posted - 2008-04-26 : 04:06:43
how function parameters other databasename (@dbname) use ?

CREATE FUNCTION fnGetKur(@date datetime,@exchange varchar(5),@type tinyint,@dbname varchar(30)) RETURNS float AS
BEGIN
DECLARE @rate float
set @rate=0
select @rate=K1.TUTAR4 from @dbname.dbo.KUR K where K.D=@exchange and K.TARIH=(select max(K1.DATE) from @dbname.dbo.KUR K1 where K1.D=@exchange and K1.DATE <=@date)
return @rate
end

erdem12345
Starting Member

8 Posts

Posted - 2008-04-26 : 04:08:56
dbo.fnGetKur('2008-04-18','Eur','0','GeneralDB') error...
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-04-26 : 04:50:48
You need to use dynamic sql for this:-

CREATE FUNCTION fnGetKur(@date datetime,@exchange varchar(5),@type tinyint,@dbname varchar(30)) RETURNS float AS 
BEGIN
DECLARE @rate float
set @rate=0
select @Sql='select @rate=K1.TUTAR4 from ' + @dbname +'.dbo.KUR K where K.D='+ @exchange +' and K.TARIH=(select max(K1.DATE) from ' + @dbname+'.dbo.KUR K1 where K1.D='+ @exchange + ' and K1.DATE <='+ @date +')'
return @rate
end
Go to Top of Page

erdem12345
Starting Member

8 Posts

Posted - 2008-04-26 : 07:34:36
thank you but return @rate is 0 invalid value
Go to Top of Page

erdem12345
Starting Member

8 Posts

Posted - 2008-04-26 : 07:38:06
what is @Sql execute ???
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-04-26 : 08:15:54
[code]CREATE FUNCTION fnGetKur(@date datetime,@exchange varchar(5),@type tinyint,@dbname varchar(30)) RETURNS float AS
BEGIN
DECLARE @rate float
set @rate=0
select @Sql='select @rate=K1.TUTAR4 from ' + @dbname +'.dbo.KUR K where K.D='+ @exchange +' and K.TARIH=(select max(K1.DATE) from ' + @dbname+'.dbo.KUR K1 where K1.D='+ @exchange + ' and K1.DATE <='+ @date +')'
EXEC(@Sql)
return @rate
end[/code]

Sorry my bad...missed EXEC statement
Go to Top of Page

erdem12345
Starting Member

8 Posts

Posted - 2008-04-26 : 08:27:21
first error - Must declare the variable '@Sql' -

CREATE FUNCTION fnGetKur(@date datetime,@exchange varchar(5),@type tinyint,@dbname varchar(30)) RETURNS float AS
BEGIN
DECLARE @rate float

DECLARE @Sql varchar --

set @rate=0
select @Sql='select @rate=K1.TUTAR4 from ' + @dbname +'.dbo.KUR K where K.D='+ @exchange +' and K.TARIH=(select max(K1.DATE) from ' + @dbname+'.dbo.KUR K1 where K1.D='+ @exchange + ' and K1.DATE <='+ @date +')'
EXEC(@Sql)
return @rate
end


second error - Invalid use of 'EXECUTE' within a function.
Go to Top of Page
   

- Advertisement -