MSSQL数据库 跨表 和 跨数据库 查询方法
来源:互联网 发布:firefox js引擎 编辑:程序博客网 时间:2024/06/10 08:48
MSSQL数据库 跨表 和 跨数据库 查询方法
http://blog.csdn.net/highwell1/archive/2009/04/22/4099940.aspx
条件:有数据库 test1、数据库 test2。test1中有表 table1、table2;test2 中有表 table1。三个表的字段为:id、xingming、shijian、shuliang。
一、跨数据库:
(1)原始:
SELECT *
FROM OPENROWSET('sqloledb',
'DRIVER={SQL Server};SERVER=127.0.0.1;UID=sa;PWD=ccds',
test1.dbo.table1) where xingming='a'
UNION all
SELECT *
FROM OPENROWSET('sqloledb',
'DRIVER={SQL Server};SERVER=127.0.0.1;UID=sa;PWD=ccds',
test2.dbo.table1) where xingming='a'
(2)简化:
SELECT * FROM test1.dbo.table1 where xingming='a'
UNION all
SELECT * FROM test2.dbo.table1 where xingming='a'
注意事项:dbo 一定要有,不可以没有
二、跨表:(在数据库test1内)
SELECT * FROM table1 where xingming='a'
UNION all
SELECT * FROM table2 where xingming='a'
这是 UNION ALL 的作用。
如果上面没有看懂,先建好上面的数据库和表,下面有个asp实例,照抄就可以了。
文件名:unionall.asp
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
</head>
<body>
<%sqlStr="provider=sqloledb;data source=127.0.0.1;uid=sa;pwd=;database=test1" '跨库时,数据库名不必指定,如:database=
set conn=server.createObject("adodb.connection")
conn.open sqlStr
set rs=server.createObject("adodb.Recordset")
sql=" SELECT * "
sql=sql&" FROM test1.dbo.table1 where xingming='a' "
sql=sql&" UNION all "
sql=sql&" SELECT * "
sql=sql&" FROM test2.dbo.table1 where xingming='a'"
rs.open sql,conn,1%>
<div align="center">
<table border="1" style="border-collapse: collapse" width="388" bordercolor="#0000FF" id="table1">
<tr>
<td height="28" bgcolor="#CCCCCC" align="center"><b>id</b></td>
<td width="135" height="28" bgcolor="#CCCCCC" align="center"><b>xingming</b></td>
<td width="109" height="28" bgcolor="#CCCCCC" align="center"><b>shijian</b></td>
<td width="89" height="28" bgcolor="#CCCCCC" align="center"><b>shuliang</b></td>
</tr><%if not rs.eof then
do while not rs.eof%>
<tr>
<td height="28" align="center"><%=rs("id")%></td>
<td width="135" height="28" align="center"><%=rs("xingming")%></td>
<td width="109" height="28" align="center"><%=rs("shijian")%></td>
<td width="89" height="28" align="center"><%=rs("shuliang")%></td>
</tr><%rs.movenext
loop
end if
rs.close
set rs=nothing
conn.close
set conn=nothing%>
</table>
</div>
</body>
</html>
再看不懂,买块豆腐自杀撞死。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/highwell1/archive/2009/04/22/4099940.aspx
- MSSQL数据库 跨表 和 跨数据库 查询方法
- MSSQL数据库 跨表 和 跨数据库 查询方法
- MsSql 和 Oracle 跨数据库查询
- MSSQL跨服务器数据库查询
- 揭秘SQL Server查询优化方法-Mssql数据库教程
- MSSQL数据库查询优化(一)
- MSSQL数据库备份恢复方法
- ASP连接MSSQL数据库方法
- mssql 数据库大小统计方法
- MSSQL数据库日志清理方法
- mssql跨数据库操作
- 备份和恢复MSSQL数据库
- MSSQL数据库导出和导入
- MSSQL数据库导出和导入
- SQL 跨数据库查询方法
- [MSSQL]跨数据库访问数据表
- MsSQL跨数据库同步数据
- asp 接数据库方法(oracle&mssql)
- 这个寒假没回家
- myeclipse7.0 安装插件的三种安装方式
- 实现了链表的一些常见操作
- smarty 中foreach 循环次数控制输出
- STUN SERVER
- MSSQL数据库 跨表 和 跨数据库 查询方法
- for与while循环读行及嵌套ssh的区别
- Flex 简单拓扑图
- 点击弹出日历框
- 作息时间表
- MMORPG的困局
- Android中的sp和wp指针 (转)
- ORACLE 中ROWNUM用法总结! (转)
- c语言实现正序链表的创建(不使用头结点)