function in a sql statement

  • 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.

  • 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

  • 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

  • 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

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

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

    - 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

  • 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.

  • 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.:doze:

    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

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

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

    - 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

  • 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.

  • 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

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

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

    - 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

  • This is the classic behaviour I wanted to point to in my warnings

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

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

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

    - 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

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply