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. Using UDFs in a SELECT

Using UDFs in a SELECT

Scheduled Pinned Locked Moved Database
databasehelpjavasql-servercom
5 Posts 3 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.
  • C Offline
    C Offline
    Colin Angus Mackay
    wrote on last edited by
    #1

    Hello, I am trying to generate some statistics from my SQL Server 2000 database. I have two UDFs, one which determines which days have complete data for a range of dates, and the other generates the statistics for a particular day. I am fustrated because SQL Server doesn't seem to want to allow me to use these functions as I would like. I even dumped the contents of the dates function into a table variable so I do something like:

    DECLARE @dates TABLE([date] smalldatetime)
    INSERT INTO @dates
    SELECT [date] FROM dbo.dates_with_complete_data(@start_date, @end_date)

    The above works fine, but if I try an use that as input to the other function I get errors.

    SELECT calc.*
    FROM @dates as fd
    INNER JOIN (dbo.calculation(fd.[date])) as calc
    ON cc.[date] = fd.[date]

    Can anyone shed some light on how I might tackle this? If I remain stuck I might have to do the unthinkable and create my first ever cursor! :eek: --Colin Mackay--

    EuroCPian Spring 2004 Get Together[^] "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar

    M M 2 Replies Last reply
    0
    • C Colin Angus Mackay

      Hello, I am trying to generate some statistics from my SQL Server 2000 database. I have two UDFs, one which determines which days have complete data for a range of dates, and the other generates the statistics for a particular day. I am fustrated because SQL Server doesn't seem to want to allow me to use these functions as I would like. I even dumped the contents of the dates function into a table variable so I do something like:

      DECLARE @dates TABLE([date] smalldatetime)
      INSERT INTO @dates
      SELECT [date] FROM dbo.dates_with_complete_data(@start_date, @end_date)

      The above works fine, but if I try an use that as input to the other function I get errors.

      SELECT calc.*
      FROM @dates as fd
      INNER JOIN (dbo.calculation(fd.[date])) as calc
      ON cc.[date] = fd.[date]

      Can anyone shed some light on how I might tackle this? If I remain stuck I might have to do the unthinkable and create my first ever cursor! :eek: --Colin Mackay--

      EuroCPian Spring 2004 Get Together[^] "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar

      M Offline
      M Offline
      michanne
      wrote on last edited by
      #2

      I suspect the problem is in the inner join statement. Try this: SELECT calc.* FROM @dates as fd INNER JOIN ( SELECT * FROM (dbo.calculation(fd.[date]))) as calc ON cc.[date] = fd.[date] Barbara :cool:

      C 1 Reply Last reply
      0
      • M michanne

        I suspect the problem is in the inner join statement. Try this: SELECT calc.* FROM @dates as fd INNER JOIN ( SELECT * FROM (dbo.calculation(fd.[date]))) as calc ON cc.[date] = fd.[date] Barbara :cool:

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #3

        Thanks for your response, however that doesn't work either. I think it may because of the circular nature of what I am trying to do. The calculation does not return any rows unless it has a date, the join doesn't know what dates match because the calculation has not returned any rows, so no dates can be plugged into the function. The error message I get is "Incorrect syntax near 'fd'." giving the line number of where I call the UDF. --Colin Mackay--

        EuroCPian Spring 2004 Get Together[^] "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar

        M 1 Reply Last reply
        0
        • C Colin Angus Mackay

          Thanks for your response, however that doesn't work either. I think it may because of the circular nature of what I am trying to do. The calculation does not return any rows unless it has a date, the join doesn't know what dates match because the calculation has not returned any rows, so no dates can be plugged into the function. The error message I get is "Incorrect syntax near 'fd'." giving the line number of where I call the UDF. --Colin Mackay--

          EuroCPian Spring 2004 Get Together[^] "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar

          M Offline
          M Offline
          michanne
          wrote on last edited by
          #4

          oh - ok I see the problem. You can't use a variable in the FROM clause. You may need to put the function in place of @dates or use a derived set. Barbara

          1 Reply Last reply
          0
          • C Colin Angus Mackay

            Hello, I am trying to generate some statistics from my SQL Server 2000 database. I have two UDFs, one which determines which days have complete data for a range of dates, and the other generates the statistics for a particular day. I am fustrated because SQL Server doesn't seem to want to allow me to use these functions as I would like. I even dumped the contents of the dates function into a table variable so I do something like:

            DECLARE @dates TABLE([date] smalldatetime)
            INSERT INTO @dates
            SELECT [date] FROM dbo.dates_with_complete_data(@start_date, @end_date)

            The above works fine, but if I try an use that as input to the other function I get errors.

            SELECT calc.*
            FROM @dates as fd
            INNER JOIN (dbo.calculation(fd.[date])) as calc
            ON cc.[date] = fd.[date]

            Can anyone shed some light on how I might tackle this? If I remain stuck I might have to do the unthinkable and create my first ever cursor! :eek: --Colin Mackay--

            EuroCPian Spring 2004 Get Together[^] "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar

            M Offline
            M Offline
            Mike Dimmick
            wrote on last edited by
            #5

            The only advice I can give is to dump the dbo.calculation function, and compute the various parts in the SELECT clause instead, probably using separate functions. I believe SQL Server is able to look inside a function when compiling a query plan, so it may be able to perform whatever operations you have specified in parallel anyway.

            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