資料庫中循環
❶ 資料庫循環語句怎麼寫
你這個好像不需要循環呀:
update A set B = 1 where C BETWEEN 1 AND 400;
這樣一個語句就可以把C為1~400的數據記錄的B欄位設置為1。
❷ 查詢資料庫時,如何循環顯示出所有數據
這個時候得用游標了。比如:
create proc cursorTest @_id int=0, @_name varchar(50)='' as --創建游標 declare @cursor cursor --設定游標欲操作的數據集 set @cursor=cursor for select _id,_name from users --打開游標 open @cursor --移動游標指向到第一條數據,提取第一條數據存放在變數中 fetch next from @cursor into @_id,@_name --如果上一次操作成功則繼續循環 while(@@fetch_status=0)begin --操作提出的數據 print @_name --繼續提下一行 fetch next from @cursor into @_id,@_name end --關閉游標 close @cursor --刪除游標 deallocate @curso
❸ 在sql server中循環語句 for要怎麼使用
sql server里有循環語句,在sqlserver 資料庫中,while循環語句是最常用的語句之一,for指定次數用的很少。比如:
SQL循環語句
declare @i int
set @i=1
while @i<30
begin
insert into test (userid) values(@i)
set @i=@i+1
end
while 條件
begin
執行操作
set @i=@i+1
end
WHILE
設置重復執行 SQL 語句或語句塊的條件,只要指定的條件為真,就重復執行語句,可以使用 BREAK 和 CONTINUE 關鍵字在循環內部控制 WHILE 循環中語句的執行。
語法
WHILE Boolean_expression
{ sql_statement | statement_block }
[ BREAK ]
{ sql_statement | statement_block }
[ CONTINUE ]
參數
Boolean_expression
返回 TRUE 或 FALSE 的表達式。如果布爾表達式中含有 SELECT 語句,必須用圓括弧將 SELECT 語句括起來。
{sql_statement | statement_block}
Transact-SQL 語句或用語句塊定義的語句分組,若要定義語句塊,請使用控制流關鍵字 BEGIN 和 END。
BREAK
❹ 資料庫怎麼循環插入數據
數量多的纖廳昌話用insert
select方法插毀扒入,如果只伏仿是純數字的話,可以用系統表
insert
into
tb
(a,b)
select
a.number,b.number
from
master..spt_values
a,master..spt_values
b
where
a.type='p'
and
b.type='p'
and
a.number
between
1
and
100
and
b.number
between
1
and
5
❺ 資料庫怎麼循環插入數據
已經測試,創建並運行下面的存儲過程可以循環添加數據:
create procere dowhile()
begin
declare i int default 0;
start transaction;
while i<50 do
insert into users(userId,userName,userPwd) values(null,concat('s00',i),123456);
set i=i+1;
end while;
commit;
end;
delimiter;
我設計了一個思想並實現運行成功:
設計思想:
把資料庫表中數據全部查出。每一個數據為一個結點,比如說樓主表的A,B欄位中共有數據a、b、c、d、e,則這5個數據為5個結點。創建一個雙向圖表示節點間的關系。比如說樓主提供的數據的雙向圖為a<--->b<--->c<--->d<--->e,在我的程序中表現為一個結點類余李的集合。求結果的時候只要求從指定節點能夠到達的節點集合就可以了。
求結果時使用遞歸調用,原理是:求結點a能到達的節點=求結點a關聯節點能到達的節點。
程序運行結果:
資料庫數據:
+------+------+
| A | B |
+------+------+
| a | c |
| a | d |
| b | e |
| e | f |
+------+------+
result[a]=[a, c, d]
result[c]=[c, a, d]
result[e]=[e, b, f]
更改資料庫數據,添加a和e的關聯:
mysql> select * from aaa;
+------+------+
| A | B |
+------+------+
| a | c |
| a | d |
| b | e |
| e | f |
| e | a |
+------+------+
5 rows in set (0.00 sec)
result[a]=[a, c, d, e, b, f, a]
從這個結果看出第一個資料庫數據a c d和 b e f是獨立的兩塊,建立了a e的關輪罩聯後,成了一個整體的塊,因此從a節點能到達所有節點。
程序代碼:
本程序是使用的Mysql資料庫,使用時請導入Mysql資料庫驅動。創建TestJdbc.java復制代碼進來直接運行。
如有錯誤或者疑問請聯系QQ232319792或者[email protected]或者直接發網路信息(別用Hi)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class TestJdbc {
public static List<String> result=new ArrayList<String>();
public static void main(String args[]) throws Exception{
select("a");
System.out.println(result);
}
public static void select(String who) throws Exception{
List<Node> nodes=createMap();//獲取節點圖
Node thisNode=null;
for(Node n:nodes)//從節點圖中獲取開始節點
if(n.getName().equals(who))thisNode=n;
digui(thisNode,null);//遞歸調用,假設開始節點的父親是空的。
}
public static void digui(Node star,Node father){
result.add(star.getName());//把本次開始節點加入結果集合
//遍歷開始節點的關聯節點集合
for(Node n:star.getSons()){
if(father!=null && n.equals(father))
//該關聯節點是開始臘毀鬧節點的父親節點,那麼跳過
//當一個關聯節點僅有一個關聯節點即他的父親,那麼遞歸結束
continue;
father=star;//遞歸前設置父親節點為本次開始節點
digui(n,father);//遞歸調用
}
}
public static List<Node> createMap() throws Exception{
List<Node> nodes=new ArrayList<Node>();//創建節點集合保存節電雙向集合
//以下鏈接mysql資料庫並把所有數據查詢出來
String driverName="com.mysql.jdbc.Driver";
Class.forName(driverName);
String url="jdbc:mysql://127.0.0.1:3306/jdbc";
String username="root";
String pwd="root";
Connection con=null;
con=DriverManager.getConnection(url,username,pwd);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from aaa;");
//創建節點引用,保存一條記錄中的兩個欄位值
Node temp1=null;
Node temp2=null;
//遍歷每條記錄
while(rs.next()){
temp1=new Node(rs.getString(1));//創建節點
temp2=new Node(rs.getString(2));
temp1.getSons().add(temp2);//設置節點間關聯
temp2.getSons().add(temp1);//設置節點間關聯
if(nodes.indexOf(temp1)==-1)//如果A欄位節點在節點圖中不存在,則加入到節點圖中
nodes.add(temp1);
else{//如果A欄位節點在節點圖中存在了
//得到已經存在的那個節點,並把B欄位節點關聯給他。
nodes.get(nodes.indexOf(temp1)).getSons().add(temp2);
temp1=nodes.get(nodes.indexOf(temp1));
//B欄位節點關聯的節點是已經存在的了,因此要更改關聯對象。更改成已經存在的節點而不是new出來的。
temp2.getSons().clear();
temp2.getSons().add(nodes.get(nodes.indexOf(temp1)));
}
if(nodes.indexOf(temp2)==-1)
//如果B欄位節點在節點圖中不存在直接加入節點圖
nodes.add(temp2);
else{
//否則,要重新關聯。
nodes.get(nodes.indexOf(temp2)).getSons().add(temp1);
}
}
return nodes;
}
}
//構造節點類
class Node{
private String name;//節點值
private List<Node> sons=new ArrayList<Node>();//節點關聯節點集合
public Node(String name){
this.name=name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Node> getSons() {
return sons;
}
public void setSons(List<Node> sons) {
this.sons = sons;
}
@Override
public String toString() {
return name+" ";
}
@Override
public boolean equals(Object obj) {
return name.equals(((Node)obj).getName());
}
}
❼ 請教大神,oracle資料庫循環語句怎麼寫
你想要的這幾個結果,都可以直接使用SQL語句查出,無需循環。
Oracle循環實在PLSQL塊中編寫:關鍵字for XXX loop 循環體 end loop;
❽ ACCESS資料庫中多表循環查詢
這個沒有必要用VBA,查詢語句就可以實現的