日期:2014-05-16  浏览次数:20845 次

关于跨库查询的sql语句?同一台服务器,不同数据库?

<!--#include file="conn.asp"-->
if danhao="" then
response.write "请输入要查找的货号。"
else
fgsbm = left(danhao,4)
huohao = mid(danhao,5,9)
jshj = mid(danhao,16,2)
set rs=server.createobject("adodb.recordset")
sql="select jydbh,fhrxm,shrxm,shrdh,jydzt,Name from jydxx,Bm_jydzt where jydzt=id and sxhh ='"&huohao&"'"
rs.open sql,conn,1,1
if rs.eof then
Response.Write "没有搜索到内容。"
else
do while not rs.eof

单号的格式为1001-12345-123
前四位为公司编码:固定的  ;
中间为货号 不固定 有的3位,有的4位 ,有的5位不等;
最后组为件数 :不固定  ;
现在要将其拆分为三组数字 去查询不同的数据库?
fgsbm = left(danhao,4)
huohao = mid(danhao,5,9)  ??
jshj = mid(danhao,16,2)   ??
然后根据fgsbm 去连接不同的数据库查询  这个sql语句要怎么写?
------解决方案--------------------
danhao="1001-12345-123"
set rx=new RegExp
rx.Global=true
rx.Pattern="^\d{4}-\d{3,}-\d{1,}$"'第二组数字最少3位,如果有他限制自己修改,低三组数字最少1位
if not rx.Test(danhao) then
  response.write "单号错误"
  response.end
end if
arr=split(danhao,"-")
set rs=server.createobject("adodb.recordset")
if arr(0)="xxxx" then
sql="select jydbh,fhrxm,shrxm,shrdh,jydzt,Name from jydxx,Bm_jydzt where jydzt=id and sxhh ='"&huohao&"'"
elseif arr(0)="xxxxxxxxxxx"
sql="xxxxxxxxxxxx"
else'默认查询的数据库
sql=""
end if
rs.open sql,conn,1,1
if rs.eof then
Response.Write "没有搜索到内容。"
else
do while not rs.eof

------解决方案--------------------
。。。你不就关闭下原来的链接,重新open下链接到不同的数据库就好了。。
conn.close
set rs=server.createobject("adodb.recordset")
if arr(0)="1001" Then
conn.open "Provider=SQLOledb;data source=.;uid=sa;pwd=aaaa;DATABASE=xxxxxxxxx"
sql="select jydbh,fhrxm,shrxm,shrdh,jydzt,Name from jydxx,Bm_jydzt where jydzt=id and (fgsbm='111' ) and sxhh ='"&arr(1)&"' and jshj="&arr(2)&""
elseif arr(0) ="1002" Then
conn.open "Provider=SQLOledb;data source=.;uid=sa;pwd=aaaa;DATABASE=xxxxxxxxxxxxxxxxxxxxxxxxx"
sql="select jydbh,fhrxm,shrxm,shrdh,jydzt,Name from jydxx,Bm_jydzt where jydzt=id and (fgsbm='112' ) and sxhh ='"&arr(1)&"' and jshj="&arr(2)&""
end if