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 Stored Procedures as a field (SQL Server 2008)

Using Stored Procedures as a field (SQL Server 2008)

Scheduled Pinned Locked Moved Database
databasesharepointsql-serversysadmintutorial
6 Posts 2 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.
  • W Offline
    W Offline
    willempipi
    wrote on last edited by
    #1

    Hi, I have made a stored procedure witch returns a field that i want to use in a query. There is 1 parameter that I give to the stored procedure witch comes from the normal query. The info about the stored procedure: Name: SP_Calculate Input: ID Output: table with 1 column: Amount and 0 or 1 row. And this is what I want to do in the query:

    SELECT
    db1.dbo.table1.ID as ID,
    db1.dbo.table1.Field1 as Field1,
    db2.dbo.table2.Field2 as Field2,
    ( SELECT Amount FROM db1.dbo.SP_Calculate(db1.dbo.table1.ID) ) As Amount

    FROM db1.dbo.table1

    INNER JOIN db2.dbo.table2
    ON db1.dbo.table1.ID = db2.dbo.table2.ID

    Of course this code doesn't work, but i hope it gives you a good view of what I try to do. This query is a example query, the actual query is really long.

    C W 2 Replies Last reply
    0
    • W willempipi

      Hi, I have made a stored procedure witch returns a field that i want to use in a query. There is 1 parameter that I give to the stored procedure witch comes from the normal query. The info about the stored procedure: Name: SP_Calculate Input: ID Output: table with 1 column: Amount and 0 or 1 row. And this is what I want to do in the query:

      SELECT
      db1.dbo.table1.ID as ID,
      db1.dbo.table1.Field1 as Field1,
      db2.dbo.table2.Field2 as Field2,
      ( SELECT Amount FROM db1.dbo.SP_Calculate(db1.dbo.table1.ID) ) As Amount

      FROM db1.dbo.table1

      INNER JOIN db2.dbo.table2
      ON db1.dbo.table1.ID = db2.dbo.table2.ID

      Of course this code doesn't work, but i hope it gives you a good view of what I try to do. This query is a example query, the actual query is really long.

      C Offline
      C Offline
      Corporal Agarn
      wrote on last edited by
      #2

      If you are expecting one (or no) value why not use a user defined function? The function could return NULL for no value

      W 1 Reply Last reply
      0
      • C Corporal Agarn

        If you are expecting one (or no) value why not use a user defined function? The function could return NULL for no value

        W Offline
        W Offline
        willempipi
        wrote on last edited by
        #3

        Could you give me an example how to use a function in my query? I have never used functions in sql server, only views/stored procedures and triggers. Converting my stored procedure to a function would not be the biggest problem, i think.

        C 1 Reply Last reply
        0
        • W willempipi

          Hi, I have made a stored procedure witch returns a field that i want to use in a query. There is 1 parameter that I give to the stored procedure witch comes from the normal query. The info about the stored procedure: Name: SP_Calculate Input: ID Output: table with 1 column: Amount and 0 or 1 row. And this is what I want to do in the query:

          SELECT
          db1.dbo.table1.ID as ID,
          db1.dbo.table1.Field1 as Field1,
          db2.dbo.table2.Field2 as Field2,
          ( SELECT Amount FROM db1.dbo.SP_Calculate(db1.dbo.table1.ID) ) As Amount

          FROM db1.dbo.table1

          INNER JOIN db2.dbo.table2
          ON db1.dbo.table1.ID = db2.dbo.table2.ID

          Of course this code doesn't work, but i hope it gives you a good view of what I try to do. This query is a example query, the actual query is really long.

          W Offline
          W Offline
          willempipi
          wrote on last edited by
          #4

          Hi, I already got the solution, thanks to djj55: I converted the Stored Procedure into a Database Function, now i can use it in the query like this:

          SELECT
          db1.dbo.table1.ID as ID,
          db1.dbo.table1.Field1 as Field1,
          db2.dbo.table2.Field2 as Field2,
          db1.dbo.Calculate(db1.dbo.table1.ID) As Amount

          FROM db1.dbo.table1

          INNER JOIN db2.dbo.table2
          ON db1.dbo.table1.ID = db2.dbo.table2.ID

          C 1 Reply Last reply
          0
          • W willempipi

            Could you give me an example how to use a function in my query? I have never used functions in sql server, only views/stored procedures and triggers. Converting my stored procedure to a function would not be the biggest problem, i think.

            C Offline
            C Offline
            Corporal Agarn
            wrote on last edited by
            #5

            A function is very similar to a stored procedure. If your stored procedure is db1.dbo.SP_Calculate(db1.dbo.table1.ID) the your function could be db1.dbo.udf_Calculate(db1.dbo.table1.ID) See BOL for a complete explanation of CREATE FUNCTION (scalar).

            USE DB1
            GO
            CREATE FUNCTION dbo.udf_Calculate ( @inID VARCHAR(10))
            RETURNS money
            AS
            DECLARE @myout MONEY;
            SELECT @myout = amount from mytable where ID = @inID;
            RETURN(@myout);

            This is a quick of the cuff example. Good luck

            1 Reply Last reply
            0
            • W willempipi

              Hi, I already got the solution, thanks to djj55: I converted the Stored Procedure into a Database Function, now i can use it in the query like this:

              SELECT
              db1.dbo.table1.ID as ID,
              db1.dbo.table1.Field1 as Field1,
              db2.dbo.table2.Field2 as Field2,
              db1.dbo.Calculate(db1.dbo.table1.ID) As Amount

              FROM db1.dbo.table1

              INNER JOIN db2.dbo.table2
              ON db1.dbo.table1.ID = db2.dbo.table2.ID

              C Offline
              C Offline
              Corporal Agarn
              wrote on last edited by
              #6

              Glad you got it. It took me a while to type my last replay as I am at work. :)

              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