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.)