select * from 库名..表名

解决方案 »

  1.   

    随便找开哪个数据库,要操作另外数据库中的表时,只需在表名前加库名即可select * from 另一数据库..表名
      

  2.   

    2.如何在bbb或ccc中完整地读取aaa的源码并动态创建运行它? /*
    在查询分析器中调用sqldmo生成脚本--函数邹建 2003.07-----------------*//*--调用实例
    print dbo.fgetscript('zj','','','xzkh_sa','syscolumns')/*--得到所有对象的脚本
    declare @name varchar(250)
    declare #aa cursor for
    select name from sysobjects where xtype not in('S','PK','D','X','L')
    open #aa
    fetch next from #aa into @name
    while @@fetch_status=0
    begin
    print dbo.fgetscript('zj','','','xzkh_sa',@name)
    fetch next from #aa into @name
    end
    close #aa
    deallocate #aa
    --*/
    */
    if exists(select 1 from sysobjects where id=object_id('fgetscript') and objectproperty(id,'IsInlineFunction')=0)
    drop function fgetscript
    gocreate function fgetscript(
    @servername varchar(50) --服务器名
    ,@userid varchar(50)='sa' --用户名,如果为nt验证方式,则为空
    ,@password varchar(50)='' --密码
    ,@databasename varchar(50) --数据库名称
    ,@objectname varchar(250) --对象名) returns varchar(8000)
    as
    begin
    declare @re varchar(8000) --返回脚本
    declare @srvid int,@dbsid int --定义服务器、数据库集id
    declare @dbid int,@tbid int --数据库、表id
    declare @err int,@src varchar(255), @desc varchar(255) --错误处理变量--创建sqldmo对象
    exec @err=sp_oacreate 'sqldmo.sqlserver',@srvid output
    if @err<>0 goto lberr--连接服务器
    if isnull(@userid,'')='' --如果是 Nt验证方式
    begin
    exec @err=sp_oasetproperty @srvid,'loginsecure',1
    if @err<>0 goto lberr exec @err=sp_oamethod @srvid,'connect',null,@servername
    end
    else
    exec @err=sp_oamethod @srvid,'connect',null,@servername,@userid,@password if @err<>0 goto lberr--获取数据库集
    exec @err=sp_oagetproperty @srvid,'databases',@dbsid output
    if @err<>0 goto lberr--获取要取得脚本的数据库id
    exec @err=sp_oamethod @dbsid,'item',@dbid output,@databasename
    if @err<>0 goto lberr--获取要取得脚本的对象id
    exec @err=sp_oamethod @dbid,'getobjectbyname',@tbid output,@objectname
    if @err<>0 goto lberr--取得脚本
    exec @err=sp_oamethod @tbid,'script',@re output
    if @err<>0 goto lberr --print @re
    return(@re)lberr:
    exec sp_oageterrorinfo NULL, @src out, @desc out 
    declare @errb varbinary(4)
    set @errb=cast(@err as varbinary(4))
    exec master..xp_varbintohexstr @errb,@re out
    set @re='错误号: '+@re
    +char(13)+'错误源: '+@src
    +char(13)+'错误描述: '+@desc
    return(@re)
    end
    go
      

  3.   

    看来各位误会了我的意思!问题的前提是不可能这样写:数据库名称..表名
    因为这是个动态的一对多的关系,即静态的程序事先并不知道具体是谁调用了它,即不能假设数据库名称。
    因为这个操作表的存储过程存在A数据库中,但运行时,调用者可能是B数据库,也可能是C数据库中的存储过程,并且操作的数据也在调用者所在的数据库,即B或C。
    如果可以这样写select * from 宏变量..表名,那这个问题可能很容易就解决,但SQL SERVER不允许,而且不能老用EXEC('xxx')
      

  4.   

    那调用的时候总该知道是那个数据库调用吧?这样的情况可以用下面的两种方法来解决.1.
    exec()
    既然楼主已经知道,嫌麻烦,就不多说.
    2.
    exec master..xp_cmdshell 'isql /S"服务器名" /U"用户名" /P"密码" /d"数据库名" /Q"要执行的SQL语句"'