Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

function in a sql statement Expand / Collapse
Author
Message
Posted Saturday, September 6, 2008 5:44 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, May 9, 2012 4:31 AM
Points: 89, Visits: 231

How can the function be used in MS SQL.I have afunction which retruns value & date.I would like to use this to fetch records which I do in Oracle.I am not sure how this can be done in MS SQL

Its like this

Select [Company Name],Patient,Visit,[Visit Date],[Insurance Company],
InsPolicyVal(A.Visit) -- this is the function which should return value
from [Medical Visit] A ON H.Patient = A.Patient INNER JOIN
[Payment Policies] C ON A.[Insurance Policy] = C.Policy INNER JOIN
[Medical Policy Link] D ON C.EmployerInsuranceLink = D.ID INNER JOIN
dbo.[Insurance Companies] E ON D.[Insurance Company ID] = E.[Insurance Company]

the InsPolicyVal is the function which should retrun the insurance value for a specific visit.
I have tried using Outer join but the out does not seem to be correct.
Please if some one can help.
Post #565013
Posted Saturday, September 6, 2008 5:58 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, April 22, 2014 6:56 AM
Points: 1,330, Visits: 1,434
Hello,

Is InsPolicyVal a Scalar or Table function? If it’s Scalar then you should be able to use it as in your example. If it is a Table function then you would probably want to include it in a Join.

Regards,

John Marsh


www.sql.lu
SQL Server Luxembourg User Group
Post #565014
Posted Saturday, September 6, 2008 12:53 PM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Yesterday @ 3:36 PM
Points: 727, Visits: 1,420
You need to call it using a two part identifier.

In this case it is probably dbo.InsPolVal(data). If the function has a schema, use that in replacement of the dbo. (policy.InsPolVal)

Also, unless I am much mistaken if your function is returning more than 1 value, you cannot use it in a select statement. It must be a scalar value function to be used in the select statement.

Hope that helps
Post #565068
Posted Sunday, September 7, 2008 3:47 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, September 26, 2014 7:44 AM
Points: 6,731, Visits: 8,476
Why would you want to use a function if you can do a join ?????

Most of the time it's these kind of functions that mess up response times in your applications.. blaming sqlserver..

Making it hard to discover, fine tune, problem solve,.... hidden joins are one of the worst.


Johan


Don't drive faster than your guardian angel can fly ...
but keeping both feet on the ground won't get you anywhere

- How to post Performance Problems
- How to post data/code to get the best help


- How to prevent a sore throat after hours of presenting ppt ?


"press F1 for solution", "press shift+F1 for urgent solution"


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me but most of the time this is me
Post #565111
Posted Sunday, September 7, 2008 7:12 AM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Yesterday @ 3:36 PM
Points: 727, Visits: 1,420
It has always been my policy to used functions to replace repetative coding. Why do I need to continually type to code out to verify that a customer is active when I can put it in a function and type out less than a line of code to do it. To me this is really the best use of a function.
Post #565122
Posted Sunday, September 7, 2008 9:51 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, September 26, 2014 7:44 AM
Points: 6,731, Visits: 8,476
Fraggle (9/7/2008)
It has always been my policy to used functions to replace repetative coding. Why do I need to continually type to code out to verify that a customer is active when I can put it in a function and type out less than a line of code to do it. To me this is really the best use of a function.


It may indeed server you well, for now.

There are a number of articles at SSC which handle set based operations and try to explain the issues with hidden joins, row by row operations (rbar), ...

I'd prefer a view over a function, because it still points the problem solving dba by nature to join analysis and troubleshooting.

A single function in your query may be easy to detect and analyse, but the number of functions you use in a single query can be huge, now figure the out the troubleshooting part.


Johan


Don't drive faster than your guardian angel can fly ...
but keeping both feet on the ground won't get you anywhere

- How to post Performance Problems
- How to post data/code to get the best help


- How to prevent a sore throat after hours of presenting ppt ?


"press F1 for solution", "press shift+F1 for urgent solution"


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me but most of the time this is me
Post #565135
Posted Sunday, September 7, 2008 6:47 PM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Yesterday @ 3:36 PM
Points: 727, Visits: 1,420
ALZDBA, I will have to take your word for it as I haven't read those articles. I can see what you are saying though if you are calling numerous functions inside of a store proc. That would be difficult to diagnose performance issues.

Post #565187
Posted Sunday, September 7, 2008 11:54 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, September 26, 2014 7:44 AM
Points: 6,731, Visits: 8,476
Indeed.
Also keep in mind functions are directly available to any sql statement, not only from within a stored procedure.



Also keep in mind you need to provide the actual schema for the function

Select [Company Name],Patient,Visit,[Visit Date],[Insurance Company],
dbo.InsPolicyVal(A.Visit) -- this is the function which should return value
from [Medical Visit] A ON H.Patient = A.Patient INNER JOIN
[Payment Policies] C ON A.[Insurance Policy] = C.Policy INNER JOIN
[Medical Policy Link] D ON C.EmployerInsuranceLink = D.ID INNER JOIN
dbo.[Insurance Companies] E ON D.[Insurance Company ID] = E.[Insurance Company]



Johan


Don't drive faster than your guardian angel can fly ...
but keeping both feet on the ground won't get you anywhere

- How to post Performance Problems
- How to post data/code to get the best help


- How to prevent a sore throat after hours of presenting ppt ?


"press F1 for solution", "press shift+F1 for urgent solution"


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me but most of the time this is me
Post #565220
Posted Sunday, September 14, 2008 8:04 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, September 26, 2014 7:44 AM
Points: 6,731, Visits: 8,476
This is the classic behaviour I wanted to point to in my warnings

http://www.sqlservercentral.com/Forums/Topic567766-8-1.aspx


Johan


Don't drive faster than your guardian angel can fly ...
but keeping both feet on the ground won't get you anywhere

- How to post Performance Problems
- How to post data/code to get the best help


- How to prevent a sore throat after hours of presenting ppt ?


"press F1 for solution", "press shift+F1 for urgent solution"


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me but most of the time this is me
Post #569171
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse