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 3433204
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:32:43+00:00 2026-05-18T07:32:43+00:00

I am trying to write a Stored Procedure in SQL Server (2005) to do

  • 0

I am trying to write a Stored Procedure in SQL Server (2005) to do something that sounds simple, but is actually proving to be more difficult that I thought.

I have a table with 30 columns and 50,000 rows.

The number of records is fixed, but users can edit the fields of existing records.

To save them having to re-key repetitive data, I want to give them the ability to select a record, and specify a range of IDs to copy those details to.

The SP I’m trying to write will take 3 parameters: The source record primary key, and the lower and upper primary keys of the range of records that the data will be copied into.

Obviously the PKs of the destination records remain unchanged.

So I figured the SP needs to do a SELECT – to get all the data to be copied, and an UPDATE – to write the data into the specified destination records.

I just don’t know how to store the results of the SELECT to slot them into the UPDATE.
A temp table wouldn’t help – selecting from that would be just the same as selecting from the table!

What I need is a variable that is effectively a single record, so I can go something like:

@tempRECORD = SELECT * FROM SOURCETABLE WHERE ID = @sourcePK

UPDATE SOURCETABLE
SET FIELD1 = @tempRECORD.FIELD1,  
  FIELD2 = @tempRECORD.FIELD2,  
  ...
  FIELD30 = @tempRECORD.FIELD30  
WHERE ID >= @LOWER_id AND ID <= @UPPER_id

But I don’t know how, or if you even can.
I’m also open to any other clever way I haven’t even thought of!

Thanks guys!

  • 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-05-18T07:32:43+00:00Added an answer on May 18, 2026 at 7:32 am

    So I figured the SP needs to do a SELECT – to get all the data to be copied, and an UPDATE – to write the data into the specified destination records.

    What you need is the T-SQL-specific extension to UPDATE, UPDATE ... FROM:

    UPDATE T
    SET
        Field1 = source.Field1
        , Field2 = source.Field2
        , Field3 = source.Field3
    FROM
        (SELECT * FROM T AS source_T WHERE source_T.ID = @sourcePK) as source
    WHERE
        T.ID BETWEEN @LOWER_Id AND @UPPER_Id
    

    Note that this ability to put a FROM clause in an UPDATE statement is not standard ANSI SQL, so I don’t know how this would be done in other RDBMSs.

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

Sidebar

Related Questions

I'm trying to write a stored procedure [SQL 2008] with a dynamic, auto incrementing
I'm trying to write a Stored Procedure which'll get a string, hash it with
I'm trying to write a sql statement that will insert data given a few
I am trying to run a stored procedure that has multiple in and out
So I'm trying to use a stored procedure via a cursor and callproc() but
I am trying to write a stored proicedure that gets all the townID's from
I am trying to write a complicated stored procedure for the first time. My
I have a stored procedure that adds an object to the database and returns
I'm working on a system to store appointments and recurring appointments. My schema looks

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.