简体   繁体   English

DTS可以测试MS-Access表是否存在

[英]Can DTS Test for Presence of MS-Access Table

I have an Access database in which I drop the table and then create the table afresh. 我有一个Access数据库,在其中删除表,然后重新创建表。 However, I need to be able to test for the table in case the table gets dropped but not created (ie when someone stops the DTS package just after it starts -roll-eyes- ). 但是,我需要能够测试该表,以防该表被删除但未被创建(即当有人在DTS包启动后立即停止它-roll-eyes-)。 If I were doing this in the SQL database I would just do: 如果在SQL数据库中执行此操作,我将执行以下操作:

IF (EXISTS (SELECT * FROM sysobjects WHERE name = 'Table-Name-to-look-for'))
BEGIN
drop table 'Table-Name-to-look-for'
END

But how do I do that for an Access database? 但是,如何为Access数据库执行此操作?

Optional answer: is there a way to have the DTS package ignore the error and just go to the next step rather than checking to see if it exists? 可选答案:有没有一种方法可以让DTS包忽略该错误,而直接转到下一步而不是检查它是否存在?

SQL Server 2000 SQL Server 2000

I'm not sure whether you can query the system objects table in an Access database from a DTS package. 我不确定是否可以从DTS包中查询Access数据库中的系统对象表。

If that doesn't work, why not just try doing a SELECT * from the Access table in question and then catch the error if it fails? 如果这不起作用 ,为什么不尝试从有问题的Access表中执行SELECT *,然后失败时捕获错误呢?

Microsoft Access has a system table called MSysObjects that contains a list of all database objects, including tables. Microsoft Access有一个称为MSysObjects的系统表,其中包含所有数据库对象(包括表)的列表。 Table objects have Type 1, 4 and 6. 表对象具有类型1、4和6。

It is important to reference the type: 引用类型很重要:

... Where Name='TableName' And Type In (1,4,6) ...其中Name ='TableName'并输入(1,4,6)

Otherwise, what is returned could be a some object other than a table. 否则,返回的内容可能是表以外的某个对象。

Try the same T-SQL, but in MS ACCESS the sys objects table is called: MSysObjects. 尝试使用相同的T-SQL,但是在MS ACCESS中,sys对象表称为:MSysObjects。

Try this: 尝试这个:

SELECT * FROM MSysObjects WHERE Name = 'your_table';

and see if it works from there. 看看它是否可以从那里工作。

You can take a look at these tables if you go to Tools -> Options -> View (a tab) -> and check Hidden Objects, System Objects. 如果转到工具->选项->视图(一个选项卡)->并选中“隐藏对象”,“系统对象”,则可以查看这些表。 So you can see both. 这样您就可以看到两者。 If you open the table, you should see your table names, queries, etc. Do not change this manually or the DB could panic :) 如果打开表,则应该看到表名,查询等。请勿手动更改此设置,否则数据库可能会出现恐慌:)

Martin. 马丁

PD: Your If Exists should also check of object type: PD:如果存在,还应检查对象类型:

IF EXISTS (SELECT * FROM sysobjects WHERE id = object_id(N'[dbo].[Your_Table_Name]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM