MATCH上的SQL MERGE条件删除

时间:2017-06-30 12:17:46

标签: sql sql-server tsql visual-studio-2015

我有两张桌子用于merge。我的查询有问题,我找不到任何关于原因的信息。会发生什么;我将运行查询,它工作正常,因为我的目标表填充了我想要的信息。然后我再次运行查询(紧接其后)并更改(38行受影响)我再次运行它并将行添加回来 - 再次...行被删除。有超过100行但似乎只有相同的行受到影响。源表中没有任何变化。

我怀疑我在DELETE上执行了WHEN MATCHED - 我只能找到DELETE WHEN NOT MATCHED的信息 - 但我不知道,因为这似乎只会影响到MERGE QA.dbo.RMA AS target USING Touchstn02.dbo.RMA_Detail AS source ON (target.RMANUM_52 = source.RMANUM_52) WHEN MATCHED AND (source.STATUS_52 >3) THEN DELETE WHEN NOT MATCHED AND (source.STATUS_52 < 4) AND (source.RECQTY_52 > 0) THEN INSERT (RMANUM_52, RMADATE_52, CUSTID_52, RETNUM_52, RETQTY_52, SERIAL_52, REPAIR_52, RPLACE_52, CREDIT_52, WRKNUM_52, KEYWRD_52, RECQTY_52, RECDTE_52, STATUS_52, REM1_52, REM2_52, REM3_52, Comment, CMPDTE) VALUES (source.RMANUM_52, source.RMADTE_52, source.CUSTID_52, source.RETNUM_52, source.RETQTY_52, source.SERIAL_52, source.REPAIR_52, source.RPLACE_52, source.CREDIT_52, source.WRKNUM_52, source.KEYWRD_52, source.RECQTY_52, source.RECDTE_52, source.STATUS_52, source.REM1_52, source.REM2_52, source.REM3_52, source.REM4_52, source.CMPDTE_52); 每次38行。

您需要的表格和(希望)信息在我的代码中:

<telerik:RadGridView.RowDetailsTemplate>
    <DataTemplate>
        <StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="Auto" Background="#303134" Name="RowDetails">
            <Grid Margin="10 10">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="1*" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>

                <!--SUMMARY-->
                <Grid Margin="10 10" Grid.ColumnSpan="2">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <TextBlock Text="SUMMARY" FontSize="14" FontFamily="Segoe UI Bold"  Foreground="White" Margin="4 0 0 5" />
                    <Rectangle Height="1" Fill="#A6A6A6" Grid.Row="1" VerticalAlignment="Center" Margin="2 0 2 10" Stretch="Fill" Grid.ColumnSpan="2" />
                    <TextBlock Text="Address: " FontSize="12" FontFamily="Segoe UI Bold"  Foreground="#A6A6A6" Margin="4 2" Grid.Row="2" />
                    <TextBlock Text="1501 Main Street Columbia, SC" FontSize="12" FontFamily="Segoe UI"  Foreground="#A6A6A6" Margin="4 2" 
                                       Grid.Row="2" Grid.Column="1" HorizontalAlignment="Left" />
                    <TextBlock Text="Subtype: " FontSize="12" FontFamily="Segoe UI Bold"  Foreground="#A6A6A6" Margin="4 2" Grid.Row="3" />
                    <TextBlock Text="Active Shooter on Foot" FontSize="12" FontFamily="Segoe UI"  Foreground="#A6A6A6" Margin="4 2" Grid.Row="3" 
                                       Grid.Column="1" HorizontalAlignment="Left"/>
                    <TextBlock Text="Dispatch Group: " FontSize="12" FontFamily="Segoe UI Bold"  Foreground="#A6A6A6" Margin="4 2" Grid.Row="4"/>
                    <TextBlock Text="District 22" FontSize="12" FontFamily="Segoe UI"  Foreground="#A6A6A6" Margin="4 2" Grid.Row="4" Grid.Column="1" 
                                       HorizontalAlignment="Left"/>
                </Grid>

                <!--COMMENTS-->
                <Grid Margin="10 10" Grid.Row="1" Grid.ColumnSpan="2">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="1*" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <TextBlock Text="COMMENTS" FontSize="14" FontFamily="Segoe UI Bold"  Foreground="White" Margin="4 10 0 5" />
                    <Rectangle Height="1" Fill="#A6A6A6" Grid.Row="1" VerticalAlignment="Center" Margin="2 0 2 10" Stretch="Fill" />
                    <TextBlock Text="Event Comments" FontSize="12" FontFamily="Segoe UI Bold"  Foreground="#A6A6A6" Margin="4 6" Grid.Row="2"/>
                    <Border Grid.Row="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="100" BorderBrush="#535354">
                        <TextBox Grid.Row="3" Name="EventComments" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" TextAlignment="Left"
                                     Margin="4 0 10 10" TextWrapping="Wrap" Text="Comments from CAD system here" IsReadOnly="True" Height="100" Background="#3F4145" 
                                     BorderThickness="0" Foreground="#A6A6A6" />
                    </Border>
                    <TextBlock Text="Location Comments" FontSize="12" FontFamily="Segoe UI Bold"  Foreground="#A6A6A6" Margin="4 6" Grid.Row="4"/>
                    <Border Grid.Row="5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="100" BorderBrush="#535354">
                        <TextBox Grid.Row="5" Name="LocationComments" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" TextAlignment="Left"
                                     Margin="4 0 10 10" TextWrapping="Wrap" IsReadOnly="True" Height="100" Background="#3F4145" 
                                     BorderThickness="0" Foreground="#A6A6A6" />
                    </Border>
                </Grid>

                <!--RESPONDING ASSETS-->
                <Grid Margin="10 10" Grid.Row="2" Grid.ColumnSpan="2">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="1*" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="300" />
                    </Grid.RowDefinitions>
                    <TextBlock Text="RESPONDING ASSETS" FontSize="14" FontFamily="Segoe UI Bold"  Foreground="White" Margin="4 10 0 5" />
                    <Rectangle Height="1" Fill="#A6A6A6" Grid.Row="1" VerticalAlignment="Center" Margin="2 0 2 10" Stretch="Fill" />

                    <!--ENTITY LIST-->

                    <Grid Grid.Row="2">
                        <ListBox ItemsSource="{Binding FilteredTrackList}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="0 5 0 10">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <Grid Margin="0 2">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="100" />
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Text="{Binding TrackName.Value}" />
                                        <TextBlock Text="{Binding DateCreated}" />
                                    </Grid>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </Grid>

                </Grid>
            </Grid>
        </StackPanel>
    </DataTemplate>
</telerik:RadGridView.RowDetailsTemplate>

与往常一样,我感谢任何帮助/输入

1 个答案:

答案 0 :(得分:0)

我想我已经得到了它!

要删除的38行在源表中至少有两个STATUS_52值,用于这些RMANUM_52键值。

对于给定的RMANUM_52键值:

  • 其中一个STATUS_52值将是&lt; 4,并且具有数量&gt; 0.
  • 另一行状态值将是&gt; 3。

因此,在第一次运行时,插入一行STATUS_52值&lt; 4。

然后在下一次运行DELETE时触发,因为匹配是由RMANUM_52和SOURCE表中的STATUS_52进行的。这里棘手的是我们正在回顾源表中所有行的状态值(包括不属于第一个插入的行)。并且源表中有一个不同的行,它将RMANUM_52与STATUS_52&gt;匹配。 3.所以DELETE逻辑匹配和删除发生。

我无法从你的例子中确切地知道你的要求是什么,所以我不会冒险猜测修复。