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. User Defined Function - Condition based tabel return ???

User Defined Function - Condition based tabel return ???

Scheduled Pinned Locked Moved Database
algorithmshelpquestion
5 Posts 4 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.
  • P Offline
    P Offline
    Paw Jershauge
    wrote on last edited by
    #1

    I have a colleague, that has a bit of trouble creating a UDF heres there code but it fails:

    CREATE FUNCTION GetData(@Param int)
    RETURNS TABLE
    AS
    RETURN
    IF @Param = 1
    SELECT * FROM Emp
    ELSE
    SELECT * FROM Offices
    END

    The function shall return different table based oppon the condition of the @Param value. Can someone help ???

    With great code, comes great complexity, so keep it simple stupid...:-\ :-\

    P R M 3 Replies Last reply
    0
    • P Paw Jershauge

      I have a colleague, that has a bit of trouble creating a UDF heres there code but it fails:

      CREATE FUNCTION GetData(@Param int)
      RETURNS TABLE
      AS
      RETURN
      IF @Param = 1
      SELECT * FROM Emp
      ELSE
      SELECT * FROM Offices
      END

      The function shall return different table based oppon the condition of the @Param value. Can someone help ???

      With great code, comes great complexity, so keep it simple stupid...:-\ :-\

      P Online
      P Online
      PIEBALDconsult
      wrote on last edited by
      #2

      I haven't written a UDF or a while, and I don't like that design, but can you use a UNION?

      SELECT * FROM Emp WHERE @Param=1
      UNION ALL
      SELECT * FROM Pffices WHERE @Param=0

      P 1 Reply Last reply
      0
      • P PIEBALDconsult

        I haven't written a UDF or a while, and I don't like that design, but can you use a UNION?

        SELECT * FROM Emp WHERE @Param=1
        UNION ALL
        SELECT * FROM Pffices WHERE @Param=0

        P Offline
        P Offline
        Paw Jershauge
        wrote on last edited by
        #3

        Thanks I will let him know this, lets see if it works ;) Thanks for the reply

        With great code, comes great complexity, so keep it simple stupid...:-\ :-\

        1 Reply Last reply
        0
        • P Paw Jershauge

          I have a colleague, that has a bit of trouble creating a UDF heres there code but it fails:

          CREATE FUNCTION GetData(@Param int)
          RETURNS TABLE
          AS
          RETURN
          IF @Param = 1
          SELECT * FROM Emp
          ELSE
          SELECT * FROM Offices
          END

          The function shall return different table based oppon the condition of the @Param value. Can someone help ???

          With great code, comes great complexity, so keep it simple stupid...:-\ :-\

          R Offline
          R Offline
          Ravi Sant
          wrote on last edited by
          #4

          No this isn't possible with either an inline or multi statement TVF (unless the 2 tables are union compatible?) so the above reply is only workable solution, if tables are union compatible.

          1 Reply Last reply
          0
          • P Paw Jershauge

            I have a colleague, that has a bit of trouble creating a UDF heres there code but it fails:

            CREATE FUNCTION GetData(@Param int)
            RETURNS TABLE
            AS
            RETURN
            IF @Param = 1
            SELECT * FROM Emp
            ELSE
            SELECT * FROM Offices
            END

            The function shall return different table based oppon the condition of the @Param value. Can someone help ???

            With great code, comes great complexity, so keep it simple stupid...:-\ :-\

            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #5

            This is not a valid use for a function, a stored proc maybe but not a function. There is no way that emp and offices are going to be union compatible, if they are then why are they in different tables. You need to rethink your design.

            Select * from anytable

            This is frowned upon, explicitly declare the fields.

            Never underestimate the power of human stupidity RAH

            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