批量刪除存儲過程
① 怎麼使用sql語句批量刪除多個表的相同欄位
不知道您用的是什麼資料庫了。
不過做法肯定是一樣,用兩個游標,第一個游標從數據字典中檢索出所有的表,第二個游標從每個表中檢索所有的欄位,然後遇到有要和你刪除名稱相同的欄位的時候,執行alter table tablename drop column columnname的操作。
不過這個執行不能直接這么執行,而是設置一個字元串的變數,例如SQLSERVER,需要@delsql = 'alter table ' + @tablename + ' drop column ' + @columnname
exec(@delsql)
而oracle也一樣delsql = 'alter table ' + tablename + ' drop column ' + columnname;
execute immediate delsql;
要使用這樣的寫法。
不可能使用一個SQL直接刪除所有的。
② 怎麼使用SQL語句批量刪除多個表的相同欄位
不知道您用的是什麼資料庫了。
不過做法肯定是一樣,用兩個游標,第一個游標從數據字典中檢索出所有的表,第二個游標從每個表中檢索所有的欄位,然後遇到有要和你刪除名稱相同的欄位的時候,執行alter
table
tablename
drop
column
columnname的操作。
不過這個執行不能直接這么執行,而是設置一個字元串的變數,例如SQLSERVER,需要@delsql
=
'alter
table
'
+
@tablename
+
'
drop
column
'
+
@columnname
exec(@delsql)
而oracle也一樣delsql
=
'alter
table
'
+
tablename
+
'
drop
column
'
+
columnname;
execute
immediate
delsql;
要使用這樣的寫法。
不可能使用一個SQL直接刪除所有的。
③ Hibernate中如何做批量刪除
批量刪除雖然在hibernate里也可以,但他卻是一個一個刪除,在數量大的情況下很影響效率,昨天在網站上看到了個更好的方法,原來hibernate也提供了JDBC介面,實在是太方便了。
把他cope過來了:
批量更新是指在一個事務中更新大批量數據,批量刪除是指在一個事務中刪除大批量數據。以下程序直接通過Hibernate API批量更新CUSTOMERS表中年齡大於零的所有記錄的AGE欄位:
tx = session.beginTransaction();Iterator customers=session.find("from Customer c where c.age>0").iterator();while(customers.hasNext()){Customer customer=(Customer)customers.next();customer.setAge(customer.getAge()+1);}tx.commit();session.close();
如果CUSTOMERS表中有1萬條年齡大於零的記錄,那麼Session的find()方法會一下子載入1萬個Customer對象到內存。當執行tx.commit()方法時,會清理緩存,Hibernate執行1萬條更新CUSTOMERS表的update語句:
update CUSTOMERS set AGE=? …. where ID=i;update CUSTOMERS set AGE=? …. where ID=j;……update CUSTOMERS set AGE=? …. where ID=k;
以上批量更新方式有兩個缺點:
(1)佔用大量內存,必須把1萬個Customer對象先載入到內存,然後一一更新它們。
(2)執行的update語句的數目太多,每個update語句只能更新一個Customer對象,必須通過1萬條update語句才能更新一萬個Customer對象,頻繁的訪問資料庫,會大大降低應用的性能。
為了迅速釋放1萬個Customer對象佔用的內存,可以在更新每個Customer對象後,就調用Session的evict()方法立即釋放它的內存:
tx = session.beginTransaction();Iterator customers=session.find("from Customer c where c.age>0").iterator();while(customers.hasNext()){Customer customer=(Customer)customers.next();customer.setAge(customer.getAge()+1);session.flush();session.evict(customer);}tx.commit();session.close();
在以上程序中,修改了一個Customer對象的age屬性後,就立即調用Session的flush()方法和evict()方法,flush()方法使Hibernate立刻根據這個Customer對象的狀態變化同步更新資料庫,從而立即執行相關的update語句;evict()方法用於把這個Customer對象從緩存中清除出去,從而及時釋放它佔用的內存。
但evict()方法只能稍微提高批量操作的性能,因為不管有沒有使用evict()方法,Hibernate都必須執行1萬條update語句,才能更新1萬個Customer對象,這是影響批量操作性能的重要因素。假如Hibernate能直接執行如下SQL語句:
update CUSTOMERS set AGE=AGE+1 where AGE>0;
那麼,以上一條update語句就能更新CUSTOMERS表中的1萬條記錄。但是Hibernate並沒有直接提供執行這種update語句的介面。應用程序必須繞過Hibernate API,直接通過JDBC API來執行該SQL語句:
tx = session.beginTransaction();Connection con=session.connection();PreparedStatement stmt=con.prepareStatement("update CUSTOMERS set AGE=AGE+1 "+"where AGE>0 ");stmt.executeUpdate();tx.commit();
以上程序演示了繞過Hibernate API,直接通過JDBC API訪問資料庫的過程。應用程序通過Session的connection()方法獲得該Session使用的資料庫連接,然後通過它創建PreparedStatement對象並執行SQL語句。值得注意的是,應用程序仍然通過Hibernate的Transaction介面來聲明事務邊界。
如果底層資料庫(如Oracle)支持存儲過程,也可以通過存儲過程來執行批量更新。存儲過程直接在資料庫中運行,速度更加快。在Oracle資料庫中可以定義一個名為batchUpdateCustomer()的存儲過程,代碼如下:
create or replace procere batchUpdateCustomer(p_age in number) asbeginupdate CUSTOMERS set AGE=AGE+1 where AGE>p_age;end;
以上存儲過程有一個參數p_age,代表客戶的年齡,應用程序可按照以下方式調用存儲過程:
tx = session.beginTransaction();Connection con=session.connection();String procere = "{call batchUpdateCustomer(?) }";CallableStatement cstmt = con.prepareCall(procere);cstmt.setInt(1,0); //把年齡參數設為0cstmt.executeUpdate();tx.commit();
從上面程序看出,應用程序也必須繞過Hibernate API,直接通過JDBC API來調用存儲過程。
Session的各種重載形式的update()方法都一次只能更新一個對象,而delete()方法的有些重載形式允許以HQL語句作為參數,例如:
session.delete("from Customer c where c.age>0");
如果CUSTOMERS表中有1萬條年齡大於零的記錄,那麼以上代碼能刪除一萬條記錄。但是Session的delete()方法並沒有執行以下delete語句:
delete from CUSTOMERS where AGE>0;
Session的delete()方法先通過以下select語句把1萬個Customer對象載入到內存中:
select * from CUSTOMERS where AGE>0;
接下來執行一萬條delete語句,逐個刪除Customer對象:
delete from CUSTOMERS where ID=i;delete from CUSTOMERS where ID=j;……delete from CUSTOMERS where ID=k;
由此可見,直接通過Hibernate API進行批量更新和批量刪除都不值得推薦。而直接通過JDBC API執行相關的SQL語句或調用相關的存儲過程,是批量更新和批量刪除的最佳方式,這兩種方式都有以下優點:
(1)無需把資料庫中的大批量數據先載入到內存中,然後逐個更新或修改它們,因此不會消耗大量內存。
(2)能在一條SQL語句中更新或刪除大批量的數據。
④ 批量刪除(修改標記) update ERPInfo set IfDel=0 where ID in ("+ID+") 在sql2005中存儲過程怎麼寫。
兩種辦法處理.
1. 使用動態SQL
也就是把你要執行的 SQL 語句, 存儲到一個 varchar 變數裡面。
然後你存儲過程裡面,執行的時候, 動態的拼接一個 SQL 語句,然後執行。
SQL Server 動態SQL 的例子:
http://hi..com/wangqing999/blog/item/6f6da5fee476db869f51465d.html
2.使用 CHARINDEX 替代 IN 的功能
例子:
http://hi..com/wangqing999/blog/item/1b8698fe14d68d8258ee9014.html
⑤ sql能否批量刪除多個行
1、首先我們打開SQL Server 2012資源管理器,在已創建好的資料庫表t_call_info,查詢和比較表記錄。
⑥ 在Hibernate3中怎樣實現多條件批量刪除
Hibernate3中可以直接通過面向對象的形式進行條件刪除,或者是直接sql的形式進行批量刪除。
Sql代碼:
Session session=HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
String strSQL=" delete from Classes as a where a.classno like :name";
Query query = session.createQuery(strSQL);
query.setString("name", "%"+OId+"%");
Session session=HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
String strSQL="from Classes as a where a.classno like :name";
Query query = session.createQuery(strSQL);
HQL代碼:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List result=session.createQuery("delete from Classes as a where a.classno
like " '%"+OId+"%'").list();
[sql] view plainprint?
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List result=session.createQuery("from Classes as a where a.classno like " '%"+OId+"%'").list();
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
String result=session.createQuery("delete from Classes as a where a.classno like " '%"+OId+"%'").list();
⑦ ASP.NET怎麼調用存儲過程實現批量刪除數據
public int RunSql(string procName, SqlParameter[] sp){int rowCount = 0;try{//獲得資料庫連接 conn = getConn(); //打開資料庫連接 conn.Open(); //設置存儲過程 cmd = new SqlCommand(); cmd.CommandType = CommandType.StoredProcere; //設置存儲過程名稱 cmd.CommandText = procName; //設置資料庫連接實例 cmd.Connection = conn; //遍歷存儲過程參數集合
⑧ 求一個oracle的批量刪除的存儲過程
declarecursorc1isselect*fromtable_A;
li_linenonumber(8);
begin
li_lineno:=1;
fortinc1loop
deletefromtable_Awherexbie00='1';
ifmod(li_lineno,1000)=0then
commit;
endif;
li_lineno:=li_lineno+1;
endloop;
end;
/