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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:59:38+00:00 2026-06-08T12:59:38+00:00

I created a stored procedure to update only the rows of a table that

  • 0

I created a stored procedure to update only the rows of a table that have changed

I used the SQLSERVER coalesce function.

CREATE PROCEDURE update_only_changed

            @FName varchar(50) = NULL,

            @LName varchar(50) = NULL,

            @CUST_DB VARCHAR(20) 

AS

BEGIN

            DECLARE @QUERY VARCHAR(255)

            SET @QUERY = 'UPDATE ' + @CUST_DB + '..people SET LastName = COALESCE(@LName, LastName) WHERE id = 1'

            EXEC (@QUERY)

END

GO

Some considerations

  • Is a dynamic sql

  • The name of the database is passed as a parameter in the procedure

So, When I run the sql I have two situations:

  • EXEC(@ QUERY):

    I get the error saying: Must declare the scalar variable "@ LName".
    it does not interpret the variable that the command coalesce
    
  • EXEC @QUERY (without parentheses)

     I get the error saying: The database 'UPDATE CUSTOMER' does not exist. Make sure the name is spelled correctly.
    

how can i put that to work ?

Remember, if a run the procedure with the update statement static, without EXEC, it works

UPDATE CUSTOMER..people SET LastName = COALESCE(@LName, LastName) WHERE id = 1
  • 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-08T12:59:40+00:00Added an answer on June 8, 2026 at 12:59 pm

    The first one doesn’t work because there is no variable @Lname in the context of the exec – it only exists in your procedure’s scope. You need to pass it into the scope of the query by using sp_executesql instead of exec

    exec sp_executesql @query, N'@LName varchar(50)', @Lname
    

    BUT

    I wouldn’t recommend doing anything at all like this.

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

Sidebar

Related Questions

I want to create stored procedure that call recursion function, each time it will
I would like to have a stored procedure that will update values in a
I have a stored procedure in MySQL that should update a column in a
How can I create a stored procedure to update the Std_Course's column based on
I have a dynamic SQL statement I've created in a stored procedure. I need
I have just created a report in Report Manager using a Stored Procedure which
I am attempting to create a Stored Procedure for a newly created database. However
SQL Server 2008 R2: normally, we create our table, and stored procedure, and grant
I am trying to make one stored procedure only in SQL Server that lets
I have a stored procedure in mysql thats to perform a task that needs

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.