Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Subquery returned more than 1 value

Subquery returned more than 1 value

Scheduled Pinned Locked Moved Database
helpdatabase
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sam Heller
    wrote on last edited by
    #1

    I am having issues with the following code ever since I added a subquery to it. The error tells me that more than one value was returned from the subquery. The query should in fact retrieve more than one value in most instances so this is fine. Thing is it stops the query from working and I don't know why. My code is as follows in it's entirety. Thanks for any help SELECT IH.InvoiceDate AS HistoryDate, 'Part of Invoice' AS HistoryType, SI.SiteName AS HistoryBranch, ('Invoice Number - ' + IH.InvoiceNumber + ' - ' + C.CustomerForeName + ' ' + C.CustomerSurname) AS HistoryReference, ( SELECT TIP.InvoicePhoneIMEI FROM tblInvoicePhones TIP INNER JOIN tblInvoiceItems TII ON TIP.InvoiceItemID = TII.InvoiceItemID WHERE TII.InvoiceID = IH.InvoiceID ) As HistoryIMEIs, (ST.StaffForeName + ' ' + ST.StaffSurname) AS HistoryUser FROM tblInvoiceHeader IH INNER JOIN tblInvoiceItems II ON II.InvoiceID = IH.InvoiceID INNER JOIN tblSites SI ON IH.SiteID = SI.SiteID INNER JOIN tblStaff ST ON IH.StaffID = ST.StaffID LEFT JOIN tblCustomers C ON IH.CustomerID = C.CustomerID WHERE II.StockID = '1750' AND IH.InvoiceDate >= 'January 1, 2007' AND IH.InvoiceDate <= 'July 2, 2007' AND IH.SiteID = '10' AND IH.InvoiceProForma = 0

    J H D 3 Replies Last reply
    0
    • S Sam Heller

      I am having issues with the following code ever since I added a subquery to it. The error tells me that more than one value was returned from the subquery. The query should in fact retrieve more than one value in most instances so this is fine. Thing is it stops the query from working and I don't know why. My code is as follows in it's entirety. Thanks for any help SELECT IH.InvoiceDate AS HistoryDate, 'Part of Invoice' AS HistoryType, SI.SiteName AS HistoryBranch, ('Invoice Number - ' + IH.InvoiceNumber + ' - ' + C.CustomerForeName + ' ' + C.CustomerSurname) AS HistoryReference, ( SELECT TIP.InvoicePhoneIMEI FROM tblInvoicePhones TIP INNER JOIN tblInvoiceItems TII ON TIP.InvoiceItemID = TII.InvoiceItemID WHERE TII.InvoiceID = IH.InvoiceID ) As HistoryIMEIs, (ST.StaffForeName + ' ' + ST.StaffSurname) AS HistoryUser FROM tblInvoiceHeader IH INNER JOIN tblInvoiceItems II ON II.InvoiceID = IH.InvoiceID INNER JOIN tblSites SI ON IH.SiteID = SI.SiteID INNER JOIN tblStaff ST ON IH.StaffID = ST.StaffID LEFT JOIN tblCustomers C ON IH.CustomerID = C.CustomerID WHERE II.StockID = '1750' AND IH.InvoiceDate >= 'January 1, 2007' AND IH.InvoiceDate <= 'July 2, 2007' AND IH.SiteID = '10' AND IH.InvoiceProForma = 0

      J Offline
      J Offline
      jonathan15
      wrote on last edited by
      #2

      The message relates to the subquery. In this example the subquery can only return one row. I would run the subquery only and see how many rows it is returning. If it returns more than one then you have a problem Jon

      S 1 Reply Last reply
      0
      • J jonathan15

        The message relates to the subquery. In this example the subquery can only return one row. I would run the subquery only and see how many rows it is returning. If it returns more than one then you have a problem Jon

        S Offline
        S Offline
        Sam Heller
        wrote on last edited by
        #3

        As i mentioned in my original post the subquery should and does return more than one rows. I want for example the following to be returned. 12/10/2007 Sale 967676689768879, 23987298739872, 39278387298372, 3927328739872973 24/12/2007 Sale 8768676876876876, 87687678876876786, 9876987865765678 Where the large numbers are collected from the subquery. Is this not possible? How else can I go about getting information in this way?

        J 1 Reply Last reply
        0
        • S Sam Heller

          As i mentioned in my original post the subquery should and does return more than one rows. I want for example the following to be returned. 12/10/2007 Sale 967676689768879, 23987298739872, 39278387298372, 3927328739872973 24/12/2007 Sale 8768676876876876, 87687678876876786, 9876987865765678 Where the large numbers are collected from the subquery. Is this not possible? How else can I go about getting information in this way?

          J Offline
          J Offline
          jonathan15
          wrote on last edited by
          #4

          No, You cant do it that way. The way your SQL is structured the subquery is in effect a single field. you cant just put several values into one field. I see what you are trying to do but i dont think there is a way to do it with a single SQL statement (although i may be wrong about that).

          S 1 Reply Last reply
          0
          • J jonathan15

            No, You cant do it that way. The way your SQL is structured the subquery is in effect a single field. you cant just put several values into one field. I see what you are trying to do but i dont think there is a way to do it with a single SQL statement (although i may be wrong about that).

            S Offline
            S Offline
            Sam Heller
            wrote on last edited by
            #5

            Ok so you can't merge the fields in anyway. Does anyone else know?

            1 Reply Last reply
            0
            • S Sam Heller

              I am having issues with the following code ever since I added a subquery to it. The error tells me that more than one value was returned from the subquery. The query should in fact retrieve more than one value in most instances so this is fine. Thing is it stops the query from working and I don't know why. My code is as follows in it's entirety. Thanks for any help SELECT IH.InvoiceDate AS HistoryDate, 'Part of Invoice' AS HistoryType, SI.SiteName AS HistoryBranch, ('Invoice Number - ' + IH.InvoiceNumber + ' - ' + C.CustomerForeName + ' ' + C.CustomerSurname) AS HistoryReference, ( SELECT TIP.InvoicePhoneIMEI FROM tblInvoicePhones TIP INNER JOIN tblInvoiceItems TII ON TIP.InvoiceItemID = TII.InvoiceItemID WHERE TII.InvoiceID = IH.InvoiceID ) As HistoryIMEIs, (ST.StaffForeName + ' ' + ST.StaffSurname) AS HistoryUser FROM tblInvoiceHeader IH INNER JOIN tblInvoiceItems II ON II.InvoiceID = IH.InvoiceID INNER JOIN tblSites SI ON IH.SiteID = SI.SiteID INNER JOIN tblStaff ST ON IH.StaffID = ST.StaffID LEFT JOIN tblCustomers C ON IH.CustomerID = C.CustomerID WHERE II.StockID = '1750' AND IH.InvoiceDate >= 'January 1, 2007' AND IH.InvoiceDate <= 'July 2, 2007' AND IH.SiteID = '10' AND IH.InvoiceProForma = 0

              H Offline
              H Offline
              Harini N K
              wrote on last edited by
              #6

              Hi You can write your sub-query in a separate function. (table-valued function). Using this function, you can join with the main query as given below. Select Col1, Col2, dbo.Fn_SubQuery(param1,param2) from Table1 Hope this helps. See the example below use northwind go create function dbo.Fn_OrdersProductID(@OrderID int) returns nvarchar(4000) as begin DECLARE @ProductID nvarchar(4000) set @ProductID = null SELECT @ProductID = COALESCE(@ProductID + ',','') + cast(ProductID as nvarchar) FROM [Order Details] WHERE OrderID = @OrderID RETURN @ProductID end -- run the foll. stmt after executing the above function -- SELECT OrderID,dbo.Fn_OrdersProductID(OrderID) from Orders Apply the same for your query -- modified at 7:08 Tuesday 3rd July, 2007 -- modified at 7:11 Tuesday 3rd July, 2007

              Harini

              P 1 Reply Last reply
              0
              • S Sam Heller

                I am having issues with the following code ever since I added a subquery to it. The error tells me that more than one value was returned from the subquery. The query should in fact retrieve more than one value in most instances so this is fine. Thing is it stops the query from working and I don't know why. My code is as follows in it's entirety. Thanks for any help SELECT IH.InvoiceDate AS HistoryDate, 'Part of Invoice' AS HistoryType, SI.SiteName AS HistoryBranch, ('Invoice Number - ' + IH.InvoiceNumber + ' - ' + C.CustomerForeName + ' ' + C.CustomerSurname) AS HistoryReference, ( SELECT TIP.InvoicePhoneIMEI FROM tblInvoicePhones TIP INNER JOIN tblInvoiceItems TII ON TIP.InvoiceItemID = TII.InvoiceItemID WHERE TII.InvoiceID = IH.InvoiceID ) As HistoryIMEIs, (ST.StaffForeName + ' ' + ST.StaffSurname) AS HistoryUser FROM tblInvoiceHeader IH INNER JOIN tblInvoiceItems II ON II.InvoiceID = IH.InvoiceID INNER JOIN tblSites SI ON IH.SiteID = SI.SiteID INNER JOIN tblStaff ST ON IH.StaffID = ST.StaffID LEFT JOIN tblCustomers C ON IH.CustomerID = C.CustomerID WHERE II.StockID = '1750' AND IH.InvoiceDate >= 'January 1, 2007' AND IH.InvoiceDate <= 'July 2, 2007' AND IH.SiteID = '10' AND IH.InvoiceProForma = 0

                D Offline
                D Offline
                DQNOK
                wrote on last edited by
                #7

                Harini is right I believe. Something like:

                SELECT DISTINCT
                   IH.InvoiceDate    AS HistoryDate
                 , 'Part of Invoice' AS HistoryType
                 , SI.SiteName       AS HistoryBranch
                 , ('Invoice Number - ' + IH.InvoiceNumber + ' - ' + C.CustomerForeName + ' ' + C.CustomerSurname) AS HistoryReference
                 , getInvoicePhoneIMEIs(IH.InvoiceID) AS HistoryIMEIs
                FROM         tblInvoiceHeader IH
                  INNER JOIN tblInvoiceItems  II ON II.InvoiceID = IH.InvoiceID
                  INNER JOIN tblSites         SI ON IH.SiteID    = SI.SiteID
                  INNER JOIN tblStaff         ST ON IH.StaffID   = ST.StaffID
                  LEFT  JOIN tblCustomers      C ON IH.CustomerID= C.CustomerID
                WHERE II.StockID = '1750'
                  AND IH.InvoiceDate >= 'January 1, 2007'
                  AND IH.InvoiceDate <= 'July 2, 2007'
                  AND IH.SiteID = '10'
                  AND IH.InvoiceProForma = 0
                

                where getInvoicePhoneIMEIs(IH.InvoiceID) uses a cursor based on your subselect to build a string. http://www.sqlteam.com/article/using-coalesce-to-build-comma-delimited-string[^] Be sure to include the DISTINCT keyword or you'll get a result row for every InvoicePhoneIMEI value. David

                1 Reply Last reply
                0
                • H Harini N K

                  Hi You can write your sub-query in a separate function. (table-valued function). Using this function, you can join with the main query as given below. Select Col1, Col2, dbo.Fn_SubQuery(param1,param2) from Table1 Hope this helps. See the example below use northwind go create function dbo.Fn_OrdersProductID(@OrderID int) returns nvarchar(4000) as begin DECLARE @ProductID nvarchar(4000) set @ProductID = null SELECT @ProductID = COALESCE(@ProductID + ',','') + cast(ProductID as nvarchar) FROM [Order Details] WHERE OrderID = @OrderID RETURN @ProductID end -- run the foll. stmt after executing the above function -- SELECT OrderID,dbo.Fn_OrdersProductID(OrderID) from Orders Apply the same for your query -- modified at 7:08 Tuesday 3rd July, 2007 -- modified at 7:11 Tuesday 3rd July, 2007

                  Harini

                  P Offline
                  P Offline
                  Praveen029
                  wrote on last edited by
                  #8

                  what is the need of COALESCE

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups