Ⅰ 如何用一條sql語句得到一個表的所有欄位和記錄條數
說一下幾種思路:
一、如果你的表名是有規律的,就用循環組合成sql,再執行。如:
declare @i int,@sql varchar(1000)
set @i=1
while @i<10
begin
set @sql='select count(*) from table'+convert(varchar,@i)
exec @sql
end
二、把表名從系統表裡取出,來組成sql
select ''select count(*) from '+name from sysobjects where xtype='u'
再把sql取出來執行既可。
三、如果你有興趣研究,還可以使用微軟未公開的存儲過程來處理 sp_MSforeachtable。這個就不詳寫了。
Ⅱ sql語句,sql怎麼循環查詢,把一個list中的所有值當做查詢條件,查詢符合這個list的所有的數據
selectf1fromtable1的結果集做為查詢條件循環查詢。
如:
set@a=selectf1fromtable1
foreach(@a)
{
select*fromtable2
wheref2=@a
}
Ⅲ JDBC中的SQL查詢語句求助
selcet FName,LName
from ATHLETE,PARTICIPATION_IND
where (ATHLETE.athleteID)=(PARTICIPATION_IND.athleteID)and
Ⅳ jdbc實現sql語句
用這個類吧.好的話,給我加加分.
import java.sql.*;
/**
* @功能: 一個JDBC的本地化API連接類,封裝了數據操作方法,只用傳一個SQL語句即可
* @作者: 李開歡
* @日期: 2007/
*/
public class ConnectionDemo {
/*
* 這里可以將常量全部放入另一個類中,以方便修改
*/
private static Connection conn;
private static Statement ps;
private static ResultSet rs;
private static final String DRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private static final String URL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
private static final String USER ="sa";
private static final String PASS = "sa";
public ConnectionDemo() {
// TODO Auto-generated constructor stub
ConnectionDemo.getConnection();
}
public static Connection getConnection(){
System.out.println("連接中...");
try {
Class.forName(ConnectionDemo.DRIVER);
conn = DriverManager.getConnection(ConnectionDemo.URL, ConnectionDemo.USER, ConnectionDemo.PASS);
System.out.println("成功連接");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static Statement getStatement(String sql){
System.out.println("執行SQL語句中...");
try {
ps = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
if(sql.substring(0, 6).equals("select")){
rs = ps.executeQuery(sql);
System.out.println("執行完查詢操作,結果已返回ResultSet集合");
}else if(sql.substring(0, 6).equals("delete")){
ps.executeUpdate(sql);
System.out.println("已執行完畢刪除操作");
}else if(sql.substring(0, 6).equals("insert")){
ps.executeUpdate(sql);
System.out.println("已執行完畢增加操作");
}else{
ps.executeUpdate(sql);
System.out.println("已執行完畢更新操作");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ps;
}
public static ResultSet getResultSet(){
System.out.println("查詢結果為:");
return rs;
}
public static void closeConnection(){
System.out.println("關閉連接中...");
try {
if (rs != null) {
rs.close();
System.out.println("已關閉ResultSet");
}
if (ps != null) {
ps.close();
System.out.println("已關閉Statement");
}
if (conn != null) {
conn.close();
System.out.println("已關閉Connection");
}
} catch (Exception e) {
// TODO: handle exception
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ConnectionDemo.getConnection();
String sql = "delete from type where id = 1";
ConnectionDemo.getStatement(sql);
String sql2 = "insert into type values(1,'教學設備')";
ConnectionDemo.getStatement(sql2);
String sql1 = "select * from type";
ConnectionDemo.getStatement(sql1);
ResultSet rs = ConnectionDemo.getResultSet();
System.out.println("編號 "+"類 型");
try {
while(rs.next()){
System.out.print(" "+rs.getInt(1)+" ");
System.out.println(rs.getString(2));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ConnectionDemo.closeConnection();
}
}
Ⅳ SQL語句怎麼循環取出一個表裡面的所有欄位,用逗號間隔
declare @str varchar(8000)
set @str=''
select @str=@str+','+Name from syscolumns where id=object_id('ERPWorkToDo')
set @str=stuff(@str,1,1,'')
select @str
--'ERPWorkToDo是表名
Ⅵ 怎樣用SQL查詢一個表的所有欄位
可以用一句sql語句查詢解決,如要查test表中的所有欄位及類型
Selectb.nameasTableName,C.nameASTYPEfromsyscolumnsa,sysobjectsb,systypesc
wherea.id=b.id
andb.type='U'
anda.xtype=c.xtype
andb.name='TEST';
結果截圖: