Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


function in a sql statement


function in a sql statement

Author
Message
mathewspsimon
mathewspsimon
SSC Journeyman
SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)

Group: General Forum Members
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.
John H Marsh
John H Marsh
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1341 Visits: 1455
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
Fraggle-805517
Fraggle-805517
Right there with Babe
Right there with Babe (733 reputation)Right there with Babe (733 reputation)Right there with Babe (733 reputation)Right there with Babe (733 reputation)Right there with Babe (733 reputation)Right there with Babe (733 reputation)Right there with Babe (733 reputation)Right there with Babe (733 reputation)

Group: General Forum Members
Points: 733 Visits: 1510
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
ALZDBA
ALZDBA
SSCertifiable
SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)

Group: General Forum Members
Points: 6974 Visits: 8839
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 w00t

- 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" :-D


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me Alien but most of the time this is me Hehe
Fraggle-805517
Fraggle-805517
Right there with Babe
Right there with Babe (733 reputation)Right there with Babe (733 reputation)Right there with Babe (733 reputation)Right there with Babe (733 reputation)Right there with Babe (733 reputation)Right there with Babe (733 reputation)Right there with Babe (733 reputation)Right there with Babe (733 reputation)

Group: General Forum Members
Points: 733 Visits: 1510
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.
ALZDBA
ALZDBA
SSCertifiable
SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)

Group: General Forum Members
Points: 6974 Visits: 8839
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


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

- 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" :-D


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me Alien but most of the time this is me Hehe
Fraggle-805517
Fraggle-805517
Right there with Babe
Right there with Babe (733 reputation)Right there with Babe (733 reputation)Right there with Babe (733 reputation)Right there with Babe (733 reputation)Right there with Babe (733 reputation)Right there with Babe (733 reputation)Right there with Babe (733 reputation)Right there with Babe (733 reputation)

Group: General Forum Members
Points: 733 Visits: 1510
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.
ALZDBA
ALZDBA
SSCertifiable
SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)

Group: General Forum Members
Points: 6974 Visits: 8839
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 w00t

- 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" :-D


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me Alien but most of the time this is me Hehe
ALZDBA
ALZDBA
SSCertifiable
SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)

Group: General Forum Members
Points: 6974 Visits: 8839
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 w00t

- 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" :-D


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me Alien but most of the time this is me Hehe
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search