473,548 Members | 2,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

User Defined Functions, passing parameters from another udf's results (end result=Crosstab )

Hi All:
I've read a whole slew of posts about creating temp tables using stored
proceedures to get the crosstab ability, but I'm wondering if, for this
specific case, there might be a more efficient way.

What makes this question different from the others that I've read is
that I'm using user defined functions, not tables. I actually think
that I've got the crosstab thing down, it's just passing the parameter
to the 2nd udf that's messing me up.

I've got a people table and an address table. Each person can have
multiple addresses. I need to create a dataset that has in each row
the name of the person, the first address, any second address, and any
third address. I only need to show the first 3, so if there's 100, I
can just ignore the rest.

I created a user defined function to return the 1st, 2nd, or 3rd
address for a given person.
udf_ReturnAddre ss(PersonID,Mat chNumber)

Another user defined function returns the people that I'm looking for
(potential duplicates for a person in this case).
udf_ReturnPossi bleDupsForAPers on(PersonID)
SELECT
Main.FoundPerso nID, Main.LastName, A1.Street, A2.Street,
A3.Street
FROM
udf(ReturnPossi bleDupsForAPers on(@PersonID) MainTable
CROSS JOIN
(SELECT Street1 FROM
udf_ReturnAddre ss(Main.FoundPe rsonID,1) Adr1) A1
CROSS JOIN
(SELECT Street1 FROM
udf_ReturnAddre ss(Main.FoundPe rsonID,2) Adr2) A2
CROSS JOIN
(SELECT Street1 FROM
udf_ReturnAddre ss(Main.FoundPe rsonID,3) Add3) A3
If, for the first parameter for the return address function, I replace
Main.FoundPerso nID with the ID of a person, it works just fine. I
obviously don't want a static id as a parameter - I want to use the ID
of the person that the first udf found. Leaving the variable
MainTable.Perso nID there causes an error in the query designer though.

I get "Error in list of function arguments: '.' not recognized.

So maybe my problem is that I just don't know how to pass the id of the
person that's found by the first UDF as the parameter of the function
to find the found person's 3 addresses.

Any guidance would be greatly appreciated!
Thanks
Ken

Oct 25 '05 #1
6 2388
I left out that I'm using SQL Server 2000.

Oct 25 '05 #2
Simple stupid mistake! Never mind.

Had a table UDF, not Scalar UDF for the function that returned the
address number and some messed up thinking.

Here's what I've got now, and it works just fine.

SELECT
Possibles.*,
dbo.udf_ReturnA ddress(FoundPer sonID, 1) AS FullAddr1,
dbo.udf_ReturnA ddress(FoundPer sonID, 2) AS FullAddr2
FROM
dbo.udf_ReturnP ossibleDupsForA Person(@PersonI D) Possibles

WAYYYY simpler now.

So, for those of you who may have found this by searching for crosstab,
there's a way to create a crosstab, with no temp table, but with a
FIXED number of columns, not dynamic. (not that this is a unique
solution, there's lots of other postings on it too...)

Oct 25 '05 #3
Ken Post (nn*******@gmai l.com) writes:
Had a table UDF, not Scalar UDF for the function that returned the
address number and some messed up thinking.

Here's what I've got now, and it works just fine.

SELECT
Possibles.*,
dbo.udf_ReturnA ddress(FoundPer sonID, 1) AS FullAddr1,
dbo.udf_ReturnA ddress(FoundPer sonID, 2) AS FullAddr2
FROM
dbo.udf_ReturnP ossibleDupsForA Person(@PersonI D) Possibles

WAYYYY simpler now.


OK, but what is this udf_ReturnAddre ss actually doing? Is just doing a
plain lookup with a SELECT statement, like:

BEGIN
RETURN (SELECT FullAddress FROM addresses
WHERE PersonId = @personid AND Adrno = @no)
END

In such case, rewrite into a join for a considerable performance
improvement.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp

Oct 25 '05 #4
Thanks for the reply Erland!

The function can't be optimal the way I've got it, but it works. It's
not exactly just a plain lookup, it's numbering address rows. Still,
I'm VERY eager to understand how I could do this with a join for better
performance. Can you explain?
Here's the funcion
ALTER FUNCTION dbo.udf_ReturnA ddress
(
@PersonID int,
@MatchNumber int
)
RETURNS varchar(600)
AS
BEGIN
DECLARE @FullAddr varchar(600)

SELECT @FullAddr = Addresses.Addre ss1 +
CASE WHEN len(Addresses.A ddress2) > 0 THEN ', ' + Addresses.Addre ss2
ELSE '' END +
CASE WHEN len(Addresses.A ddress3) > 0 THEN ', ' + Addresses.Addre ss3
ELSE '' END +
Addresses.City

FROM dbo.ppl_address es Addresses INNER JOIN
(SELECT COUNT(*) LineNumber, a.PersonID,
a.AddressID
FROM ppl_Addresses A JOIN
ppl_Addresses B ON A.AddressID >=
B.AddressID AND A.PersonID = B.PersonID
GROUP BY A.PersonID, A.AddressID) N
ON Addresses.Perso nID = N.PersonID AND Addresses.Addre ssID =
N.AddressID

WHERE
(Addresses.Pers onID = @PersonID) AND
(N.LineNumber = @MatchNumber)

ORDER BY Addresses.IsMai ling DESC, Addresses.IsBil ling DESC

RETURN @FullAddr
END

Oct 25 '05 #5
Ken Post (nn*******@gmai l.com) writes:
Thanks for the reply Erland!

The function can't be optimal the way I've got it, but it works. It's
not exactly just a plain lookup, it's numbering address rows. Still,
I'm VERY eager to understand how I could do this with a join for better
performance. Can you explain?


Below is an attempt to a rewrite, which may flat out wrong. (I did not
understand the ORDER BY, so I simply ignored those. :-)

Since yours is a bit complicated, it may warrant a function, as long as
performance is decent. But I recently leard that there are people who
do things like:

SELECT OrderId, dbo.GetCustomer Name(CustomerID ), ...
FROM ...

and the UDF is a plain SELECT. That is very unnecessary.
SELECT Poss.*,
MIN(CASE WHEN N.LineNumber WHEN 1 THEN UDF.FullAddr) AS FullAddr1,
MIN(CASE WHEN N.LineNumber WHEN 2 THEN UDF.FullAddr) AS FullAddr2
dbo.udf_ReturnA ddress(FoundPer sonID, 1) AS FullAddr1,
dbo.udf_ReturnA ddress(FoundPer sonID, 2) AS FullAddr2
FROM dbo.udf_ReturnP ossibleDupsForA Person(@PersonI D) Poss
JOIN (SELECT Adr.PersonID, N.LineNumber,
FullAddr = Adr.Address1 +
CASE WHEN len(Adr.Address 2) > 0
THEN ', ' + Adr.Address2
ELSE ''
END +
CASE WHEN len(Adr.Address 3) > 0
THEN ', ' + Adr.Address3
ELSE ''
END + Adr.City
FROM dbo.ppl_address es Adr
JOIN (SELECT COUNT(*) LineNumber, a.PersonID, a.AddressID
FROM ppl_Addresses A
JOIN ppl_Addresses B ON A.AddressID >= B.AddressID
AND A.PersonID = B.PersonID
GROUP BY A.PersonID, A.AddressID) N
ON Adr.PersonID = N.PersonID
AND Adr.AddressID = N.AddressID
WHERE N.LineNumber IN (1, 2)) AS UDF
ON UDF.PersonID = Poss.FoundPerso nID
GROUP BY Poss.PersonID, Poss.col1, Poss.col2, ...

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp

Oct 25 '05 #6
Ah, now that's interesting. Totally diffrent way of thinking about it.
While my udf wasn't a plain select, if your rewrite will work, I'd
much rather use that. I'll test it out tomorrow. Thanks!

The order by is irrelevant, but in case you're interested, there are 2
bit columns in ppl_Addresses. One is IsMailing and the other is
IsBilling. Each person has 1 billing address and 1 mailing address
(though they can't be the same ID). I was showing first the primary
address, then the billing (if different), then any other addresses on
file.

Oct 26 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2032
by: markus | last post by:
This string does not work in php: $sql="UPDATE mytable SET myfield=myuserfunction(myfield)" What I want is my function to evaluate each field and put the new value in the field. For example, this works: $sql= "UPDATE mytable SET myfield=myfield+3";
0
5786
by: Mike Chirico | last post by:
With mysql-4.1.1-alpha, using any User Defined Function ( UDF) at the query execution stage, on Linux 2.6, gave me the following error: "ERROR 2013 (HY000): Lost connection to MySQL server during query" However, upgrading to mysql-4.1.2-alpha seemed to solve the problem. Both versions were configured exactly as follows ...
1
1910
by: mehl | last post by:
Hello -- We have annual values for several 'MeasName': Capital expenditure increment Growth rate Subscribers The table has these fields: Year MeasName
2
23345
by: Vic Y | last post by:
Hi, I am trying to call a user defined function(UDF) from a stored proc, using a default parameter in the called UDF (e.g. @bid_price_type int = 0 ). However the calling stored proc complains that the UDF is expecting a 2nd parameter when I only provide the @test parameter value. Are default parameter values supported in UDF's? I wld...
5
1518
by: Mamuninfo | last post by:
Hello all, I want to write a user defined function in the bd2 database that will return somethings from database by the java like srote procedure. Is it possible, if yes Please give me hints. ...Mamun
5
2629
by: Zlatko Matić | last post by:
Hello. How can I call some functions on MSDE when working in .mdb ? Especially in-line functions which are similar to stored procedures. How can I use MSDE in-line functions as recordsource for .mdb forms ? Can I call in-line functions using ADO ? I tried, but it seems that only stored procedures are allowed (adCmdStoredProc).... Thanks.
3
4115
by: chreo | last post by:
I have user-defined function in MSSQL which returns Table (with 10 columns) (sorry for Polish names) CREATE FUNCTION PACZKI_Z_AKCJI (@AKCJA_ID int) RETURNS TABLE RETURN SELECT TOP 100 PERCENT dbo.PACZKI.ID_PACZKI, dbo.PACZKI.ID_AKCJI, dbo.PACZKI.NR_PACZKI, dbo.PACZKI.ILOSC_KUPONOW, LOG_1.Login AS LOGIN1,
9
3298
by: billmiami2 | last post by:
I was playing around with the new SQL 2005 CLR functionality and remembered this discussion that I had with Erland Sommarskog concerning performance of scalar UDFs some time ago (See "Calling sp_oa* in function" in this newsgroup). In that discussion, Erland made the following comment about UDFs in SQL 2005: >>The good news is that in SQL...
0
1346
by: TertiaryKey | last post by:
Firstly I would like to say hello to you all as I am a new member. I'm calling a User Defined Function 'FormatDate' from DB2 SQL on an AS/400. The UDF receives a CYYMMDD numeric date and returns a DD/MM/YYYY character date. The UDF doesn't allow null parameters so I'm using a Case statement as follows:- select case when DATEVAL is not null...
0
7438
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7951
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7803
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6036
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5082
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3495
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3475
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
751
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.