Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 482
  • Last Modified:

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
--*/


0
tagtekin
Asked:
tagtekin
  • 5
  • 4
  • 2
1 Solution
 
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
 
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
Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

 
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
 
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
 
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now