Friday, February 24, 2012

create function

I want to assign the querie to the ptvalue variable... i just want which mistake im making, it trow me an error near to the select.

create function pt
(@.idpt INT)
Returns INT
AS
BEGIN
declare @.ptvalue INT
SET @.ptvalue = select sqrt(a.px*a.px + a.py*a.py)
from abstractparticle as a
where @.idpt = a.id;
Return @.ptvalue
END

It looks to me like if you enclose your select statement with parends that your query will work.

create function pt
(@.idpt INT)
Returns INT
AS
BEGIN
declare @.ptvalue INT
SET @.ptvalue = (select sqrt(a.px*a.px + a.py*a.py)
from abstractparticle as a
where @.idpt = a.id);
Return @.ptvalue
END

I will give this a look in a sec for verification.

|||

Yes, the parends will fix the define; you can also define your function without the variable if you choose; something like this:

create function pt
(@.idpt INT)
Returns INT
AS
BEGIN
Return ( select sqrt(a.px*a.px + a.py*a.py)
from abstractparticle as a
where @.idpt = a.id)
END

|||

Another thing to consider is that the fact that this is a scalar function. Referencing this function will likely make your code more readable; however, keep in mind that scalar functions tend to have a negative impact on performance.

If you have queries that use this function that seem to perform badly one way to improve performance at the expense of readability will always be to replace the UDF function references with the SELECT statement that you have enclosed in parends.

No comments:

Post a Comment