创建存储过程需要很长时间

时间:2018-03-25 12:54:50

标签: sql-server tsql stored-procedures

我有这个存储过程:

Create Procedure spVerhoogPrijzen1
    @artikelcategorie varchar(128),
    @ingangsdatum date
as 
    begin transaction

    update p
    set prijs = prijs * 1.1
    from artikelprijs p 
    join artikel a on a.artikelnr = p.artikelnr
    join artikelcategorie c on c.catcode = a.catcode
    where c.catomschrijving like @artikelcategorie
      and p.begindatum >= @ingangsdatum

    if @@ERROR <> 0
    begin
        rollback
        Raiserror('Je hebt iets fouts ingevuld', 16, 1)
    end
    commit

但是当我运行它时,创建程序需要永远。我在15分钟后停了下来。

我正在使用SQL Server 2014 Management Studio。

Artikel包含:

401 Kaviaar                 lux
402 Ganzenlever             lux
403 Vruchtenyoghurt Aardbei zuv
404 Volle Yoghurt           zuv
405 Magere Kwark            zuv

Artikelcategorie包含:

bio biologische artikelen  
lux luxe artikelen van de traitteur  
zuv zuivelartikelen  

Artikelprijs包含:

401 21.50   2012-01-01  2099-12-31  
402 38.95   2012-01-01  2012-12-31  
402 39.95   2013-01-01  2013-10-31  
402 37.00   2013-11-01  2099-12-31  
403 16.25   2012-01-01  2013-07-31  

2 个答案:

答案 0 :(得分:0)

我认为您不需要单个陈述的交易。

尝试一下:

Create Procedure spVerhoogPrijzen1
    @artikelcategorie varchar(128),
    @ingangsdatum date
as 
    update p
    set prijs = prijs * 1.1
    from artikelprijs p 
    join artikel a on a.artikelnr = p.artikelnr
    join artikelcategorie c on c.catcode = a.catcode
    where c.catomschrijving like @artikelcategorie
      and p.begindatum >= @ingangsdatum

    if @@ERROR <> 0
    begin
        Raiserror('Je hebt iets fouts ingevuld', 16, 1)
    end

答案 1 :(得分:0)

请您尝试以下程序

public class UpdateDB {

    public static void main(String[] args) {

        Connection c = null;

        try {
            Class.forName("org.sqlite.JDBC");
            c = DriverManager.getConnection("jdbc:sqlite:C:/Users/Andrei/test.db");

            String sql = "SELECT * FROM sqlite_master WHERE type='table'";

            Statement stmt  = c.createStatement();
            ResultSet rs    = stmt.executeQuery(sql);

            List<String> tableNames = new ArrayList<>();

            // loop through the result set
            while (rs.next()) {
                tableNames.add(rs.getString(3));
            }

            System.out.println("BEFORE UPDATE");
            System.out.println("---------------------------");

            for (String table: tableNames) {

                String sqlUpdate = "SELECT * FROM " + table + " ";
                ResultSet rs2 = stmt.executeQuery(sqlUpdate);

                while(rs2.next()) {
                    System.out.println(rs2.getInt("ID") + " " + rs2.getString("AGE"));
                }
            }

            // This is the statement which deletes the rows
            for (String table: tableNames) {
                String sqlUpdate = "DELETE FROM " + table + " WHERE AGE < 20";
                stmt.executeUpdate(sqlUpdate);
            }

            System.out.println();
            System.out.println("AFTER UPDATE");
            System.out.println("---------------------------");

            for (String table: tableNames) {

                String sqlUpdate = "SELECT * FROM " + table + " ";
                ResultSet rs2 = stmt.executeQuery(sqlUpdate);

                while(rs2.next()) {
                    System.out.println(rs2.getInt("ID") + " " + rs2.getString("AGE"));
                }
            }

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(UpdateDB.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(UpdateDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

根据你的连接条件,一行受影响哪个catcode为403,值为16.25。更新后,此值已更改为17.88。请检查并谢谢。