combine the results from the function and the select statement

I have the function that returns 4 columns i am asked to return 5 vendor id from the select statement that tests the function.  I have the following function and the select statement however i dont get 5 colmuns I only get one that comes from the vendors table.
/*
CREATE function fnDateRange
(@DateMin varchar(50), @datemax varchar(50))
returns table

return
(SELECT InvoiceTotal, InvoiceNumber, InvoiceDate,InvoiceTotal - CreditTotal - PaymentTotal AS Balance

 FROM Invoices
 WHERE InvoiceDate BETWEEN @DateMin AND @DateMax)

*/
--drop function fnDateRange
--/*
select VENDORNAME, (SELECT * FROM dbo.fndaterange('april 10 2002', 'april 18 2002')AS Q
where o.VENDORID = Q.VENDORID)as maxe
FROM VENDORS AS o
--*/


tagtekinAsked:
Who is Participating?
 
Aneesh RetnakaranDatabase AdministratorCommented:
> but this function created returns the vendor id directly from the function i needed to return that column from the select

both are the same, since you are joing the the two, again if you wish you can use the following

SELECT Vendors.VendorName, Vendors.VendorID, t.InvoiceTotal, t.InvoiceNumber, t.InvoiceDate,t.balance
FROM Vendors
INNER JOIN  dbo.fnDateRange (...) t
on t.VendorID = Vendors.vendorID
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
CREATE function fnDateRange
(@DateMin varchar(50), @datemax varchar(50))
returns table ( InvoiceTotal as numeric(12,4), InvoiceNumber as int, Invoicedate as date, Balance as numeric(12,4))

return
(SELECT InvoiceTotal, InvoiceNumber, InvoiceDate,InvoiceTotal - CreditTotal - PaymentTotal AS Balance
 FROM Invoices
 WHERE InvoiceDate BETWEEN @DateMin AND @DateMax)



select o.VENDORNAME, q.*
from vendors o
join dbo.fndaterange('april 10 2002', 'april 18 2002') AS Q
on o.VENDORID = Q.VENDORID
0
 
Aneesh RetnakaranDatabase AdministratorCommented:
Hi tagtekin,

I think You should use  INNER JOIN between tour table and function.
But the problem here is i didn't dind any proper joining keye in your function. It's better to return one unique key, something like VendorId from your funcion and do a join

SELECT VendorName, t.*
FROM Vendors
INNER JOIN  dbo.fnDateRange (...) t
on t.VendorID = Vendors.vendorID


Aneesh
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
good point:

CREATE function fnDateRange
(@DateMin varchar(50), @datemax varchar(50))
returns table ( VendorIDas int , InvoiceTotal as numeric(12,4), InvoiceNumber as int, Invoicedate as date, Balance as numeric(12,4))

return
(SELECT Vendorid, InvoiceTotal, InvoiceNumber, InvoiceDate,InvoiceTotal - CreditTotal - PaymentTotal AS Balance
 FROM Invoices
 WHERE InvoiceDate BETWEEN @DateMin AND @DateMax)
0
 
Aneesh RetnakaranDatabase AdministratorCommented:
CREATE function fnDateRange
(@DateMin varchar(50), @datemax varchar(50))
returns table ( VendorID as int , InvoiceTotal as numeric(12,4), InvoiceNumber as int, Invoicedate as date, Balance as numeric(12,4)) -- missed s space here

return
(SELECT Vendorid, InvoiceTotal, InvoiceNumber, InvoiceDate,InvoiceTotal - CreditTotal - PaymentTotal AS Balance
 FROM Invoices
 WHERE InvoiceDate BETWEEN @DateMin AND @DateMax)
0
 
tagtekinAuthor Commented:
yes but this function created returns the vendor id directly from the function i needed to return that column from the select statement.

Thanks
0
 
tagtekinAuthor Commented:
I did this and add it made the change with your comment and it worked. THank you. I m posting another one with my other problem. Hope to see your comments.

/*
CREATE function fnDateRange
(@DateMin varchar(50), @datemax varchar(50))
returns table

return
(SELECT Vendorid, InvoiceTotal, InvoiceNumber, InvoiceDate,InvoiceTotal - CreditTotal - PaymentTotal AS Balance

 FROM Invoices
 WHERE InvoiceDate BETWEEN @DateMin AND @DateMax)
*/
--*/
--drop function fnDateRange
--select * from dbo.fndaterange('april 10 2002', 'april 18 2002')

SELECT Vendors.VendorName, Vendors.VendorID, t.InvoiceTotal, t.InvoiceNumber, t.InvoiceDate,t.balance
FROM Vendors
INNER JOIN  dbo.fnDateRange ('april 10 2002', 'april 18 2002')as t on t.VendorID = Vendors.vendorID
0
 
Aneesh RetnakaranDatabase AdministratorCommented:
are you getting any error ?
0
 
tagtekinAuthor Commented:
no I was getting since there was no vendorid coming from the function but i added andsolved the problem.
0
 
Aneesh RetnakaranDatabase AdministratorCommented:
Ok.. now it seems that you are solved the problem
0
 
tagtekinAuthor Commented:
yes thank you very much
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.