I need help with one update- insert with merge in sql Here is the original Insert
INSERT INTO [WarehouseMgmt].[JobSteps] ([JobId],[StepName],[StepNo],[ExecName]) VALUES (N'HOURLY_JOB', N'SyncReportServerReports', 4210, N'WarehouseMgmt.SyncReportServerReports'), (N'MANUAL_JOB', N'SyncReportServerReports', 4210, N'WarehouseMgmt.SyncReportServerReports'), (N'HOURLY_JOB', N'SyncReportServerUserEntries', 4220,N'WarehouseMgmt.SyncReportServerUserEntries'), (N'MANUAL_JOB', N'SyncReportServerUserEntries', 4220, N'WarehouseMgmt.SyncReportServerUserEntries'), (N'HOURLY_JOB', N'SyncReportServerUsers', 4230,N'WarehouseMgmt.SyncReportServerUsers'), (N'MANUAL_JOB', N'SyncReportServerUsers', 4230, N'WarehouseMgmt.SyncReportServerUsers'), (N'HOURLY_JOB', N'StageFactReportServerExecutionLog', 4240, N'WarehouseStaging.StageFactReportServerExecutionLog'), (N'MANUAL_JOB', N'StageFactReportServerExecutionLog', 4240, N'WarehouseStaging.StageFactReportServerExecutionLog'), (N'HOURLY_JOB', N'SyncFactReportServerExecutionLog', 4250, N'WarehouseMgmt.SyncFactReportServerExecutionLog'), (N'MANUAL_JOB', N'SyncFactReportServerExecutionLog', 4250, N'WarehouseMgmt.SyncFactReportServerExecutionLog') INSERT WarehouseMgmt.SyncJobSteps ( JobStepId, StepType, SyncDataSQLId ) SELECT JobStepId = JS.Id, StepType = 'SQL', SyncDataSQLId = SSD.Id FROM WarehouseMgmt.JobSteps JS JOIN ( VALUES ('SyncReportServerReports', 'WarehouseMgmt.DimReportServerReports'), ('SyncReportServerUserEntries', 'WarehouseMgmt.DimReportServerUserEntries'), ('SyncReportServerUsers', 'WarehouseMgmt.DimReportServerUsers'), ('StageFactReportServerExecutionLog', 'WarehouseStaging.FactReportServerExecutionLog'), ('SyncFactReportServerExecutionLog', 'WarehouseMgmt.FactReportServerExecutionLog') ) V([SyncJobStep], [SQLDataObjectName]) ON JS.StepName=V.[SyncJobStep] JOIN WarehouseMgmt.SyncSQLData SSD ON V.[SQLDataObjectName]=SSD.ObjectName and i want to have this i my table
INSERT INTO [WarehouseMgmt].[JobSteps] ([JobId],[StepName],[StepNo],[ExecName]) VALUES (N'HOURLY_JOB', N'SyncReportServerReports', 4210, N'WarehouseMgmt.SyncReportServerReports'), (N'MANUAL_JOB', N'SyncReportServerReports', 4210, N'WarehouseMgmt.SyncReportServerReports'), (N'HOURLY_JOB', N'SyncReportServerUserEntries', 4220,N'WarehouseMgmt.SyncReportServerUserEntries'), (N'MANUAL_JOB', N'SyncReportServerUserEntries', 4220, N'WarehouseMgmt.SyncReportServerUserEntries'), (N'HOURLY_JOB', N'SyncReportServerUsers', 4230,N'WarehouseMgmt.SyncReportServerUsers'), (N'MANUAL_JOB', N'SyncReportServerUsers', 4230, N'WarehouseMgmt.SyncReportServerUsers'), (N'HOURLY_JOB', N'SyncReportServerFormatEntries', 4240,N'WarehouseMgmt.SyncReportServerFormatEntries'), (N'MANUAL_JOB', N'SyncReportServerFormatEntries', 4240, N'WarehouseMgmt.SyncReportServerFormatEntries'), (N'HOURLY_JOB', N'SyncReportServerReportFormat', 4250,N'WarehouseMgmt.SyncReportServerReportFormat'), (N'MANUAL_JOB', N'SyncReportServerReportFormat', 4250, N'WarehouseMgmt.SyncReportServerReportFormat'), (N'HOURLY_JOB', N'StageFactReportServerExecutionLog', 4260, N'WarehouseStaging.StageFactReportServerExecutionLog'), (N'MANUAL_JOB', N'StageFactReportServerExecutionLog', 4260, N'WarehouseStaging.StageFactReportServerExecutionLog'), (N'HOURLY_JOB', N'SyncFactReportServerExecutionLog', 4270, N'WarehouseMgmt.SyncFactReportServerExecutionLog'), (N'MANUAL_JOB', N'SyncFactReportServerExecutionLog', 4270, N'WarehouseMgmt.SyncFactReportServerExecutionLog') INSERT WarehouseMgmt.SyncJobSteps ( JobStepId, StepType, SyncDataSQLId ) SELECT JobStepId = JS.Id, StepType = 'SQL', SyncDataSQLId = SSD.Id FROM WarehouseMgmt.JobSteps JS JOIN ( VALUES ('SyncReportServerReports', 'WarehouseMgmt.DimReportServerReports'), ('SyncReportServerUserEntries', 'WarehouseMgmt.DimReportServerUserEntries'), ('SyncReportServerUsers', 'WarehouseMgmt.DimReportServerUsers'), ('SyncReportServerFormatEntries', 'WarehouseMgmt.DimReportServerFormatEntries'), ('SyncReportServerReportFormat', 'WarehouseMgmt.DimReportServerReportFormat'), ('StageFactReportServerExecutionLog', 'WarehouseStaging.FactReportServerExecutionLog'), ('SyncFactReportServerExecutionLog', 'WarehouseMgmt.FactReportServerExecutionLog') ) V([SyncJobStep], [SQLDataObjectName]) ON JS.StepName=V.[SyncJobStep] JOIN WarehouseMgmt.SyncSQLData SSD ON V.[SQLDataObjectName]=SSD.ObjectName I cannot use only INSERT,because there are already values in database and i don't want to delete them.How can i update 4 values in my first Insert and and 4 new. Look carefully first insert and second
I want something like this
MERGE [WarehouseMgmt].[JobSteps] JS USING #JobSteps TJS ON(TJS.[StepName] = JS.[StepName]) WHEN NOT MATCHED BY TARGET THEN INSERT ([JobId],[StepName],[StepNo],[ExecName]) VALUES (N'HOURLY_JOB', N'SyncReportServerFormatEntries', 4240,N'WarehouseMgmt.SyncReportServerFormatEntries'), (N'MANUAL_JOB', N'SyncReportServerFormatEntries', 4240, N'WarehouseMgmt.SyncReportServerFormatEntries'), (N'HOURLY_JOB', N'SyncReportServerReportFormat', 4250,N'WarehouseMgmt.SyncReportServerReportFormat'), (N'MANUAL_JOB', N'SyncReportServerReportFormat', 4250, N'WarehouseMgmt.SyncReportServerReportFormat') WHEN MATCHED THEN UPDATE JS.[StepNo] = TJS.[StepNo] ; Why this is wrong ?