Sql 如果找到行,则触发代码;如果未返回,则不执行任何操作

Sql 如果找到行,则触发代码;如果未返回,则不执行任何操作,sql,sql-server,if-statement,Sql,Sql Server,If Statement,我有我在过去已经测试和使用过的代码。我想现在用if语句自动化这段代码。也就是说,当返回行时,我希望使用游标执行该过程。如果找不到任何记录或什么也没有返回,我不想发生任何事情,干脆停止 附件,是我的代码我如何才能正确嵌入if语句 declare @date smalldatetime set @date = (select max(date) from inventory_table) select symbol, id, count(distinct 2) cnt into #targ

我有我在过去已经测试和使用过的代码。我想现在用if语句自动化这段代码。也就是说,当返回行时,我希望使用游标执行该过程。如果找不到任何记录或什么也没有返回,我不想发生任何事情,干脆停止

附件,是我的代码我如何才能正确嵌入if语句

declare @date smalldatetime 
set @date = (select max(date) from inventory_table) 



select symbol, id, count(distinct 2) cnt into #target 
from inventory_table 
where date between dateadd(day, -7, @date) and @date
group by symbol,id 
having count(distinct 2) >= 4 


-- Process with cursor starts here
DECLARE @MyList TABLE (iid int)
INSERT INTO @MyList 
select distinct id from #target


DECLARE @iid int

DECLARE db_cursor CURSOR FOR  
SELECT iid FROM @MyList
OPEN db_cursor   
FETCH NEXT FROM db_cursor INTO @iid   

WHILE @@FETCH_STATUS = 0   
BEGIN 

    declare @mindate date,
    @maxdate date

    set @mindate = ( select min(date) from inventory_table where ID = @iid  )
    set @maxdate =  ( select max(date) from inventory_table where ID = @iid   )

      exec spReissuingIDs @mindate , @maxdate, @iid

       FETCH NEXT FROM db_cursor INTO @iid   
END   

CLOSE db_cursor   
DEALLOCATE db_cursor

只需稍作修改,您就可以处理所请求的需求,消除光标的使用,并进行一些优化

-- Define @MyList --
DECLARE @MyList TABLE (
    ListId INT IDENTITY(1,1) PRIMARY KEY,
    Iid INT,
    MinDate DATETIME,
    MaxDate DATETIME
);

-- Fetch Data --
INSERT INTO @MyList (
    Iid, MinDate, MaxDate
)
SELECT DISTINCT
    t.id, i.MinDate, i.MaxDate
FROM #target AS t
CROSS APPLY (
    SELECT MIN( [date] ) AS MinDate, MAX( [date] ) AS MaxDate FROM inventory_table WHERE id = t.id 
) AS i;

DECLARE @ListId INT, @iid INT, @min DATETIME, @max DATETIME;

-- Set @ListId to 1 for the first row.
SET @ListId = 1; 

-- While @ListId <= to the number of rows in @MyList, run spReissuingIDs for each Iid --
-- Note: If no rows are present, spReissuingIDs will never be called --
WHILE ( @ListId <= ( SELECT MAX( ListId ) FROM @MyList ) )
BEGIN

    -- Current Row --
    SELECT
        @iid = Iid, @min = MinDate, @max = MaxDate
    FROM @MyList WHERE ListId = @ListId;

    -- Process Row --
    EXEC spReissuingIDs @min, @max, @iid

    -- Next Row --
    SET @ListId = ( @ListId + 1 );

END

只需稍作修改,您就可以处理所请求的需求,消除光标的使用,并进行一些优化

-- Define @MyList --
DECLARE @MyList TABLE (
    ListId INT IDENTITY(1,1) PRIMARY KEY,
    Iid INT,
    MinDate DATETIME,
    MaxDate DATETIME
);

-- Fetch Data --
INSERT INTO @MyList (
    Iid, MinDate, MaxDate
)
SELECT DISTINCT
    t.id, i.MinDate, i.MaxDate
FROM #target AS t
CROSS APPLY (
    SELECT MIN( [date] ) AS MinDate, MAX( [date] ) AS MaxDate FROM inventory_table WHERE id = t.id 
) AS i;

DECLARE @ListId INT, @iid INT, @min DATETIME, @max DATETIME;

-- Set @ListId to 1 for the first row.
SET @ListId = 1; 

-- While @ListId <= to the number of rows in @MyList, run spReissuingIDs for each Iid --
-- Note: If no rows are present, spReissuingIDs will never be called --
WHILE ( @ListId <= ( SELECT MAX( ListId ) FROM @MyList ) )
BEGIN

    -- Current Row --
    SELECT
        @iid = Iid, @min = MinDate, @max = MaxDate
    FROM @MyList WHERE ListId = @ListId;

    -- Process Row --
    EXEC spReissuingIDs @min, @max, @iid

    -- Next Row --
    SET @ListId = ( @ListId + 1 );

END

您可以在DML语句之后检查系统变量
@@rowcount
。我至少会考虑重新编写存储过程来处理集合,这样您就不必使用光标了。
COUNT(DISTINCT 2)
怎么可能不是
1
?您可以在DML语句之后检查
@@rowcount
系统变量。我至少会考虑重新编写存储过程来处理集合,这样您就不必使用光标了。
COUNT(DISTINCT 2)
怎么可能不是
1