English 中文(简体)
如何检查某一指数是否在表格中存在?
原标题:How do you check if a certain index exists in a table?
  • 时间:2010-04-22 09:55:15
  •  标签:
  • sql-server

与此类似:

SELECT
* 
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS 
WHERE CONSTRAINT_NAME = FK_TreeNodesBinaryAssets_BinaryAssets 
and TABLE_NAME =  TreeNodesBinaryAssets 

但指数。

最佳回答

你们可以采用简单明了的办法,如:

SELECT * 
FROM sys.indexes 
WHERE name= YourIndexName  AND object_id = OBJECT_ID( Schema.YourTableName )
问题回答

rel=“noreferer”INDEXPROPERTY/code 内部职能:

INDEXPROPERTY ( object_ID , index_or_statistics_name , property )  

最简单的使用是<代码>。 IndexID property:

If IndexProperty(Object_Id( MyTable ),  MyIndex ,  IndexID ) Is Null

如果该指数存在,则上述指数将退还该指数;如果该指数为t,则该指数将退回NUL

AdaTheDEV,我使用了你的辛迪加,并造成以下原因。

问题: 由于缺少指数,这一过程每半年进行一次。

教养: 检查指数和编制数据的程序 调查表和删除指数的程序结束时,采用萨米语法,因为不需要,而是每季度。 展示 这里只有 drop子

-- drop the index 
begin

  IF EXISTS (SELECT *  FROM sys.indexes  WHERE name= Index_Name  
    AND object_id = OBJECT_ID( [SchmaName].[TableName] ))
  begin
    DROP INDEX [Index_Name] ON [SchmaName].[TableName];
  end

end

您提问的隐蔽目的是DROP 在将上填入一个大的表格之前,该指数是有用的一行:

DROP INDEX IF EXISTS [IndexName] ON [dbo].[TableName]

该星号自2016年8月5日以来一直可供使用。 文件<代码> EXISTS:

https://blogs.msdn.microsoft.com/sqlserverstorageengine/2015/11/03/drop-if-exists-new-thing-in-ql-server-2016/

如果你处理一件主要事,则使用:

ALTER TABLE [TableName] DROP CONSTRAINT IF EXISTS [PK_name] 

转而履行以下职能,使我能够迅速检查是否存在指数;只像项目目标。

CREATE FUNCTION INDEX_OBJECT_ID (
    @tableName VARCHAR(128),
    @indexName VARCHAR(128)
    )
RETURNS INT
AS
BEGIN
    DECLARE @objectId INT

    SELECT @objectId = i.object_id
    FROM sys.indexes i
    WHERE i.object_id = OBJECT_ID(@tableName)
    AND i.name = @indexName

    RETURN @objectId
END
GO

EDIT:这只是回到表中的分目标,但如果指数确实存在的话,它将是NUL。 我恳请你将这一指数确定下来,以换取回报指数,但这是多余的。

-- Delete index if exists
IF EXISTS(SELECT TOP 1 1 FROM sys.indexes indexes INNER JOIN sys.objects 
objects ON indexes.object_id = objects.object_id WHERE indexes.name 
= Your_Index_Name  AND objects.name =  Your_Table_Name )
BEGIN
    PRINT  DROP INDEX [Your_Index_Name] ON [dbo].[Your_Table_Name] 
    DROP INDEX [our_Index_Name] ON [dbo].[Your_Table_Name]
END
GO
EXEC sp_helpindex  [[[SCHEMA-NAME.TABLE-NAME]]] 
GO

a. 特定表格中是否存在分类指数:

SELECT * FROM SYS.indexes 
WHERE index_id = 1 AND name IN (SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME =  Table_Name )




相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

SQL server: Can NT accounts be mapped to SQL server accounts

In our database we have an SQL server account that has the correct roles to access some of the databases. We are now switching to windows authentication and I was wondering if we can create a NT user ...

SQL Server 2000, ADO 2.8, VB6

How to determine if a Transaction is active i.e. before issuing Begin Transaction I want to ensure that no previous transaction are open.. the platform is VB6, MS-SQL Server 2000 and ADO 2.8

热门标签