Tsql 如果条件为真,是否有其他方法来比较两个不同表中的行并更新一个表,而不是使用嵌套游标?

Tsql 如果条件为真,是否有其他方法来比较两个不同表中的行并更新一个表,而不是使用嵌套游标?,tsql,Tsql,我使用嵌套游标比较两个表中的行 如果If语句为true,那么我将更新tbl_1中的一列 问题是,我在每个tbl_1和tbl_2中都有100000行,性能非常糟糕 有没有其他方法可以在不使用游标的情况下完成此任务 我使用嵌套游标比较两个表中的行。当我测试表中的一个小数据集时,我的查询为我提供了正确的信息,但当我测试完整的数据集时,查询将持续3小时 create table tbl_1 ( start_date varchar(8), begintime var

我使用嵌套游标比较两个表中的行

如果If语句为true,那么我将更新tbl_1中的一列

问题是,我在每个tbl_1和tbl_2中都有100000行,性能非常糟糕

有没有其他方法可以在不使用游标的情况下完成此任务

我使用嵌套游标比较两个表中的行。当我测试表中的一个小数据集时,我的查询为我提供了正确的信息,但当我测试完整的数据集时,查询将持续3小时

create table tbl_1 (
 start_date         varchar(8),
 begintime          varchar(4), 
 endtime            varchar(4), 
 duration           varchar(4), 
 interval           varchar(4), 
 color          varchar(10),    
 description        varchar(75),
 )


insert into tbl_1 values('20190102',    '0830', '1200', '210',  '15',   'green',    'Dorothy')
insert into tbl_1 values('20190102',    '1300', '1730', '270',  '15',   'green',    'Dorothy')
insert into tbl_1 values('20190103',    '0830', '1200', '210',  '15',   'green',    'Dorothy')
insert into tbl_1 values('20190103',    '1300', '1730', '270',  '15',   'green',    'Dorothy')
insert into tbl_1 values('20190104',    '0830', '1200', '210',  '15',   'green',    'Dorothy')
insert into tbl_1 values('20190104',    '1300', '1730', '270',  '15',   'green',    'Dorothy')



 create table tbl_2 (
 appt_date          varchar(8),
 begintime          varchar(4), 
 endtime            varchar(4), 
 duration           varchar(4), 
 description            varchar(75),
 )

insert into tbl_2 values('20190102',    '0830', '0900', '30',   'Dorothy')
insert into tbl_2 values('20190102',    '0930', '0945', '15',   'Dorothy')
insert into tbl_2 values('20190102',    '0945', '1000', '15',   'Dorothy')
insert into tbl_2 values('20190102',    '1300', '1315', '15',   'Dorothy')
insert into tbl_2 values('20190102',    '1300', '1330', '30',   'Dorothy')
insert into tbl_2 values('20190107',    '1300', '1330', '30',   'Dorothy')


DECLARE @appt_date          VARCHAR(8)
DECLARE @begintime          VARCHAR(4)
DECLARE @endtime            VARCHAR(4)
DECLARE @duration           int
DECLARE @description        VARCHAR(75)

DECLARE @start_date2            VARCHAR(8)
DECLARE @begintime2             VARCHAR(4)
DECLARE @endtime2               VARCHAR(4)
DECLARE @duration2              int
DECLARE @interval2              int 
DECLARE @color2                 VARCHAR(10)
DECLARE @description2           VARCHAR(75)

DECLARE appt_cursor CURSOR FOR

    select 
    appt_date, begintime, endtime, duration, description
    from tbl_2
    order by appt_date, begintime 

OPEN appt_cursor;
 FETCH NEXT FROM appt_cursor INTO @appt_date, @begintime, @endtime, @duration, @description

 WHILE @@FETCH_STATUS = 0
 BEGIN
 DECLARE slot_cursor CURSOR FOR

select  start_date,begintime, endtime, duration, interval, color, description
from tbl_1
order by description, start_date, begintime

 OPEN slot_cursor;
 FETCH NEXT FROM slot_cursor INTO @start_date2, @begintime2, @endtime2, @duration2, @interval2, @color2, @description2;

 WHILE @@FETCH_STATUS = 0
 BEGIN


IF @appt_date = @start_date2 and @begintime >= @begintime2 and @begintime <= @endtime2 and @endtime >= @begintime and @endtime <= @endtime2 and @description = @description2

    update tbl_1
    set duration = duration-@duration
    where @appt_date = start_date and @begintime >= begintime and @begintime <= endtime and @endtime >= begintime and @endtime <= endtime and @description = description

  FETCH NEXT FROM slot_cursor INTO @start_date2, @begintime2, @endtime2, @duration2, @interval2, @color2, @description2;
 END;
 CLOSE slot_cursor;
 DEALLOCATE slot_cursor;


 FETCH NEXT FROM appt_cursor INTO @appt_date, @begintime, @endtime, @duration, @description;
END
CLOSE appt_cursor;
DEALLOCATE appt_cursor;

有人能帮你创建一个合并语句吗?

哇,嵌套的光标…读一下你的问题。你认为我们能如何帮助你???我们甚至不知道你想要什么。你所要做的就是发布一些示例输入和长时间运行的查询;tr,但是出于直觉,我建议读一读关于或声明。我很确定,你不需要光标。你试过相关子查询吗?他们非常快。如果你不熟悉他们,看看这个,看看你是怎么想的。您已经将示例数据发布为DDL+DML,这很好,但是您的问题缺少对您想要实现的内容的描述。非常确定嵌套游标不是最佳解决方案(很少有游标是最佳解决方案,更不用说嵌套游标了)。如果你能为你的问题提供一个解释,这将给你一个更好的机会得到正确的答案。哇,嵌套光标…阅读你的问题。你认为我们能如何帮助你???我们甚至不知道你想要什么。你所要做的就是发布一些示例输入和长时间运行的查询;tr,但是出于直觉,我建议读一读关于或声明。我很确定,你不需要光标。你试过相关子查询吗?他们非常快。如果你不熟悉他们,看看这个,看看你是怎么想的。您已经将示例数据发布为DDL+DML,这很好,但是您的问题缺少对您想要实现的内容的描述。非常确定嵌套游标不是最佳解决方案(很少有游标是最佳解决方案,更不用说嵌套游标了)。如果你能为你的问题提供一个解释,这将给你一个更好的机会得到正确的答案。
start_date  begintime   endtime duration    interval    color   description
 20190102   0830        1200    150         15          green   Dorothy
 20190102   1300        1730    225         15          green   Dorothy
 20190103   0830        1200    210         15          green   Dorothy
 20190103   1300        1730    270         15          green   Dorothy
 20190104   0830        1200    210         15          green   Dorothy
 20190104   1300        1730    270         15          green   Dorothy