Author |
Topic |
saurabhjj
Starting Member
13 Posts |
Posted - 2007-09-21 : 07:48:25
|
hi friends, i want to write a user defined function which will return a value. then i want to catch the value returned in another variable from another stored procedure or function, i am not able to do this, pls help me out and if possible please give me a sample code also |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-09-21 : 07:52:41
|
I have no idea what you want. But this is how you assign the value from a UDF to a variableselect @var = dbo.yourudf() Can you describe more in detail and preferably with some example ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-09-21 : 07:53:36
|
[code]create function dbo.test (@a int)returns intasbegin return(@a*@a)endgo-- Retrieve function value in variabledeclare @x intset @x = dbo.test(2)select @x[/code]Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-09-21 : 08:15:56
|
from email :quote: i have written a parameterized user defined function, which has to return a valuei another procedure or function, i want to catch the value that the above function returns, i hope i was able to explain you what i want, please provide me with a sample code now
create function dbo.another_function(@a int)returns intasbegin declare @b int, @c int select @b = dbo.yourudf(@a) -- for example, perform some calculation select @c = @b * 1 / 1 return @cend KH[spoiler]Time is always against us[/spoiler] |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-09-21 : 08:20:03
|
orcreate function dbo.another_function(@a int)returns intasbegin declare @c int -- for example, perform some calculation select @c = dbo.yourudf(@a) * 1 / 1 return @cend MadhivananFailing to plan is Planning to fail |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-09-21 : 08:48:43
|
from email again.quote: thanks for your prompt replies,i think you have understood the problem but, please do one more thing, i am passing 2 parameters both being varchar to the function and am calling the fucntion from another procedure which is catching the value and displaying the output,the problem is, if i give the parameters in single or double quotes, then the ouput is null, but if i pass the parameters without any quotes then it fires an error, which says invlaid column name
saurabhjj,You can use the Quick Reply at the end of this page to reply. Isn't it better to have more people helping you than just one ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-09-21 : 08:49:44
|
quote: the problem is, if i give the parameters in single or double quotes, then the ouput is null, but if i pass the parameters without any quotes then it fires an error, which says invlaid column name
Please post the code of your function here. KH[spoiler]Time is always against us[/spoiler] |
 |
|
saurabhjj
Starting Member
13 Posts |
Posted - 2007-09-21 : 08:53:02
|
following is my function and am calling this function from another procedure, please help me outCREATE function dbo.fixed_assets (@datset varchar, @year varchar)returns moneyasbegindeclare @fa moneyselect @fa=sum(amount) from account_details,account_master where dataset_code=@datset and period=@yearand account_details.s_heading=account_master.s_heading and account_master.category='fixed_assets' group by account_details.dataset_codereturn (@fa)end--------calling procedureCREATE proc myproc asdeclare @x moneyset @x=dbo.fixed_assets1("itchotels001","2005")select @xGO |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-09-21 : 08:58:14
|
the size of your variable should correspond to the column size.CREATE function dbo.fixed_assets (@datset varchar(10), @year varchar(10)) why are you using varchar for @year ? @year clearly it is an integer value. KH[spoiler]Time is always against us[/spoiler] |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-09-21 : 09:04:04
|
Note that if you dont specify length for a varchar datatype, by default, 1 would be takenMadhivananFailing to plan is Planning to fail |
 |
|
|