Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2005
»
SQL Server Newbies
»
function in a sql statement
function in a sql statement
Rate Topic
Display Mode
Topic Options
Author
Message
mathewspsimon
mathewspsimon
Posted Saturday, September 06, 2008 5:44 AM
SSC Journeyman
Group: General Forum Members
Last Login: Wednesday, May 09, 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
John H Marsh
John H Marsh
Posted Saturday, September 06, 2008 5:58 AM
Ten Centuries
Group: General Forum Members
Last Login: Thursday, February 28, 2013 1:54 AM
Points: 1,325,
Visits: 1,376
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
Fraggle-805517
Fraggle-805517
Posted Saturday, September 06, 2008 12:53 PM
Right there with Babe
Group: General Forum Members
Last Login: Thursday, May 09, 2013 8:15 AM
Points: 716,
Visits: 1,317
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
ALZDBA
ALZDBA
Posted Sunday, September 07, 2008 3:47 AM
SSCertifiable
Group: General Forum Members
Last Login: Today @ 2:17 AM
Points: 6,862,
Visits: 8,049
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
Jul 13
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
Fraggle-805517
Fraggle-805517
Posted Sunday, September 07, 2008 7:12 AM
Right there with Babe
Group: General Forum Members
Last Login: Thursday, May 09, 2013 8:15 AM
Points: 716,
Visits: 1,317
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
ALZDBA
ALZDBA
Posted Sunday, September 07, 2008 9:51 AM
SSCertifiable
Group: General Forum Members
Last Login: Today @ 2:17 AM
Points: 6,862,
Visits: 8,049
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
Jul 13
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
Fraggle-805517
Fraggle-805517
Posted Sunday, September 07, 2008 6:47 PM
Right there with Babe
Group: General Forum Members
Last Login: Thursday, May 09, 2013 8:15 AM
Points: 716,
Visits: 1,317
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
ALZDBA
ALZDBA
Posted Sunday, September 07, 2008 11:54 PM
SSCertifiable
Group: General Forum Members
Last Login: Today @ 2:17 AM
Points: 6,862,
Visits: 8,049
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
Jul 13
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
ALZDBA
ALZDBA
Posted Sunday, September 14, 2008 8:04 AM
SSCertifiable
Group: General Forum Members
Last Login: Today @ 2:17 AM
Points: 6,862,
Visits: 8,049
This is the classic behaviour I wanted to point to in my warnings
http://www.sqlservercentral.com/Forums/Topic567766-8-1.aspx
Johan
Jul 13
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 »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.