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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:03:26+00:00 2026-05-11T08:03:26+00:00

I have a table that stores formulary drug information and needs to be updated

  • 0

I have a table that stores formulary drug information and needs to be updated daily from a central formulary. The temp table is identical to the drug table. The temp table data could be identical (and on most days will be) to the main table or it could have updated rows or new rows.

I have a stored procedure to update the main table, but it fails because it won’t update NULL rows (if there is a new row in the temp table).

This is a MSSQL Server 2005.

Where am I going wrong here:

    -- Insert statements for procedure here UPDATE [RX_Billing].[dbo].[FS_Drug] SET [TRADENAME] = [RX_Billing].[dbo].[FS_Drug_TEMP].[TRADENAME]   ,[CDM] = [RX_Billing].[dbo].[FS_Drug_TEMP].[CDM]   ,[NDC] = [RX_Billing].[dbo].[FS_Drug_TEMP].[NDC]   ,[IP_COST] = [RX_Billing].[dbo].[FS_Drug_TEMP].[IP_COST]   ,[OP_COST] = [RX_Billing].[dbo].[FS_Drug_TEMP].[OP_COST]   ,[HH_COST] = [RX_Billing].[dbo].[FS_Drug_TEMP].[HH_COST]   ,[VAR_COST] = [RX_Billing].[dbo].[FS_Drug_TEMP].[VAR_COST]   ,[LSTUPDATE] = [RX_Billing].[dbo].[FS_Drug_TEMP].[LSTUPDATE] FROM [RX_Billing].[dbo].[FS_Drug] RIGHT OUTER JOIN [RX_Billing].[dbo].[FS_Drug_TEMP] ON            [RX_Billing].[dbo].[FS_Drug].[TRADENAME] =                     [RX_Billing].[dbo].[FS_Drug_TEMP].[TRADENAME] 

EDIT:

I went with Rory’s code. Thanks, that works beautifully.

A note to Orion Edwards: UPSERT/MERGE is exactly what I wanted, but it is not supported under SQL Server 2005. Apparently it was planned, but didn’t make that release. It is available in Server 2008. (From what the Interwebs has told me.)

  • 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. 2026-05-11T08:03:27+00:00Added an answer on May 11, 2026 at 8:03 am

    Standard way is to do an UPDATE and then an INSERT:

    -- UPDATE rows using an INNER JOIN with matching TRADENAME. No need to update TRADENAME column. UPDATE drug SET [CDM] = tmp.[CDM]   , [NDC] = tmp.[NDC]   , [IP_COST] = tmp.[IP_COST]   , [OP_COST] = tmp.[OP_COST]   , [HH_COST] = tmp.[HH_COST]   , [VAR_COST] = tmp.[VAR_COST]   , [LSTUPDATE] = tmp.[LSTUPDATE] FROM [RX_Billing].[dbo].[FS_Drug] drug INNER JOIN [RX_Billing].[dbo].[FS_Drug_TEMP] tmp       ON drug.[TRADENAME] = tmp.[TRADENAME]  -- Insert rows that don't have matching TRADENAME INSERT INTO drug SELECT      tmp.[TRADENAME]   , tmp.[CDM]   , tmp.[NDC]   , tmp.[IP_COST]   , tmp.[OP_COST]   , tmp.[HH_COST]   , tmp.[VAR_COST]   , tmp.[LSTUPDATE] FROM [RX_Billing].[dbo].[FS_Drug] drug RIGHT OUTER JOIN [RX_Billing].[dbo].[FS_Drug_TEMP] tmp      ON  drug.[TRADENAME] = tmp.[TRADENAME] WHERE drug.[TRADENAME] IS NULL  

    You might also want to delete or flag as deleted any records in drug that aren’t in tmp any more. Do that as a separate statement same as the UPDATE but with a LEFT OUTER JOIN where tmp.TRADENAME IS NULL.

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

Sidebar

Related Questions

I have a table that stores data that has been entered regarding the amount
I have a table that stores user information. The table has a userid (identity)
I have a table that stores the data as follows: ID ListId ModuleId RecordId
I have a database table that stores imported information. For simplicity, its something like:
I have a table that stores scores from users of my game - what
I have a table User that stores user information - such as name, date
I have a table that stores product price information. It has a column for
I have a table that stores customer items. The table needs to reflect the
I have a table that stores messages from one user to another. messages(user_id,friend_id,message,created_date). My
I have a table that stores a person's information with close to 10 million

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.