2011-08-25 51 views
0

我得到了下面的SQL代码,我需要如何检查是否有行和执行插入

  1. 执行INSERT只有在代码返回大于0的行。
  2. 在屏幕上显示执行脚本的人的消息,说 没有检测到缺失的行或者检测到并添加了3行缺失行。

    SELECT * FROM DistributionKey_Section其中SectionID 不 ( 从站点选择SITEID其中SiteTypeCodeID IN(8) ) 和DistributionKeyID NOT IN

( 从DistributionKey其中UnitInclusive = 1 选择DistributionKeyID)

回答

1
DECLARE 
    @MissingRows int, 
    @InsertedRows int  

SELECT * 
FROM DistributionKey_Section 
WHERE SectionID NOT IN (select siteid from Site where SiteTypeCodeID IN(8)) AND 
     DistributionKeyID NOT IN (SELECT DistributionKeyID FROM DistributionKey WHERE 
     UnitInclusive=1) 

SET @MissingRows = @@ROWCOUNT  

IF @MissingRows > 0 
    BEGIN 
    <Insert Statement/Logic> 
    SET @InsertedRows = @@ROWCOUNT 
    PRINT CAST(@InsertedRows as varchar(5)) + ' missing rows were detected and added' 
    IF @MissingRows <> @InsertedRows 
     BEGIN 
     RAISERROR('The number of rows inserted does not equal the number of rows missing', 16, 1) 
     END 
    END 
ELSE 
    PRINT 'No Missing Rows Detected' 
+1

这应该让你指出正确的方向。我没有碰到插件,因为我不确定你想要插入什么以及在哪里。 –

+0

thx,那很快。 –

相关问题