Sql server 如何验证插入&;删除&;更新在Ms Sql server的单个存储过程中完成

Sql server 如何验证插入&;删除&;更新在Ms Sql server的单个存储过程中完成,sql-server,Sql Server,我正在尝试编写存储过程测试用例以进行功能检查。我需要检查更新操作是否完成了插入和/或删除操作。我尝试使用二进制校验和(*)进行更新,但如何通过结果集通过更新操作插入或删除多少行的表 我正在使用MS SQL Server 2005 谢谢。如果很快: 仅删除-删除 仅插入-插入 插入和删除-更新 作为一个选项,您可以尝试使用触发器查找操作类型: ALTER TRIGGER [dbo].[MyTrigger] ON [dbo].[MyTable] AFTER INSERT, UPDA

我正在尝试编写存储过程测试用例以进行功能检查。我需要检查更新操作是否完成了插入和/或删除操作。我尝试使用二进制校验和(*)进行更新,但如何通过结果集通过更新操作插入或删除多少行的表

我正在使用MS SQL Server 2005

谢谢。

如果很快:

  • 仅删除-删除
  • 仅插入-插入
  • 插入和删除-更新
作为一个选项,您可以尝试使用触发器查找操作类型:

ALTER TRIGGER [dbo].[MyTrigger]
   ON  [dbo].[MyTable]
   AFTER INSERT, UPDATE, DELETE
AS 
BEGIN
    SET NOCOUNT ON;

    declare
        @i int = (select count(*) from (select top (1) * from inserted) as i),
        @d int = (select count(*) from (select top (1) * from deleted) as d),
        @type varchar(20);

    if @i = 1 and @d = 1 set @type = 'update'
        else if @i = 1 and @d = 0 set @type = 'insert'
            else if @i = 0 and @d = 1 set @type = 'delete'
                else set @type = 'empty'; 


    if @type = 'insert'
    begin
        -- YOUR CODE HERE
    end

    if @type = 'delete'
    begin
        -- YOUR CODE HERE
    end

    if @type = 'update'
    begin
        -- YOUR CODE HERE
    end

    if @type = 'empty'
    begin
        -- YOUR CODE HERE
    end


END
如果很快:

  • 仅删除-删除
  • 仅插入-插入
  • 插入和删除-更新
作为一个选项,您可以尝试使用触发器查找操作类型:

ALTER TRIGGER [dbo].[MyTrigger]
   ON  [dbo].[MyTable]
   AFTER INSERT, UPDATE, DELETE
AS 
BEGIN
    SET NOCOUNT ON;

    declare
        @i int = (select count(*) from (select top (1) * from inserted) as i),
        @d int = (select count(*) from (select top (1) * from deleted) as d),
        @type varchar(20);

    if @i = 1 and @d = 1 set @type = 'update'
        else if @i = 1 and @d = 0 set @type = 'insert'
            else if @i = 0 and @d = 1 set @type = 'delete'
                else set @type = 'empty'; 


    if @type = 'insert'
    begin
        -- YOUR CODE HERE
    end

    if @type = 'delete'
    begin
        -- YOUR CODE HERE
    end

    if @type = 'update'
    begin
        -- YOUR CODE HERE
    end

    if @type = 'empty'
    begin
        -- YOUR CODE HERE
    end


END
引述:


我接下来要做的是将数据插入temp。表格和实际表格 正在对其中一个执行操作,并且。。。检查功能 这是程序的一部分

如果您的最后一个问题可以描述为比较两个表中的实际数据和预期数据,那么您可以尝试下一种方法-使用
except
子句进行比较:

create table [tb_source] (id int primary key identity(1,1), value varchar(50));
create table [tb_dest] (id int primary key, value varchar(50));


insert into [tb_source] (value) values ('a');
insert into [tb_source] (value) values ('b');
insert into [tb_source] (value) values ('c');

-- tables are equal
insert into [tb_dest] select * from [tb_source];

-- one row inserted
insert [tb_dest] (id, value) values (4, 'd');

-- one row deleted
delete from [tb_dest] where id = 1;

-- one row updated
update [tb_dest] set value = 'b_' where id = 2;

 -- answering the question "how many number of rows with update operation through result"
with [raw] as (
    (select *, 'deleted' [operation] from [tb_source]
    except 
    select *, 'deleted' from [tb_dest])
    union all
    (select *, 'inserted' from [tb_dest]
    except 
    select *, 'inserted' from [tb_source])),

[updates] as (
    select id
    from [raw]
    group by id
    having count(*) > 1),

