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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:21:36+00:00 2026-06-12T14:21:36+00:00

I am using SQL Server 2008. I have many tables, each table contains ID

  • 0

I am using SQL Server 2008.

I have many tables, each table contains ID field and TheOrder field.

I want to create a stored procedure which can swap values of two records in TheOrder field.

I am getting the IDs of these records by ASP.NET code and pass them to the stored procedure.

But because I want to execute this stored procedure on many tables, I have to use dynamic SQL and pass table name from ASP.NET code.

My stored procedure code is:

ALTER PROCEDURE [dbo].[SwapOrders]
    @TblName nvarchar(20)
    , @ID1 int
    , @ID2 int
AS
BEGIN
    DECLARE @ID1Str nvarchar(5)
    SET @ID1Str = CAST(@ID1 AS nvarchar)
    DECLARE @ID2Str nvarchar(5)
    SET @ID2Str = CAST(@ID2 AS nvarchar)
    DECLARE @Order1 nvarchar(5)
    DECLARE @Order2 nvarchar(5)
    DECLARE @Cmd nvarchar(100)
    /* Get orders */
    SET @Cmd = 'SELECT @Order1 = [TheOrder] FROM ' + @TblName + ' WHERE ID = ' + @ID1Str
    EXECUTE sp_executesql @Cmd, "@Order1 nvarchar(5) OUTPUT", @Order1 = [TheOrder]
    SET @Cmd = 'SELECT @Order2 = [TheOrder] FROM ' + @TblName + ' WHERE ID = ' + @ID2Str
    EXECUTE sp_executesql @Cmd, "@Order2 nvarchar(5) OUTPUT", @Order2 = [TheOrder]
    /* Update orders */
    SET @Cmd = 'UPDATE ' + @TblName + ' SET TheOrder = @Order2 WHERE ID = ' + @ID1Str
    EXECUTE sp_executesql @Cmd, "@Order2 nvarchar(5)", @Order2
    SET @Cmd = 'UPDATE ' + @TblName + ' SET TheOrder = @Order1 WHERE ID = ' + @ID2Str
    EXECUTE sp_executesql @Cmd, "@Order1 nvarchar(5)", @Order1
END
GO

When I tested this code, two values of TheOrder field become NULL.

What is the problem?

Thanks very much for any help, and I am sorry for long question.

  • 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-12T14:21:37+00:00Added an answer on June 12, 2026 at 2:21 pm

    See this working demo

    The code is below

    CREATE PROCEDURE [dbo].[SwapOrders]
      @TblName nvarchar(20),
      @ID1 int,
      @ID2 int
    AS
    set nocount on
    DECLARE @Cmd nvarchar(max) =
     'set nocount on;
      update t
      set TheOrder = u.TheOrder
      from ' + quotename(@tblname) + 't
      join ' + quotename(@tblname) + 'u on
      u.id=case when t.id=@id1 then @id2 else @id1 end
      where t.id in (@id1,@id2);';
    exec sp_executesql @cmd, N'@id1 int,@id2 int', @id1, @id2;
    GO
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok I'm using SQL Server 2008 and have a table field of type VARCHAR(MAX)
I am using SQL Server 2008. I have a table A which accepts many
I'm using SQL Server 2008 and I have a table with three columns: Length
I'm using SQL Server 2008 and I have a table that has a 'Status'
I am using T/SQL in SQL Server 2008 I have a table MyTable with
Using SQL Server 2008. I have multiple Locations which each contain multiple Departments which
I'm using SQL Server 2008. I have a table with x amount of rows.
I'm using SQL Server 2008 and I have 3 tables, x , y and
I have a SQL Server 2008 DB with many tables filled with data and
I am using SQL Server 2008. I have two tables User ( UserID, Name,

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.