Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8492811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:49:54+00:00 2026-06-10T22:49:54+00:00

I have a stored procedure that calculates a special value for a given record

  • 0

I have a stored procedure that calculates a special value for a given record based on the information contained within that record and some information from other tables. I’d like to write a query that returns a result table containing each record’s regular information, with the addition of each record’s calculated value in a new column. For example, I want something like this:

SELECT
   [id]     as Name,
   [shape]  as Shape,
   [color]  as Color,
   EXEC FindCode
       @id = [id]
       @shape = [shape]
       @color = [color] 
   as Code
FROM Shapes

With the equivalent of the above ‘pseudo’ code, I’d expect to get back a result set like this:

Name |  Shape  | Color | Code
-----+---------+-------+-----
AB   |  Circle | Blue  | 4276
BC   |  Square | Red   | 9825
CD   |  Rect   | Gray  | 3723

Where the Name, Shape, and Color, were already contained in the table as id, shape, and color, but the ‘Code’ was calculated using the stored procedure. What’s the best way to go about doing this in SQL Server 2008 R2?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T22:49:56+00:00Added an answer on June 10, 2026 at 10:49 pm

    Here are a few examples of what your query might look like using some of the suggestions in this thread.

    1. CROSS APPLYing a scalar-valued user-defined function:

    SELECT
         s.[id]     as Name
        ,s.[shape]  as Shape
        ,s.[color]  as Color
        ,c.[code]   as Code
    FROM 
        [Shapes] s
    CROSS APPLY
        fnFunctionThatCalculatesCodeAsAScalarValue(s.[id], s.[shape], s.[color]) c
    

    2. Changing your table definition to have a computed column:

    CREATE TABLE Shapes AS
    (
        [id] int NOT NULL,
        [shape] varchar(40) NOT NULL,
        [color] varchar(40) NOT NULL,
        [code] AS fnFunctionThatCalculatesCodeAsAScalarValue ([id], [shape],[color])
    )
    

    3. JOINing a table-valued user-defined function:

    SELECT
         s.[id]     as Name
        ,s.[shape]  as Shape
        ,s.[color]  as Color
        ,c.[code]   as Code
    FROM 
        [Shapes] s
    JOIN
        fnFunctionThatCalculatesCodesAsATable() c
        ON
        s.[id] = c.[id]
        AND
        s.[shape] = c.[shape]
        AND
        s.[color] = c.[color]
    

    Or 4. you could just pre-fill a different table will all of the possible color codes (if feasible).

    The use of the scalar-value functions as-is in examples 1 and 2 will perform the worst, especially if it has to go after other tables, because it will execute once per row.

    You can improve the performance of the computed column example by tacking on the PERSISTED keyword after the column definition. This is probably how I would do it. The persisted values will be calculated once upon insert, auto-updated when a row updates, but pulled from its persisted location for a select statement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a stored procedure that I want to call from within another, and
I have a stored procedure that takes a user ID and calculates their balance
I have a stored procedure that logs some data, how can I call this
I have a stored procedure in my database that calculates the distance between two
I have a stored procedure in SQL 2005 that calculates distance using the Haversine
I have a clr stored procedure that has to return decimal value. I cannot
I have a stored procedure that has the parameter: @desk VARCHAR(50) I want to
I have this stored procedure that I want to use for my SAP crystal
I have a stored procedure that returns data in the format of: Date |
I have a stored procedure (that I didn't write) that uses openquery to populate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.