[results] as (
    select * from [raw] where id not in (select id from [updates])
    union all
    select id, value, 'updated' from [raw] where id in (select id from [updates]))

select [operation], count(distinct id)
from [results]
group by [operation]
order by count(*) desc
结果:

operation   count
---------------------------------
updated         1
deleted         1
inserted        1
引述:


我接下来要做的是将数据插入temp。表格和实际表格 正在对其中一个执行操作,并且。。。检查功能 这是程序的一部分

如果您的最后一个问题可以描述为比较两个表中的实际数据和预期数据,那么您可以尝试下一种方法-使用
except
子句进行比较:

create table [tb_source] (id int primary key identity(1,1), value varchar(50));
create table [tb_dest] (id int primary key, value varchar(50));


insert into [tb_source] (value) values ('a');
insert into [tb_source] (value) values ('b');
insert into [tb_source] (value) values ('c');

-- tables are equal
insert into [tb_dest] select * from [tb_source];

-- one row inserted
insert [tb_dest] (id, value) values (4, 'd');

-- one row deleted
delete from [tb_dest] where id = 1;

-- one row updated
update [tb_dest] set value = 'b_' where id = 2;

 -- answering the question "how many number of rows with update operation through result"
with [raw] as (
    (select *, 'deleted' [operation] from [tb_source]
    except 
    select *, 'deleted' from [tb_dest])
    union all
    (select *, 'inserted' from [tb_dest]
    except 
    select *, 'inserted' from [tb_source])),

[updates] as (
    select id
    from [raw]
    group by id
    having count(*) > 1),

[results] as (
    select * from [raw] where id not in (select id from [updates])
    union all
    select id, value, 'updated' from [raw] where id in (select id from [updates]))

select [operation], count(distinct id)
from [results]
group by [operation]
order by count(*) desc
结果:

operation   count
---------------------------------
updated         1
deleted         1
inserted        1

您是使用.Net还是Java代码来检查存储过程的结果?嗯,您可以使用触发器,但是如果要集成到自动化测试中,这可能会变得混乱和复杂。除此之外,您可能应该先复制表格,然后再将其与之后的副本进行比较。如果需要为许多表做这件事,那么考虑将整个数据库用备份复制到另一个数据库。是否使用.NET或java代码检查存储过程的结果?那么,您可以使用触发器,但是这可能会变得混乱和复杂,从而集成到自动化测试中。除此之外,您可能应该先复制表格,然后再将其与之后的副本进行比较。如果需要对许多表进行此操作,那么考虑将整个数据库用备份复制到另一个数据库。亲爱的先生,我需要通过结果检查来检查功能。在这种情况下,什么可以比触发器更好(更重要):您可以将触发器的操作信息插入到中间表(或通过CONTERXY INFO())。并立即在测试用例过程中分析结果。我接下来要做的是将数据插入temp。table&actual table然后对其中一个表执行操作,然后通过sum()或BINARY_CHECKSUM()对两个表执行适当的联接操作,检查过程的功能。我使用此工具进行单元测试和表比较,也使用其他存储库进行共享,我服务的公司为我们提供了在存储过程中定义的明确框架,我负责根据这些框架传达解决方案。因此,我是在寻求一些内部逻辑方面的帮助,而不是任何其他工具。亲爱的先生,我需要通过结果检查来检查功能。在这种情况下,有什么比触发器更好(更为最终):您可以将操作信息从触发器插入到中间表(或通过上下文_info())并立即在测试用例过程中分析结果。我接下来要做的是将数据插入temp。table&actual table然后对其中一个表执行操作,然后通过sum()或BINARY_CHECKSUM()对两个表执行适当的联接操作,检查过程的功能。我使用此工具进行单元测试和表比较,也使用其他存储库进行共享,我服务的公司为我们提供了在存储过程中定义的明确框架,我负责根据这些框架传达解决方案。所以我是在寻求一些内部逻辑方面的帮助,而不是任何额外的工具。@Andrey亲爱的,先生。提出的方法比我使用的方法更能解决问题,但由于我正在休假并将问题移交给其他人,我将在加入工作场所后尽快回复。亲爱的先生,我与我的上级讨论过。甚至他也同意我的意见。谢谢你的努力。@Andrey亲爱的,先生。提出的方法比我使用的方法更能解决问题,但由于我正在休假并将问题移交给其他人,我将在加入工作场所后尽快回复。亲爱的先生,我与我的上级讨论过。甚至他也同意我的意见。谢谢你的努力。