在Visual Foxpro 9中测试主键是否存在

时间:2013-02-18 19:48:50

标签: visual-foxpro

在我的代码中,我有一个例程在启动时进行一些数据库维护,我使用以下代码从表中删除主键。

USE Students EXCLUSIVE
ALTER TABLE Students DROP PRIMARY KEY

如果只运行一次并且关键在那里,那么生命是美好的。但是,如果它已经被删除,它会生成我测试的错误代码1879,只是做一个RETURN并且工作正常。

但是,我希望能够在发出ALTER TABLE命令之前测试密钥是否存在。

我搜索了帮助文件和MSDN无济于事,我无法想象没有代码可以检查是否存在主键,但我肯定找不到它。

由于

2 个答案:

答案 0 :(得分:1)

您可以使用ATAGINFO()TAG()PRIMARY()函数来确定您的主键索引。来自Primary()的帮助:

CLOSE DATABASES
SET PATH TO (HOME(2) + 'Data\')   && Sets path to database
OPEN DATABASE testdata  && Open testdata database
USE Customer     && Open customer table

FOR nCount = 1 TO 254
   IF !EMPTY(TAG(nCount))  && Checks for tags in the index
   ? TAG(nCount)  && Display tag name
   ? PRIMARY(nCount)     && Display primary status
   ELSE
      EXIT  && Exit the loop when no more tags are found
   ENDIF
ENDFOR

HTH

答案 1 :(得分:0)

您可以先检查它的存在:

If !Empty(  DBGetProp("Students","TABLE","PrimaryKey") )
  Alter table students drop primary key
endif