473,498 Members | 1,704 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How pass column to udf in join

Ray
Group,

Passing inline values to a udf is straightforward. However, how or is it
possible to pass a column from the select of one table into a udf that
returns a table variable in a join to the original table. The goal is to
explode the number of rows in the result set out based on the result of the
udf. Although the example I am providing here is simplified, we are trying
to parse out multiple values out of a text column and using a select into so
the result will be one row for each of row in the original table compounded
by the number of occurrences in the text field.
(I know bad table design but that's out of my control)

Thanks,
Ray

create table TableOne
(
Col1 int,
Col2 char(5)
)
go

insert TableOne values (1, 'One')
insert TableOne values (2, 'Two')
insert TableOne values (3, 'Three')
go

Create Function dbo.udfTableTwo
(@Id int)
RETURNS @tbl TABLE (TID int, TChar char(1))
AS
BEGIN
Declare @test int
set @test = @Id
While @test >= 0
Begin
INSERT @tbl VALUES (@Id, Char(65+@test))
set @test = @test - 1
End
RETURN
END

--works
select a.*, b.* from TableOne a join dbo.udfTableTwo(2) b
on a.col1=b.TID

--Fails with Line 1: Incorrect syntax near '.'.
select a.*, b.* from TableOne a join dbo.udfTableTwo(a.col1) b
on a.col1=b.TID

drop table TableOne
go
drop function dbo.udfTableTwo
go

Jul 23 '05 #1
6 5278
SQL Server Books Online states:
Parameters can take the place only of constants; they cannot be used in
place of table names, column names, or the names of other database objects.

This seems strikingly applicable for inline table valued UDFs.

--
Anith
Jul 23 '05 #2
Ray
Anith,

It seems to me that this statement from BOL is in regards to parameters as
they are used inside the function. In the example, I am not doing such.
Perhaps I'm clinging to hope but it isn't sounding like the answer has been
found. When dealing with hundreds of millions of rows in an ETL process, I
really like to avoid reading a table twice or using cursors.

All insights are appreciated.
Ray

"Anith Sen" <an***@bizdatasolutions.com> wrote in message
news:Lf**************@newsread1.news.pas.earthlink .net...
SQL Server Books Online states:
Parameters can take the place only of constants; they cannot be used in
place of table names, column names, or the names of other database
objects.

This seems strikingly applicable for inline table valued UDFs.

--
Anith

Jul 23 '05 #3
On Tue, 15 Feb 2005 00:39:12 GMT, Ray wrote:
Passing inline values to a udf is straightforward. However, how or is it
possible to pass a column from the select of one table into a udf that
returns a table variable in a join to the original table.


Hi Ray,

This is not possible. Your code is in fact not trying to join TableOne to
one other table, but to a table of tables (since dbo.udfTableTwo would
have a different content for each row in TableOne).

To me, it makes sense that this won't work. It seems logical that the
table has to be materialized before it can be joined to another table, but
you can't materialize the udfTableTwo table without knowing the values of
TableOne.col1.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #4
You can't reference a column from another table as a parameter to a
table-valued UDF. This makes perfect sense when you consider that, in
theory, the server has to be able to materialize the derived tables
after the FROM clause before performing the join. The same principle
also prevents you referencing columns from other tables in a derived
table query. The syntax is extended in SQL Server 2005 with the CROSS
APPLY operator to allow this type of correlated query but in 2000 it
isn't supported.

A multi-statement TVF seems unlikely to be the most efficient solution
for you. How about just creating TableTwo and using it in a join:

CREATE TableTwo (tid INTEGER PRIMARY KEY, tchar CHAR(1) NOT NULL
UNIQUE)

SELECT A.*, B.*
FROM TableOne AS A,
TableTwo AS B
WHERE A.col1 <= B.tid

--
David Portas
SQL Server MVP
--

Jul 23 '05 #5
CORRECTION. That should be:

SELECT A.*, B.*
FROM TableOne AS A,
TableTwo AS B
WHERE A.col1 >= B.tid

--
David Portas
SQL Server MVP
--

Jul 23 '05 #6
Ray (so*****@nowhere.com) writes:
--Fails with Line 1: Incorrect syntax near '.'.
select a.*, b.* from TableOne a join dbo.udfTableTwo(a.col1) b
on a.col1=b.TID


As Aniht, Hugo and David have pointed out, there is no way you can do
this in SQL 2000.

However, in SQL 2005, currently in beta, you can do this:

select a.*, b.* from TableOne a cross apply dbo.udfTableTwo(a.Col1) b

I have not really dug into the details of the APPLY operator, so
I cannot say whether this would give you expected output in your case,
but this is what I get from your sample:

Col1 Col2 TID TChar
----------- ----- ----------- -----
1 One 1 B
1 One 1 A
2 Two 2 C
2 Two 2 B
2 Two 2 A
3 Three 3 D
3 Three 3 C
3 Three 3 B
3 Three 3 A

(9 row(s) affected)
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #7

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

Similar topics

2
1793
by: jlficken | last post by:
I am trying to create a stored procedure that accepts 3 parameters. Using these parameters I am joining on 2 tables and a UDF that accepts these 3 parameters to get movement information. When I try...
20
2733
by: Steve Jorgensen | last post by:
Hi all, I've just finished almost all of what has turned out to be a real bear of a project. It has to import data from a monthly spreadsheet export from another program, and convert that into...
1
6119
by: Will | last post by:
I have a combo box on a form which is based on table tblMachine. On that combo box I have four columns visible MachineNumber, description, location and type. The bound column is the MachineNumber...
1
4557
by: Eric E | last post by:
Hi, I'm trying to write a recordset-returning function that returns a values from a base table, and one column from a joined table, where the joined table varies according to a field of the base...
6
35556
by: Mike S | last post by:
Hi all, A (possibly dumb) question, but I've had no luck finding a definitive answer to it. Suppose I have two tables, Employees and Employers, which both have a column named "Id": Employees...
2
1620
by: André | last post by:
Hi, I have to pass a lot of variables from vb.net to javascript using client callback. the problem is that there are single variables, but also arrays with differents indexes. I have two...
2
3145
by: sunilkes | last post by:
Hi, I have a crosstab query made by a linked table, the row headers are displayed correctly, but the column headers are showing the SNo (Autonumber) value instead of the NAME field. Please...
3
2251
by: emgallagher | last post by:
I have a form which lists studies. People can filter the form based on details about the study, such as the study type. Currently users filter via the right click method. I would like to be...
4
10646
tjc0ol
by: tjc0ol | last post by:
Hi guys, I'm a newbie in php and I got error in my index.php which is: 1054 - Unknown column 'p.products_id' in 'on clause' select p.products_image, pd.products_name, p.products_id,...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
6887
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...
0
7379
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...
0
5462
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,...
1
4910
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4590
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...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1419
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
291
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...

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.