So I'm writing a Visual Studio SQL database project for an existing database.
I'm generating simple merge scripts per table for standing data, existing data so on and so forth. It's been relatively straight forward until I come to one particular table which is giving me the error in the title.
Now, if this was a complex query I'd understand, but it's not, it has one key field with a clustered index and no complex operations. I have tried removing all of the ISNULL checks from the MATCHED check after the first AND and it still gives me the same error. I don't understand!
They key is [my_table_code], the first column. Here is the bulk of the query with only the first row for clarity.
MERGE INTO [my_table] AS Target
USING (VALUES
(N'00001L',N'20160425',N'20160425',N'132011',N'DMTEAM',N'C',N'Name',N'F',365,0,N'19790702',N'20130418',N'20130418',N'20130418',N'20130418',N'',0.0,0.0,0,N'GBP',1.0,N'19790702',N'N',N'N',0,0,0,0,N'Y',N'SLAC Life Series 1',N'19790703',365,N'UK',-1,N'N',N'H',N'N',0,N'',N'ALL',N'10000101',N'',0,0,N'0',N'N',N'N',0.000000000000000e+000,N'',N'E',365,0,0,N'N',365,0,0,N'N',0,0,N'',N'',0,N'',N'DEF',N'',N'',0,0,N'',N'',-1,N' ',N' ',N' ',N' ',N' ',N'E',N' ',N'',N' ',N' ',N' ',0,1.5,4.5,7.5,0.0,0,N'0',N'0')
-- 2600 rows more
) AS Source ([my_table_code],[my_table_create_date],[my_table_last_updt_date],[my_table_last_updt_time],[my_table_last_updt_userid],[my_table_record_status],[my_table_name],[my_table_next_or_last_price],[my_table_pricing_frequency],[my_table_sell_pricing_frequency],[my_table_commenced_date],[my_table_closed_date],[my_table_closed_for_newbus_date],[my_table_closed_for_spi_date],[my_table_closed_for_money_date],[my_table_category],[my_table_spread_relationship],[my_table_management_charge],[my_table_sellout_period_mths],[my_table_currency],[my_table_price_tolerance],[my_table_initial_price_date],[my_table_last_date_in_period],[my_table_sell_last_date_in_period],[my_table_last_day_in_period],[my_table_sell_last_day_in_period],[my_table_same_day_in_period],[my_table_sell_same_day_in_period],[my_table_use_working_days],[my_table_external_id],[my_table_post_initial_price_date],[my_table_price_applies_frequency],[my_table_price_country_code],[my_table_non_working_price_days_adj],[my_table_pricing_calendar],[my_table_risk_profile],[my_table_pay_dividend],[my_table_dividend_frequency],[my_table_sedol],[my_table_type],[my_table_strike_date],[my_table_maturity_date],[my_table_amc_basis_no],[my_table_coupon_select_id],[my_table_coupon_defaults],[my_table_whole_units],[my_table_clone_by_policy],[my_table_initial_clone_price],[my_table_price_base_calc_date],[my_table_group],[my_table_disinv_notice_freq],[my_table_disinv_notice_value],[my_table_disinv_same_day_in_period],[my_table_disinv_notice_last_date],[my_table_trade_cutoff_freq],[my_table_trade_cutoff_value],[my_table_trade_cutoff_same_day_in_period],[my_table_trade_cutoff_last_date],[my_table_expected_delay_rules],[my_table_settlement_days],[my_table_bid_offer_exception_rules],[my_table_holding_fund_id],[my_table_timeout_days],[my_table_price_valuation_basis],[my_table_classification],[my_table_target_days],[my_table_sell_target_days],[my_table_disinv_same_day_period_skip],[my_table_trade_cutoff_same_day_period_skip],[my_table_equity_sector],[my_table_client_classification],[my_table_max_decimal_places],[my_table_rounding_method],[my_table_closing_time],[my_table_rounding_direction],[my_table_use_external_WS],[my_table_gl_code],[my_table_asset_class],[my_table_grouping],[my_table_factsheet_url],[my_table_aladdin_ticker],[my_table_manager],[my_table_use_notice_for_backdates],[my_table_business_date_delay],[my_table_growth_low],[my_table_growth_med],[my_table_growth_high],[my_table_growth_SMPI],[my_table_instruction_file_decimal_places],[my_table_suspend_start_date],[my_table_suspend_end_date])
ON (Target.[my_table_code] = Source.[my_table_code])
WHEN MATCHED AND (
NULLIF(Source.[my_table_create_date], Target.[my_table_create_date]) IS NOT NULL OR NULLIF(Target.[my_table_create_date], Source.[my_table_create_date]) IS NOT NULL OR
NULLIF(Source.[my_table_last_updt_date], Target.[my_table_last_updt_date]) IS NOT NULL OR NULLIF(Target.[my_table_last_updt_date], Source.[my_table_last_updt_date]) IS NOT NULL OR
NULLIF(Source.[my_table_last_updt_time], Target.[my_table_last_updt_time]) IS NOT NULL OR NULLIF(Target.[my_table_last_updt_time], Source.[my_table_last_updt_time]) IS NOT NULL OR
NULLIF(Source.[my_table_last_updt_userid], Target.[my_table_last_updt_userid]) IS NOT NULL OR NULLIF(Target.[my_table_last_updt_userid], Source.[my_table_last_updt_userid]) IS NOT NULL OR
NULLIF(Source.[my_table_record_status], Target.[my_table_record_status]) IS NOT NULL OR NULLIF(Target.[my_table_record_status], Source.[my_table_record_status]) IS NOT NULL OR
NULLIF(Source.[my_table_name], Target.[my_table_name]) IS NOT NULL OR NULLIF(Target.[my_table_name], Source.[my_table_name]) IS NOT NULL OR
NULLIF(Source.[my_table_next_or_last_price], Target.[my_table_next_or_last_price]) IS NOT NULL OR NULLIF(Target.[my_table_next_or_last_price], Source.[my_table_next_or_last_price]) IS NOT NULL OR
NULLIF(Source.[my_table_pricing_frequency], Target.[my_table_pricing_frequency]) IS NOT NULL OR NULLIF(Target.[my_table_pricing_frequency], Source.[my_table_pricing_frequency]) IS NOT NULL OR
NULLIF(Source.[my_table_sell_pricing_frequency], Target.[my_table_sell_pricing_frequency]) IS NOT NULL OR NULLIF(Target.[my_table_sell_pricing_frequency], Source.[my_table_sell_pricing_frequency]) IS NOT NULL OR
NULLIF(Source.[my_table_commenced_date], Target.[my_table_commenced_date]) IS NOT NULL OR NULLIF(Target.[my_table_commenced_date], Source.[my_table_commenced_date]) IS NOT NULL OR
NULLIF(Source.[my_table_closed_date], Target.[my_table_closed_date]) IS NOT NULL OR NULLIF(Target.[my_table_closed_date], Source.[my_table_closed_date]) IS NOT NULL OR
NULLIF(Source.[my_table_closed_for_newbus_date], Target.[my_table_closed_for_newbus_date]) IS NOT NULL OR NULLIF(Target.[my_table_closed_for_newbus_date], Source.[my_table_closed_for_newbus_date]) IS NOT NULL OR
NULLIF(Source.[my_table_closed_for_spi_date], Target.[my_table_closed_for_spi_date]) IS NOT NULL OR NULLIF(Target.[my_table_closed_for_spi_date], Source.[my_table_closed_for_spi_date]) IS NOT NULL OR
NULLIF(Source.[my_table_closed_for_money_date], Target.[my_table_closed_for_money_date]) IS NOT NULL OR NULLIF(Target.[my_table_closed_for_money_date], Source.[my_table_closed_for_money_date]) IS NOT NULL OR
NULLIF(Source.[my_table_category], Target.[my_table_category]) IS NOT NULL OR NULLIF(Target.[my_table_category], Source.[my_table_category]) IS NOT NULL OR
NULLIF(Source.[my_table_spread_relationship], Target.[my_table_spread_relationship]) IS NOT NULL OR NULLIF(Target.[my_table_spread_relationship], Source.[my_table_spread_relationship]) IS NOT NULL OR
NULLIF(Source.[my_table_management_charge], Target.[my_table_management_charge]) IS NOT NULL OR NULLIF(Target.[my_table_management_charge], Source.[my_table_management_charge]) IS NOT NULL OR
NULLIF(Source.[my_table_sellout_period_mths], Target.[my_table_sellout_period_mths]) IS NOT NULL OR NULLIF(Target.[my_table_sellout_period_mths], Source.[my_table_sellout_period_mths]) IS NOT NULL OR
NULLIF(Source.[my_table_currency], Target.[my_table_currency]) IS NOT NULL OR NULLIF(Target.[my_table_currency], Source.[my_table_currency]) IS NOT NULL OR
NULLIF(Source.[my_table_price_tolerance], Target.[my_table_price_tolerance]) IS NOT NULL OR NULLIF(Target.[my_table_price_tolerance], Source.[my_table_price_tolerance]) IS NOT NULL OR
NULLIF(Source.[my_table_initial_price_date], Target.[my_table_initial_price_date]) IS NOT NULL OR NULLIF(Target.[my_table_initial_price_date], Source.[my_table_initial_price_date]) IS NOT NULL OR
NULLIF(Source.[my_table_last_date_in_period], Target.[my_table_last_date_in_period]) IS NOT NULL OR NULLIF(Target.[my_table_last_date_in_period], Source.[my_table_last_date_in_period]) IS NOT NULL OR
NULLIF(Source.[my_table_sell_last_date_in_period], Target.[my_table_sell_last_date_in_period]) IS NOT NULL OR NULLIF(Target.[my_table_sell_last_date_in_period], Source.[my_table_sell_last_date_in_period]) IS NOT NULL OR
NULLIF(Source.[my_table_last_day_in_period], Target.[my_table_last_day_in_period]) IS NOT NULL OR NULLIF(Target.[my_table_last_day_in_period], Source.[my_table_last_day_in_period]) IS NOT NULL OR
NULLIF(Source.[my_table_sell_last_day_in_period], Target.[my_table_sell_last_day_in_period]) IS NOT NULL OR NULLIF(Target.[my_table_sell_last_day_in_period], Source.[my_table_sell_last_day_in_period]) IS NOT NULL OR
NULLIF(Source.[my_table_same_day_in_period], Target.[my_table_same_day_in_period]) IS NOT NULL OR NULLIF(Target.[my_table_same_day_in_period], Source.[my_table_same_day_in_period]) IS NOT NULL OR
NULLIF(Source.[my_table_sell_same_day_in_period], Target.[my_table_sell_same_day_in_period]) IS NOT NULL OR NULLIF(Target.[my_table_sell_same_day_in_period], Source.[my_table_sell_same_day_in_period]) IS NOT NULL OR
NULLIF(Source.[my_table_use_working_days], Target.[my_table_use_working_days]) IS NOT NULL OR NULLIF(Target.[my_table_use_working_days], Source.[my_table_use_working_days]) IS NOT NULL OR
NULLIF(Source.[my_table_external_id], Target.[my_table_external_id]) IS NOT NULL OR NULLIF(Target.[my_table_external_id], Source.[my_table_external_id]) IS NOT NULL OR
NULLIF(Source.[my_table_post_initial_price_date], Target.[my_table_post_initial_price_date]) IS NOT NULL OR NULLIF(Target.[my_table_post_initial_price_date], Source.[my_table_post_initial_price_date]) IS NOT NULL OR
NULLIF(Source.[my_table_price_applies_frequency], Target.[my_table_price_applies_frequency]) IS NOT NULL OR NULLIF(Target.[my_table_price_applies_frequency], Source.[my_table_price_applies_frequency]) IS NOT NULL OR
NULLIF(Source.[my_table_price_country_code], Target.[my_table_price_country_code]) IS NOT NULL OR NULLIF(Target.[my_table_price_country_code], Source.[my_table_price_country_code]) IS NOT NULL OR
NULLIF(Source.[my_table_non_working_price_days_adj], Target.[my_table_non_working_price_days_adj]) IS NOT NULL OR NULLIF(Target.[my_table_non_working_price_days_adj], Source.[my_table_non_working_price_days_adj]) IS NOT NULL OR
NULLIF(Source.[my_table_pricing_calendar], Target.[my_table_pricing_calendar]) IS NOT NULL OR NULLIF(Target.[my_table_pricing_calendar], Source.[my_table_pricing_calendar]) IS NOT NULL OR
NULLIF(Source.[my_table_risk_profile], Target.[my_table_risk_profile]) IS NOT NULL OR NULLIF(Target.[my_table_risk_profile], Source.[my_table_risk_profile]) IS NOT NULL OR
NULLIF(Source.[my_table_pay_dividend], Target.[my_table_pay_dividend]) IS NOT NULL OR NULLIF(Target.[my_table_pay_dividend], Source.[my_table_pay_dividend]) IS NOT NULL OR
NULLIF(Source.[my_table_dividend_frequency], Target.[my_table_dividend_frequency]) IS NOT NULL OR NULLIF(Target.[my_table_dividend_frequency], Source.[my_table_dividend_frequency]) IS NOT NULL OR
NULLIF(Source.[my_table_sedol], Target.[my_table_sedol]) IS NOT NULL OR NULLIF(Target.[my_table_sedol], Source.[my_table_sedol]) IS NOT NULL OR
NULLIF(Source.[my_table_type], Target.[my_table_type]) IS NOT NULL OR NULLIF(Target.[my_table_type], Source.[my_table_type]) IS NOT NULL OR
NULLIF(Source.[my_table_strike_date], Target.[my_table_strike_date]) IS NOT NULL OR NULLIF(Target.[my_table_strike_date], Source.[my_table_strike_date]) IS NOT NULL OR
NULLIF(Source.[my_table_maturity_date], Target.[my_table_maturity_date]) IS NOT NULL OR NULLIF(Target.[my_table_maturity_date], Source.[my_table_maturity_date]) IS NOT NULL OR
NULLIF(Source.[my_table_amc_basis_no], Target.[my_table_amc_basis_no]) IS NOT NULL OR NULLIF(Target.[my_table_amc_basis_no], Source.[my_table_amc_basis_no]) IS NOT NULL OR
NULLIF(Source.[my_table_coupon_select_id], Target.[my_table_coupon_select_id]) IS NOT NULL OR NULLIF(Target.[my_table_coupon_select_id], Source.[my_table_coupon_select_id]) IS NOT NULL OR
NULLIF(Source.[my_table_coupon_defaults], Target.[my_table_coupon_defaults]) IS NOT NULL OR NULLIF(Target.[my_table_coupon_defaults], Source.[my_table_coupon_defaults]) IS NOT NULL OR
NULLIF(Source.[my_table_whole_units], Target.[my_table_whole_units]) IS NOT NULL OR NULLIF(Target.[my_table_whole_units], Source.[my_table_whole_units]) IS NOT NULL OR
NULLIF(Source.[my_table_clone_by_policy], Target.[my_table_clone_by_policy]) IS NOT NULL OR NULLIF(Target.[my_table_clone_by_policy], Source.[my_table_clone_by_policy]) IS NOT NULL OR
NULLIF(Source.[my_table_initial_clone_price], Target.[my_table_initial_clone_price]) IS NOT NULL OR NULLIF(Target.[my_table_initial_clone_price], Source.[my_table_initial_clone_price]) IS NOT NULL OR
NULLIF(Source.[my_table_price_base_calc_date], Target.[my_table_price_base_calc_date]) IS NOT NULL OR NULLIF(Target.[my_table_price_base_calc_date], Source.[my_table_price_base_calc_date]) IS NOT NULL OR
NULLIF(Source.[my_table_group], Target.[my_table_group]) IS NOT NULL OR NULLIF(Target.[my_table_group], Source.[my_table_group]) IS NOT NULL OR
NULLIF(Source.[my_table_disinv_notice_freq], Target.[my_table_disinv_notice_freq]) IS NOT NULL OR NULLIF(Target.[my_table_disinv_notice_freq], Source.[my_table_disinv_notice_freq]) IS NOT NULL OR
NULLIF(Source.[my_table_disinv_notice_value], Target.[my_table_disinv_notice_value]) IS NOT NULL OR NULLIF(Target.[my_table_disinv_notice_value], Source.[my_table_disinv_notice_value]) IS NOT NULL OR
NULLIF(Source.[my_table_disinv_same_day_in_period], Target.[my_table_disinv_same_day_in_period]) IS NOT NULL OR NULLIF(Target.[my_table_disinv_same_day_in_period], Source.[my_table_disinv_same_day_in_period]) IS NOT NULL OR
NULLIF(Source.[my_table_disinv_notice_last_date], Target.[my_table_disinv_notice_last_date]) IS NOT NULL OR NULLIF(Target.[my_table_disinv_notice_last_date], Source.[my_table_disinv_notice_last_date]) IS NOT NULL OR
NULLIF(Source.[my_table_trade_cutoff_freq], Target.[my_table_trade_cutoff_freq]) IS NOT NULL OR NULLIF(Target.[my_table_trade_cutoff_freq], Source.[my_table_trade_cutoff_freq]) IS NOT NULL OR
NULLIF(Source.[my_table_trade_cutoff_value], Target.[my_table_trade_cutoff_value]) IS NOT NULL OR NULLIF(Target.[my_table_trade_cutoff_value], Source.[my_table_trade_cutoff_value]) IS NOT NULL OR
NULLIF(Source.[my_table_trade_cutoff_same_day_in_period], Target.[my_table_trade_cutoff_same_day_in_period]) IS NOT NULL OR NULLIF(Target.[my_table_trade_cutoff_same_day_in_period], Source.[my_table_trade_cutoff_same_day_in_period]) IS NOT NULL OR
NULLIF(Source.[my_table_trade_cutoff_last_date], Target.[my_table_trade_cutoff_last_date]) IS NOT NULL OR NULLIF(Target.[my_table_trade_cutoff_last_date], Source.[my_table_trade_cutoff_last_date]) IS NOT NULL OR
NULLIF(Source.[my_table_expected_delay_rules], Target.[my_table_expected_delay_rules]) IS NOT NULL OR NULLIF(Target.[my_table_expected_delay_rules], Source.[my_table_expected_delay_rules]) IS NOT NULL OR
NULLIF(Source.[my_table_settlement_days], Target.[my_table_settlement_days]) IS NOT NULL OR NULLIF(Target.[my_table_settlement_days], Source.[my_table_settlement_days]) IS NOT NULL OR
NULLIF(Source.[my_table_bid_offer_exception_rules], Target.[my_table_bid_offer_exception_rules]) IS NOT NULL OR NULLIF(Target.[my_table_bid_offer_exception_rules], Source.[my_table_bid_offer_exception_rules]) IS NOT NULL OR
NULLIF(Source.[my_table_holding_fund_id], Target.[my_table_holding_fund_id]) IS NOT NULL OR NULLIF(Target.[my_table_holding_fund_id], Source.[my_table_holding_fund_id]) IS NOT NULL OR
NULLIF(Source.[my_table_timeout_days], Target.[my_table_timeout_days]) IS NOT NULL OR NULLIF(Target.[my_table_timeout_days], Source.[my_table_timeout_days]) IS NOT NULL OR
NULLIF(Source.[my_table_price_valuation_basis], Target.[my_table_price_valuation_basis]) IS NOT NULL OR NULLIF(Target.[my_table_price_valuation_basis], Source.[my_table_price_valuation_basis]) IS NOT NULL OR
NULLIF(Source.[my_table_classification], Target.[my_table_classification]) IS NOT NULL OR NULLIF(Target.[my_table_classification], Source.[my_table_classification]) IS NOT NULL OR
NULLIF(Source.[my_table_target_days], Target.[my_table_target_days]) IS NOT NULL OR NULLIF(Target.[my_table_target_days], Source.[my_table_target_days]) IS NOT NULL OR
NULLIF(Source.[my_table_sell_target_days], Target.[my_table_sell_target_days]) IS NOT NULL OR NULLIF(Target.[my_table_sell_target_days], Source.[my_table_sell_target_days]) IS NOT NULL OR
NULLIF(Source.[my_table_disinv_same_day_period_skip], Target.[my_table_disinv_same_day_period_skip]) IS NOT NULL OR NULLIF(Target.[my_table_disinv_same_day_period_skip], Source.[my_table_disinv_same_day_period_skip]) IS NOT NULL OR
NULLIF(Source.[my_table_trade_cutoff_same_day_period_skip], Target.[my_table_trade_cutoff_same_day_period_skip]) IS NOT NULL OR NULLIF(Target.[my_table_trade_cutoff_same_day_period_skip], Source.[my_table_trade_cutoff_same_day_period_skip]) IS NOT NULL OR
NULLIF(Source.[my_table_equity_sector], Target.[my_table_equity_sector]) IS NOT NULL OR NULLIF(Target.[my_table_equity_sector], Source.[my_table_equity_sector]) IS NOT NULL OR
NULLIF(Source.[my_table_client_classification], Target.[my_table_client_classification]) IS NOT NULL OR NULLIF(Target.[my_table_client_classification], Source.[my_table_client_classification]) IS NOT NULL OR
NULLIF(Source.[my_table_max_decimal_places], Target.[my_table_max_decimal_places]) IS NOT NULL OR NULLIF(Target.[my_table_max_decimal_places], Source.[my_table_max_decimal_places]) IS NOT NULL OR
NULLIF(Source.[my_table_rounding_method], Target.[my_table_rounding_method]) IS NOT NULL OR NULLIF(Target.[my_table_rounding_method], Source.[my_table_rounding_method]) IS NOT NULL OR
NULLIF(Source.[my_table_closing_time], Target.[my_table_closing_time]) IS NOT NULL OR NULLIF(Target.[my_table_closing_time], Source.[my_table_closing_time]) IS NOT NULL OR
NULLIF(Source.[my_table_rounding_direction], Target.[my_table_rounding_direction]) IS NOT NULL OR NULLIF(Target.[my_table_rounding_direction], Source.[my_table_rounding_direction]) IS NOT NULL OR
NULLIF(Source.[my_table_use_external_WS], Target.[my_table_use_external_WS]) IS NOT NULL OR NULLIF(Target.[my_table_use_external_WS], Source.[my_table_use_external_WS]) IS NOT NULL OR
NULLIF(Source.[my_table_gl_code], Target.[my_table_gl_code]) IS NOT NULL OR NULLIF(Target.[my_table_gl_code], Source.[my_table_gl_code]) IS NOT NULL OR
NULLIF(Source.[my_table_asset_class], Target.[my_table_asset_class]) IS NOT NULL OR NULLIF(Target.[my_table_asset_class], Source.[my_table_asset_class]) IS NOT NULL OR
NULLIF(Source.[my_table_grouping], Target.[my_table_grouping]) IS NOT NULL OR NULLIF(Target.[my_table_grouping], Source.[my_table_grouping]) IS NOT NULL OR
NULLIF(Source.[my_table_factsheet_url], Target.[my_table_factsheet_url]) IS NOT NULL OR NULLIF(Target.[my_table_factsheet_url], Source.[my_table_factsheet_url]) IS NOT NULL OR
NULLIF(Source.[my_table_aladdin_ticker], Target.[my_table_aladdin_ticker]) IS NOT NULL OR NULLIF(Target.[my_table_aladdin_ticker], Source.[my_table_aladdin_ticker]) IS NOT NULL OR
NULLIF(Source.[my_table_manager], Target.[my_table_manager]) IS NOT NULL OR NULLIF(Target.[my_table_manager], Source.[my_table_manager]) IS NOT NULL OR
NULLIF(Source.[my_table_use_notice_for_backdates], Target.[my_table_use_notice_for_backdates]) IS NOT NULL OR NULLIF(Target.[my_table_use_notice_for_backdates], Source.[my_table_use_notice_for_backdates]) IS NOT NULL OR
NULLIF(Source.[my_table_business_date_delay], Target.[my_table_business_date_delay]) IS NOT NULL OR NULLIF(Target.[my_table_business_date_delay], Source.[my_table_business_date_delay]) IS NOT NULL OR
NULLIF(Source.[my_table_growth_low], Target.[my_table_growth_low]) IS NOT NULL OR NULLIF(Target.[my_table_growth_low], Source.[my_table_growth_low]) IS NOT NULL OR
NULLIF(Source.[my_table_growth_med], Target.[my_table_growth_med]) IS NOT NULL OR NULLIF(Target.[my_table_growth_med], Source.[my_table_growth_med]) IS NOT NULL OR
NULLIF(Source.[my_table_growth_high], Target.[my_table_growth_high]) IS NOT NULL OR NULLIF(Target.[my_table_growth_high], Source.[my_table_growth_high]) IS NOT NULL OR
NULLIF(Source.[my_table_growth_SMPI], Target.[my_table_growth_SMPI]) IS NOT NULL OR NULLIF(Target.[my_table_growth_SMPI], Source.[my_table_growth_SMPI]) IS NOT NULL OR
NULLIF(Source.[my_table_instruction_file_decimal_places], Target.[my_table_instruction_file_decimal_places]) IS NOT NULL OR NULLIF(Target.[my_table_instruction_file_decimal_places], Source.[my_table_instruction_file_decimal_places]) IS NOT NULL OR
NULLIF(Source.[my_table_suspend_start_date], Target.[my_table_suspend_start_date]) IS NOT NULL OR NULLIF(Target.[my_table_suspend_start_date], Source.[my_table_suspend_start_date]) IS NOT NULL OR
NULLIF(Source.[my_table_suspend_end_date], Target.[my_table_suspend_end_date]) IS NOT NULL OR NULLIF(Target.[my_table_suspend_end_date], Source.[my_table_suspend_end_date]) IS NOT NULL ) THEN
UPDATE SET
[my_table_create_date] = Source.[my_table_create_date],
[my_table_last_updt_date] = Source.[my_table_last_updt_date],
-- All fields listed in order.
[my_table_suspend_end_date] = Source.[my_table_suspend_end_date]
WHEN NOT MATCHED BY TARGET THEN
INSERT([my_table_code],[my_table_create_date],[my_table_last_updt_date],[my_table_last_updt_time],[my_table_last_updt_userid],[my_table_record_status],[my_table_name],[my_table_next_or_last_price],[my_table_pricing_frequency],[my_table_sell_pricing_frequency],[my_table_commenced_date],[my_table_closed_date],[my_table_closed_for_newbus_date],[my_table_closed_for_spi_date],[my_table_closed_for_money_date],[my_table_category],[my_table_spread_relationship],[my_table_management_charge],[my_table_sellout_period_mths],[my_table_currency],[my_table_price_tolerance],[my_table_initial_price_date],[my_table_last_date_in_period],[my_table_sell_last_date_in_period],[my_table_last_day_in_period],[my_table_sell_last_day_in_period],[my_table_same_day_in_period],[my_table_sell_same_day_in_period],[my_table_use_working_days],[my_table_external_id],[my_table_post_initial_price_date],[my_table_price_applies_frequency],[my_table_price_country_code],[my_table_non_working_price_days_adj],[my_table_pricing_calendar],[my_table_risk_profile],[my_table_pay_dividend],[my_table_dividend_frequency],[my_table_sedol],[my_table_type],[my_table_strike_date],[my_table_maturity_date],[my_table_amc_basis_no],[my_table_coupon_select_id],[my_table_coupon_defaults],[my_table_whole_units],[my_table_clone_by_policy],[my_table_initial_clone_price],[my_table_price_base_calc_date],[my_table_group],[my_table_disinv_notice_freq],[my_table_disinv_notice_value],[my_table_disinv_same_day_in_period],[my_table_disinv_notice_last_date],[my_table_trade_cutoff_freq],[my_table_trade_cutoff_value],[my_table_trade_cutoff_same_day_in_period],[my_table_trade_cutoff_last_date],[my_table_expected_delay_rules],[my_table_settlement_days],[my_table_bid_offer_exception_rules],[my_table_holding_fund_id],[my_table_timeout_days],[my_table_price_valuation_basis],[my_table_classification],[my_table_target_days],[my_table_sell_target_days],[my_table_disinv_same_day_period_skip],[my_table_trade_cutoff_same_day_period_skip],[my_table_equity_sector],[my_table_client_classification],[my_table_max_decimal_places],[my_table_rounding_method],[my_table_closing_time],[my_table_rounding_direction],[my_table_use_external_WS],[my_table_gl_code],[my_table_asset_class],[my_table_grouping],[my_table_factsheet_url],[my_table_aladdin_ticker],[my_table_manager],[my_table_use_notice_for_backdates],[my_table_business_date_delay],[my_table_growth_low],[my_table_growth_med],[my_table_growth_high],[my_table_growth_SMPI],[my_table_instruction_file_decimal_places],[my_table_suspend_start_date],[my_table_suspend_end_date])
VALUES(Source.[my_table_code],Source.[my_table_create_date],Source.[my_table_last_updt_date],Source.[my_table_last_updt_time],Source.[my_table_last_updt_userid],Source.[my_table_record_status],Source.[my_table_name],Source.[my_table_next_or_last_price],Source.[my_table_pricing_frequency],Source.[my_table_sell_pricing_frequency],Source.[my_table_commenced_date],Source.[my_table_closed_date],Source.[my_table_closed_for_newbus_date],Source.[my_table_closed_for_spi_date],Source.[my_table_closed_for_money_date],Source.[my_table_category],Source.[my_table_spread_relationship],Source.[my_table_management_charge],Source.[my_table_sellout_period_mths],Source.[my_table_currency],Source.[my_table_price_tolerance],Source.[my_table_initial_price_date],Source.[my_table_last_date_in_period],Source.[my_table_sell_last_date_in_period],Source.[my_table_last_day_in_period],Source.[my_table_sell_last_day_in_period],Source.[my_table_same_day_in_period],Source.[my_table_sell_same_day_in_period],Source.[my_table_use_working_days],Source.[my_table_external_id],Source.[my_table_post_initial_price_date],Source.[my_table_price_applies_frequency],Source.[my_table_price_country_code],Source.[my_table_non_working_price_days_adj],Source.[my_table_pricing_calendar],Source.[my_table_risk_profile],Source.[my_table_pay_dividend],Source.[my_table_dividend_frequency],Source.[my_table_sedol],Source.[my_table_type],Source.[my_table_strike_date],Source.[my_table_maturity_date],Source.[my_table_amc_basis_no],Source.[my_table_coupon_select_id],Source.[my_table_coupon_defaults],Source.[my_table_whole_units],Source.[my_table_clone_by_policy],Source.[my_table_initial_clone_price],Source.[my_table_price_base_calc_date],Source.[my_table_group],Source.[my_table_disinv_notice_freq],Source.[my_table_disinv_notice_value],Source.[my_table_disinv_same_day_in_period],Source.[my_table_disinv_notice_last_date],Source.[my_table_trade_cutoff_freq],Source.[my_table_trade_cutoff_value],Source.[my_table_trade_cutoff_same_day_in_period],Source.[my_table_trade_cutoff_last_date],Source.[my_table_expected_delay_rules],Source.[my_table_settlement_days],Source.[my_table_bid_offer_exception_rules],Source.[my_table_holding_fund_id],Source.[my_table_timeout_days],Source.[my_table_price_valuation_basis],Source.[my_table_classification],Source.[my_table_target_days],Source.[my_table_sell_target_days],Source.[my_table_disinv_same_day_period_skip],Source.[my_table_trade_cutoff_same_day_period_skip],Source.[my_table_equity_sector],Source.[my_table_client_classification],Source.[my_table_max_decimal_places],Source.[my_table_rounding_method],Source.[my_table_closing_time],Source.[my_table_rounding_direction],Source.[my_table_use_external_WS],Source.[my_table_gl_code],Source.[my_table_asset_class],Source.[my_table_grouping],Source.[my_table_factsheet_url],Source.[my_table_aladdin_ticker],Source.[my_table_manager],Source.[my_table_use_notice_for_backdates],Source.[my_table_business_date_delay],Source.[my_table_growth_low],Source.[my_table_growth_med],Source.[my_table_growth_high],Source.[my_table_growth_SMPI],Source.[my_table_instruction_file_decimal_places],Source.[my_table_suspend_start_date],Source.[my_table_suspend_end_date])
WHEN NOT MATCHED BY SOURCE THEN
DELETE
;
GO
DECLARE #mergeError int
, #mergeCount int
SELECT #mergeError = ##ERROR, #mergeCount = ##ROWCOUNT
IF #mergeError != 0
BEGIN
PRINT 'ERROR OCCURRED IN MERGE FOR [my_table]. Rows affected: ' + CAST(#mergeCount AS VARCHAR(100)); -- SQL should always return zero rows affected
END
ELSE
BEGIN
PRINT '[my_table] rows affected by MERGE: ' + CAST(#mergeCount AS VARCHAR(100));
END
GO
EDIT: Ok so removing 2508 rows it works in seconds. I don't know enough about merge scripts to know why the amount of rows would affect this.
Inserting the values into a temp table first and referencing that in the merge fixed it.
Also, replacing the long list of NULLIF with:
WHEN MATCHED AND EXISTS
(
SELECT Source.*
EXCEPT
SELECT Target.*
)
worked to speed it up.
I need to write T-SQL code that will compare between T1.PercentComplete that need to be between T2.StageFrom and T2.StageTo. and than get the T2.Bonus_Prec and join T1
T1:
T2:
The desired result for T2.Bonus_Prec is 0.02 since T1.Percent_Complete is .27, which is between 0 and 1.
The thing is that each Key can have a different T2.StageID between 1-6.
If Key have just one T2.StageID it'll be 0. (fast way for me to know that there is only 1 bonus option)
If it have more than 1 it's will start with 1. (This can be changed if needed)
T1:
DROP TABLE T1;
CREATE TABLE T1(
Key VARCHAR(10) NOT NULL PRIMARY KEY
,Percent_Complete_ NUMBER(16,2) NOT NULL
);
INSERT INTO T1(Key,Percent_Complete_) VALUES ('Key Vendor',Percent_Complete);
INSERT INTO T1(Key,Percent_Complete_) VALUES ('***',0.27);
T2:
DROP TABLE T2;
CREATE TABLE T2(
Key VARCHAR(50) NOT NULL
,StageID INT NOT NULL
,Stage_From NUMERIC(10,2) NOT NULL
,Stage_To NUMERIC(8,2) NOT NULL
,Stage_Bonus_Prec NUMERIC(16,2) NOT NULL
);
INSERT INTO T2(Key,StageID,Stage_From,Stage_To,Stage_Bonus_Prec) VALUES ('Key',Stage_Id,Stage_From,Stage_To,Stage_Bonus_Prec);
INSERT INTO T2(Key,StageID,Stage_From,Stage_To,Stage_Bonus_Prec) VALUES ('***',1,0,0.8,0.02);
INSERT INTO T2(Key,StageID,Stage_From,Stage_To,Stage_Bonus_Prec) VALUES ('***',2,0.8,1,0.035);
INSERT INTO T2(Key,StageID,Stage_From,Stage_To,Stage_Bonus_Prec) VALUES ('***',3,1,-1,0.05);
OUTPUT:
+-----+-------------------+--------------------+
| Key | Percent_Complete | [Stage_Bonus_Prec] |
+-----+-------------------+--------------------+
| *** | 0.27 | 0.02 |
+-----+-------------------+--------------------+
Here is a SQLFiddle with these values
It is still not clear what you are trying to do but I made an attempt. Please notice I also corrected a number of issues with ddl and sample data you posted.
if OBJECT_ID('T1') is not null
drop table T1
CREATE TABLE T1(
KeyVendor VARCHAR(10) NOT NULL PRIMARY KEY
,PercentComplete VARCHAR(16) NOT NULL
);
INSERT INTO T1(KeyVendor,PercentComplete) VALUES ('***','0.27');
if OBJECT_ID('T2') is not null
drop table T2
CREATE TABLE T2(
MyKey VARCHAR(50) NOT NULL
,StageID INT NOT NULL
,Stage_From NUMERIC(10,0) NOT NULL
,Stage_To NUMERIC(8,0) NOT NULL
,Stage_Bonus_Prec NUMERIC(16,3) NOT NULL
);
INSERT INTO T2(MyKey,StageID,Stage_From,Stage_To,Stage_Bonus_Prec) VALUES ('***',1,0,0.8,0.02);
INSERT INTO T2(MyKey,StageID,Stage_From,Stage_To,Stage_Bonus_Prec) VALUES ('***',2,0.8,1,0.035);
INSERT INTO T2(MyKey,StageID,Stage_From,Stage_To,Stage_Bonus_Prec) VALUES ('***',3,1,-1,0.05);
select *
from T1
cross apply
(
select top 1 Stage_Bonus_Prec
from T2
where t1.PercentComplete >= t2.Stage_Bonus_Prec
and t1.KeyVendor = t2.MyKey
order by Stage_Bonus_Prec
) x
Taking a shot at this as well, since it's still a bit unclear:
SELECT t1.percent_complete, t2.Stage_Bonus_Prec
FROM T1 INNER JOIN T2
ON T1.[key vendor] = T2.[Key] AND
T1.[percent_complete] BETWEEN T2.Stage_From AND T2.Stage_To
Joining T1 and T2 on [Key Vendor] and [Key] and using the BETWEEN operator to find the percent_complete value that is between Stage_From and Stage_To.
I think I'm still missing something since I'm still confused about where Key value of *** comes from in your desired results.
SQLFiddle of this in action, based on a slightly fixed up version of your DDL (you put your field names in their own data record, I've removed them since they don't belong there).
I've been through a bunch of examples and other questions on SO, but I'm still having an issue with this pivot.
select [Address],[Address 1],[Address 2],[Address2],[Birth Day],[City],
[Email],[First Name],[Last Name],[Phone],[State],[Zip]
from
(SELECT *, row_number() over(PARTITION by formid order by formid) as rnk
FROM [Sitecore_WebForms].[dbo].[Field]
where FormId='C9681226-5951-403D-80A0-00019218785C'
) as SourceTable
pivot
(
max(value)
for [fieldname] in
([Address],[Address 1],[Address 2],[Address2],[Birth Day],[City],[Email],
[First Name],[Last Name],[Phone],[State],[Zip])
) as pt
The result comes back with 9 rows, 1 row for each column. How do I get it to combine these rows into a single row?
Address Address 1 Address 2 Address2 Birth Day City Email First Name Last Name Phone State Zip
NULL NULL NULL NULL NULL NULL NULL FIRS NULL NULL NULL NULL
NULL NULL NULL NULL NULL CITY NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL NULL LAST NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL ZIP
ADDR NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL DATE NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL EMAI NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL NULL NULL PHON NULL NULL
NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL
source rows
Id FieldId FormId Value Data FieldName
CE160D15-EBCC-45B6-AB67-0CCA4AA081C7 09C0317E-E235-469D-AE7E-6CB9C45916EA C9681226-5951-403D-80A0-00019218785C FIRS First Name
393E6220-8CC8-4B4E-A75B-29E1801A9A65 2ED6CAC8-7210-4BBC-9497-3B0E8A5F7CF7 C9681226-5951-403D-80A0-00019218785C LAST Last Name
64F9A694-406E-4385-932E-2D0D68A17FB7 FA5C1C21-CCFC-4E96-A1D8-57AC7B1AEF2C C9681226-5951-403D-80A0-00019218785C ADDR Address
960A2BB5-0B50-4640-8EC8-170677809E64 CA9184AE-99CD-46C0-A72D-47FBD4F768AB C9681226-5951-403D-80A0-00019218785C CITY City
CD954912-B24A-4726-9729-2ACD6DDD00C9 E695546B-2BBA-4AFF-9DAF-1B5666E42C74 C9681226-5951-403D-80A0-00019218785C ZIP Zip
FD19C907-F779-4CF8-90B7-4E072CDE36BE DF056320-27BD-4E15-BF73-AC0C4A1CF543 C9681226-5951-403D-80A0-00019218785C DATE Birth Day
497AC034-6686-4023-ADE9-CB825F65DBFE 06807268-1689-4DDF-BDE2-08FBA31E56AE C9681226-5951-403D-80A0-00019218785C PHON Phone
8A438C22-9C8E-4759-998F-542917408E38 D9402E5E-09CE-40E6-86F7-D80D99417208 C9681226-5951-403D-80A0-00019218785C EMAI Email
If we only select value and filedName from Source it will work as expected.
select [Address],[Address 1],[Address 2],[Address2],[Birth Day],[City],
[Email],[First Name],[Last Name],[Phone],[State],[Zip]
from
(SELECT value, fieldname
FROM [Sitecore_WebForms].[dbo].[Field]
where FormId='C9681226-5951-403D-80A0-00019218785C'
) as SourceTable
pivot
(
max(value)
for [fieldname] in
([Address],[Address 1],[Address 2],[Address2],[Birth Day],[City],[Email],
[First Name],[Last Name],[Phone],[State],[Zip])
) as pt
Update for all the forms:
DECLARE #form TABLE
(
id INT,
formid INT,
fieldName VARCHAR(32),
fieldValue VARCHAR(32)
)
insert into #form values(1,1,'First Name','FRST')
insert into #form values(2,1,'Last Name','LST')
insert into #form values(3,1,'Address','ADDR')
insert into #form values(4,2,'First Name','FRST2')
insert into #form values(5,2,'Last Name','LST2')
insert into #form values(6,2,'Address','ADDR2')
select FormId,[Address],[Address 1],[Address 2],[Address2],[Birth Day],[City],
[Email],[First Name],[Last Name],[Phone],[State],[Zip]
from
(SELECT FormId,fieldName,fieldValue
FROM #form
--where FormId=1
) as SourceTable
pivot
(
max(fieldValue)
for [fieldname] in
([Address],[Address 1],[Address 2],[Address2],[Birth Day],[City],[Email],
[First Name],[Last Name],[Phone],[State],[Zip])
) as pt
PIVOT will only "rotate" the columns you list; any other columns will still occopy their own row. In your case, both Id and FieldId will cause each row to remain.
To solve that, exclude those columns in a subquery:
select *
from (
select FormId
, Value
, FieldName
from #t
) as SourceTable
pivot (
max(value)
for FieldName in
(Address,[Address 1],[Address 2],Address2,...)
) as PivotAlias;
Working example at SE DATA.