Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Delete

删除数据

export
class

Delete

Hierarchy

  • Delete

Index

Methods

Static delete

  • delete(conn: ConnectionPool, pars: object, tran?: MssqlTransaction): Promise<void>
  • 根据主键删除一条数据。 注意:此方法没有开启事务。如需开启事务,见 Transaction 注意:条件会根据pars.data参数与table表中主键字段进行匹配,只有实际存在的主键才起作用。见下面例子。

    static
    memberof

    Delete

    example

    tbl1表结构:
    create table tbl1 (
     f1 int,
     f2 int,
     f3 int,
     primary key(f1,f2)
    )
    where 条件会根据表中主键字段进行过滤
    例1,以下相当于SQL: delete from tbl1 where f1=1 and f2=2
    await Delete.delete(conn, {
      data: { f1: 1, f2: 2 },
      table: 'tbl1'
    });
    例2,以下相当于SQL: delete from tbl1 where f1=1 and f2=2
    await Delete.delete(conn, {
      data: { f1: 1, f2: 2, f3: 3, f4: 4 }, // f3,f4不是主键字段,不起作用
      table: 'tbl1'
    });
    例3,会报错,必须提供主键字段f1与f2
    await Delete.delete(conn, {
      data: { f5: 5 }, // f5不是字段,不起作用
      table: 'tbl1'
    });

    Parameters

    • conn: ConnectionPool

      数据库连接

    • pars: object
      • Optional chema?: string
      • data: __type
      • Optional database?: string
      • table: string
    • Optional tran: MssqlTransaction

    Returns Promise<void>

    Promise 对象

Static deleteByWhere

  • deleteByWhere(conn: ConnectionPool, pars: object, tran?: MssqlTransaction): Promise<void>
  • 根据条件删除一条数据。 注意:如需事务处理,请传入tran参数。 注意:条件会根据pars.where参数与table表中实际字段进行匹配,只有实际存在的字段才起作用。见下面例子。

    static
    memberof

    Delete

    example
    tbl1表结构:
    create table tbl1 (
     f1 int,
     f2 int,
     f3 int
    )
    where 条件会根据表中字段进行过滤
    例1,以下相当于SQL: delete from tbl1 where f1=1 and f2=2
    await Delete.delete(conn, {
      where: { f1: 1, f2: 2 },
      table: 'tbl1'
    });
    例2,以下相当于SQL: delete from tbl1 where f1=1 and f2=2 and f3=3
    await Delete.delete(conn, {
      where: { f1: 1, f2: 2, f3: 3, f4: 4 }, // f4不是字段,不起作用
      table: 'tbl1'
    });
    例3,以下相当于SQL: delete from tbl1
    await Delete.delete(conn, {
      where: { f5: 5 }, // f5不是字段,不起作用
      table: 'tbl1'
    });
    例4,以下相当于SQL: delete from tbl1
    await Delete.delete(conn, {
      where: { },
      table: 'tbl1'
    });
    

    Parameters

    • conn: ConnectionPool

      数据库连接

    • pars: object
      • Optional chema?: string
      • Optional database?: string
      • table: string
      • Optional where?: __type
    • Optional tran: MssqlTransaction

    Returns Promise<void>

    Promise 对象