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. Set RowCount issue in query containing function tables

Set RowCount issue in query containing function tables

Scheduled Pinned Locked Moved Database
helpdatabasequestion
2 Posts 1 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.
  • D Offline
    D Offline
    Dr_X
    wrote on last edited by
    #1

    I have the query below which contains 2 functions which return tables, fn_SamplesUserProducts & fn_SamplesUserOffices. The query itself I want to set the rowcount to say 10. However, my functions will return more than 10 rows and obviously they don't due to the set RowCount. I tried to set the RowCount in the function but an error occurs when attempting to compile: Invalid use of 'UNKNOWN TOKEN' within a function. Is there a work-around? The query is the first part of a complex cross-tab w/o reporting services installed.

    SELECT s.SampleID,
    convert(CHAR(10), s.PullDate, 111) + ' ' + IsNull(S.TripNumber, rtrim(convert(varchar(25), 'N/A' + rtrim(convert(varchar(8), S.SampleID))))) as ColumnName,
    s.PullDate,
    s.TripNumber,
    s.Quantity,
    convert(CHAR(10), s.PullDate, 112) + IsNull(S.TripNumber, rtrim(convert(varchar(25), 'N/A'))) + rtrim(convert(varchar(8), S.SampleID))
    FROM tblSamplesSample s,
    tblSamplesSampleTerminalPortWellLocation stp,
    fn_SamplesUserProducts(@UID) fnp,
    fn_SamplesUserOffices(@UID) fno
    WHERE S.SampleID *= stp.SampleID
    AND (S.Deleted = 0 or S.Deleted IS NULL)
    AND (stp.Deleted = 0 or stp.Deleted IS NULL)
    AND fnp.ProductID = s.ProductID
    AND fno.OfficeID = s.OfficeID
    AND ((@DateRangeBegin IS Null AND @DateRangeEnd IS Null)
    OR PullDate BETWEEN @DateRangeBegin AND @DateRangeEnd
    )
    AND (@TerminalPortWellLocationID IS Null
    OR stp.TerminalPortWellLocationID = @TerminalPortWellLocationID
    )
    AND (@CustomerCompanyID IS NULL
    OR s.CustomerCompanyID = @CustomerCompanyID
    )
    AND (@ProductID IS NULL
    OR S.ProductID = @ProductID
    )
    AND (@CustomerProductReportID IS NULL
    OR s.CustomerProductReportID = @CustomerProductReportID
    )
    AND (@DomesticInternational IS NULL
    OR s.DomesticInternational = @DomesticInternational
    )
    AND S.StatusID = 1 --Finalized
    GROUP BY S.SampleID, S.TripNumber, S.PullDate, s.Quantity
    HAVING Count(stp.TerminalPortWellLocationID) = 1
    ORDER BY s.PullDate DESC, TripNumber ASC, s.SampleID

    Thanks, Michael I firmly believe that any man's finest hour, the greatest fulfillment of all that he holds dear, is that moment when he has worked his heart out in a good cause and lies exhausted on the field of battle - victorious. Vince Lombardi (1913-1970)

    D 1 Reply Last reply
    0
    • D Dr_X

      I have the query below which contains 2 functions which return tables, fn_SamplesUserProducts & fn_SamplesUserOffices. The query itself I want to set the rowcount to say 10. However, my functions will return more than 10 rows and obviously they don't due to the set RowCount. I tried to set the RowCount in the function but an error occurs when attempting to compile: Invalid use of 'UNKNOWN TOKEN' within a function. Is there a work-around? The query is the first part of a complex cross-tab w/o reporting services installed.

      SELECT s.SampleID,
      convert(CHAR(10), s.PullDate, 111) + ' ' + IsNull(S.TripNumber, rtrim(convert(varchar(25), 'N/A' + rtrim(convert(varchar(8), S.SampleID))))) as ColumnName,
      s.PullDate,
      s.TripNumber,
      s.Quantity,
      convert(CHAR(10), s.PullDate, 112) + IsNull(S.TripNumber, rtrim(convert(varchar(25), 'N/A'))) + rtrim(convert(varchar(8), S.SampleID))
      FROM tblSamplesSample s,
      tblSamplesSampleTerminalPortWellLocation stp,
      fn_SamplesUserProducts(@UID) fnp,
      fn_SamplesUserOffices(@UID) fno
      WHERE S.SampleID *= stp.SampleID
      AND (S.Deleted = 0 or S.Deleted IS NULL)
      AND (stp.Deleted = 0 or stp.Deleted IS NULL)
      AND fnp.ProductID = s.ProductID
      AND fno.OfficeID = s.OfficeID
      AND ((@DateRangeBegin IS Null AND @DateRangeEnd IS Null)
      OR PullDate BETWEEN @DateRangeBegin AND @DateRangeEnd
      )
      AND (@TerminalPortWellLocationID IS Null
      OR stp.TerminalPortWellLocationID = @TerminalPortWellLocationID
      )
      AND (@CustomerCompanyID IS NULL
      OR s.CustomerCompanyID = @CustomerCompanyID
      )
      AND (@ProductID IS NULL
      OR S.ProductID = @ProductID
      )
      AND (@CustomerProductReportID IS NULL
      OR s.CustomerProductReportID = @CustomerProductReportID
      )
      AND (@DomesticInternational IS NULL
      OR s.DomesticInternational = @DomesticInternational
      )
      AND S.StatusID = 1 --Finalized
      GROUP BY S.SampleID, S.TripNumber, S.PullDate, s.Quantity
      HAVING Count(stp.TerminalPortWellLocationID) = 1
      ORDER BY s.PullDate DESC, TripNumber ASC, s.SampleID

      Thanks, Michael I firmly believe that any man's finest hour, the greatest fulfillment of all that he holds dear, is that moment when he has worked his heart out in a good cause and lies exhausted on the field of battle - victorious. Vince Lombardi (1913-1970)

      D Offline
      D Offline
      Dr_X
      wrote on last edited by
      #2

      I declared the following table variables and used them in FROM clause as a work-around. The table variables are populated prior to the SET RowCount command. However, is there a way around doing it this way?

      DECLARE @SamplesUserProducts TABLE(ProductID int)
      DECLARE	@SamplesUserOffices TABLE(OfficeID int)					
      
      INSERT INTO @SamplesUserProducts SELECT ProductID FROM fn\_SamplesUserProducts(592)
      INSERT INTO @SamplesUserOffices SELECT OfficeID FROM fn\_SamplesUserOffices(592)
      

      Thanks, Michael I firmly believe that any man's finest hour, the greatest fulfillment of all that he holds dear, is that moment when he has worked his heart out in a good cause and lies exhausted on the field of battle - victorious. Vince Lombardi (1913-1970)

      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