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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:51:25+00:00 2026-06-19T01:51:25+00:00

I have a stored procedure used for updating records in a SQL Server database.

  • 0

I have a stored procedure used for updating records in a SQL Server database.

I want to use the existing value within the database if a match is found and a null is provided as the variable.

Often only partial data is available so the procedure is executed at several stages to complete the record. I do not always have all variables at each execution.

Why is the below CASE not working? If a null value is provided, it replaces existing data within the field.

ALTER PROCEDURE [dbo].[sp_UPSERT_Test] 

@ID int,
@Account_ID varchar(15)
@Name varchar(15)

AS 
BEGIN 

MERGE INTO dbo.Database_Log AS target  
USING ( 
   SELECT @ID, @Account_ID ,@Account_ID)  
   AS source (ID, Account_ID, Name)
ON target.ID = source.ID 

WHEN MATCHED
THEN UPDATE
SET
    Account_ID = CASE 
    WHEN source.Account_ID = NULL 
    THEN target.Account_ID 
    ELSE source.Account_ID 
        END
    ,Name = CASE 
    WHEN source.Name = NULL 
    THEN target.Name 
    ELSE source.Name 
        END)

WHEN NOT MATCHED  
THEN INSERT (Account_ID, Name)  
       VALUES (Account_ID, Name);

RETURN
END
  • 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-19T01:51:27+00:00Added an answer on June 19, 2026 at 1:51 am

    Maybe because you’re not properly checking for NULL ? NULL is not a value you can compare with the equality operator – you must use IS NULL

    Try this:

     SET      
        Account_ID = CASE 
                        WHEN source.Account_ID IS NULL 
                        THEN target.Account_ID 
                        ELSE source.Account_ID 
                     END
    

    Use IS NULL instead of = NULL

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

Sidebar

Related Questions

I have this stored procedure that I want to use for my SAP crystal
I have a stored procedure where I am passing a value to be used
Afternoon All, I have a stored procedure in an SQL 2005 database named GasNominationsRawData_Insert.
SQL Server 2005. I have a stored procedure which accepts a few parameters that
Need to have a stored procedure that calls a SQL Server Agent Job and
I have a stored procedure in SQL Server CREATE PROCEDURE ParseXML (@InputXML xml) The
I have a stored procedure in a MS-SQL 2005 database that: Creates two temp
I have stored procedure in my database and i need to look up a
I have a stored procedure that has the parameter: @desk VARCHAR(50) I want to
I have a stored procedure which will be used to populate a report in

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.