100请教:在数据库A的存储过程中如何判断数据库B里某个表是否存在?

Shiro8558 2004-10-13 06:48:40

if exists (select * from dbo.sysobjects where id = object_id(@database_name+'.[dbo].['+@table_name+']') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
--这样不行,只能识别出@table_name;

exec ('use '+@database_name) --这句有问题
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].['+@table_name+']') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
--这样也不行;

其他判断方法(如:objectproperty())也不行

use datab --datab 是数据库名 这样可以
exec ('use datab ') --这样不行

以上语句有何问题?有没有什么好方法解决如主题的问题?

请各位指教
先谢过了

...全文
167 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
Shiro8558 2004-10-14
  • 打赏
  • 举报
回复
邹老大(学习的对象)已是五星上将了,就不给你分了,^_^ 不介意吧;
再次谢谢各位
zjcxc 2004-10-14
  • 打赏
  • 举报
回复
--如果楼主只是简单的判断一下 @table_name 是否存在,而不保证它是否是表,可以这样做判断

--要判断的表及库名
declare @database_name sysname,@table_name sysname
select @database_name='pubs'
,@table_name='jobs'

--判断处理
select case when object_id(N'['+@database_name+'].dbo.['+@table_name+']') is null then '不存在' else '存在' end
zjcxc 2004-10-14
  • 打赏
  • 举报
回复
--如果要确定是表,则改成这样:

--要判断的表及库名
declare @database_name sysname,@table_name sysname
select @database_name='pubs'
,@table_name='jobs'

--判断处理
declare @s nvarchar(4000),@re bit
set @s='set @re=case when exists(select * from ['+@database_name+'].dbo.sysobjects where name=@table_name and xtype=''U'' and status>=0) then 1 else 0 end'
exec sp_executesql @s,N'@re bit out,@table_name sysname',@re out,@table_name
select case @re when 1 then '存在' else '不存在' end
mgsray 2004-10-14
  • 打赏
  • 举报
回复
--不好意思,刚刚忘了加是否是表的判断
declare @database varchar(10),
@table varchar(20),
@str varchar(8000)
set @database='pubs'
set @table='employee'
set @str='if exists (select 1 from '+@database+
'.dbo.sysobjects where name='''+@table+''' and xtype=''u'') begin select 1 end'
exec (@str)
zjcxc 2004-10-14
  • 打赏
  • 举报
回复
--要判断的表及库名
declare @database_name sysname,@table_name sysname
select @database_name='pubs'
,@table_name='jobs'

--判断处理
declare @s nvarchar(4000),@re bit
set @s='set @re=case when exists(select * from ['+@database_name+'].dbo.sysobjects where name=@table_name) then 1 else 0 end'
exec sp_executesql @s,N'@re bit out,@table_name sysname',@re out,@table_name
select case @re when 1 then '存在' else '不存在' end
mgsray 2004-10-14
  • 打赏
  • 举报
回复
declare @database varchar(10),
@table varchar(20),
@str varchar(8000)
set @database='pubs'
set @table='employee'
set @str='if exists (select 1 from '+@database+
'.dbo.sysobjects where name='''+@table+''') begin select 1 end'
exec (@str)
mgsray 2004-10-14
  • 打赏
  • 举报
回复
如果两个数据库是在一个实例下,那么就直接访问阿
if exists(select 1 from b.dbo.sysobjects where name=表名 and xtype='u')
begin
...
end
如果不在一个实例下,就加链接服务器进行访问
Clove 2004-10-14
  • 打赏
  • 举报
回复
exec ('use datab ')是不对的,exec不能这样用
你的两个数据库是不是在同一台机器上
如果是直接判断该数据表是否存在就好select * from dbo.b.sysobjects where name='B'
如果是两台机器的数据库,判断只能使用事务,这个见过,没做过
Shiro8558 2004-10-14
  • 打赏
  • 举报
回复
这个连接不适合我得问题

我想到一个办法:
在数据库B里写存储过程判断表的存在 再在A中调用B里的存储过程
(如:B.dbo.pro_inB @pram)

请教:
exec ('use datab ') 这样写有什么问题 为什么不执行?
多谢
yb2008 2004-10-13
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/topic/3316/3316824.xml?temp=.2319452
522jack 2004-10-13
  • 打赏
  • 举报
回复
把全部都写出来三。
522jack 2004-10-13
  • 打赏
  • 举报
回复
顶了。。我再。看。
Shiro8558 2004-10-13
  • 打赏
  • 举报
回复
自己顶

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