1

列名をインデックスの列名と一致させる方法はありますか。

たとえば、index1 が列 name,age,address を使用し、index2 が (同じテーブルで) name,age を使用するとします。(すべて 1 つの特定のテーブルに対して)

名前、年齢、およびテーブル名をストアド プロシージャに渡す場合、index1 ではなく index2 を返すクエリを作成する必要があります。

次のようなインデックスとテーブル名で列をリストする方法の例をたくさん見つけました。

テーブルにインデックスがあるかどうかを確認するにはどうすればよいですか?

しかし、私の使用法のためにSQLクエリを書くのにまだ問題がありました。

どんな助けでも大歓迎です、

ありがとうございました

(これは Microsoft sql サーバー 2008 ~ 2012 用です)

これは私が書いたものであり、適切に機能しなかったため、質問です

select index1.name,sys.tables.name, Stuff((SELECT ',' + sys.columns.name AS [text()]
FROM
(
     select sys.columns.name
     from sys.columns
     inner join sys.index_columns On sys.index_columns.index_column_id=sys.columns.column_id
     inner join sys.indexes on sys.indexes.index_id=sys.index_columns.index_id 
     where sys.indexes.index_id=index1.index_id
) x
For XML PATH (''), type ).value('(./text())[1]','NVARCHAR(MAX)'),1,1,'') As coLName
from sys.indexes as index1 
inner join sys.tables on sys.tables.object_id=index1.object_id
inner join sys.index_columns On index1.index_id=sys.index_columns.index_id  AND sys.index_columns.object_id = sys.tables.object_id
inner join sys.columns on sys.columns.column_id=sys.index_columns.column_id And sys.columns.object_id=sys.tables.object_id
where sys.tables.name=TABLE_NAME
4

2 に答える 2

1

このクエリをCTEで試すことができます

 DECLARE @searchIndex nvarchar(100) = 'name,age',
         @tableName nvarchar(100) = 'your_tableName'
;WITH cte AS
 (
  SELECT i.name AS index_name, c.name      
  FROM sys.indexes i LEFT JOIN sys.index_columns ic ON i.object_id = ic.object_id AND i.index_id = ic.index_id
                     LEFT JOIN sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id
  WHERE i.object_id = OBJECT_ID(@tableName) AND ic.is_included_column != 1
  ), cte2 AS
 (
  SELECT c2.index_name, 
    STUFF((SELECT ',' + c.name
           FROM cte c
           WHERE c.index_name = c2.index_name
  FOR XML PATH, TYPE).value('.[1]', 'nvarchar(max)'), 1, 1, '') [columns]
  FROM cte c2
  GROUP BY c2.index_name 
  )
  SELECT *
  FROM cte2
  WHERE [columns] = @searchIndex

2番目のシナリオでは、順序に関係なく、列とスペースを指定します

DECLARE @searchIndex nvarchar(100) = '  age, name' ,
        @tableName nvarchar (100) = 'your_tableName'       
;WITH ParsSearchIndex AS
 (
  SELECT SUBSTRING(@searchIndex , 0 , CHARINDEX ( ',' , @searchIndex )) AS val ,
         CAST(STUFF (@searchIndex + ',' , 1, CHARINDEX( ',', @searchIndex), '') AS nvarchar (100 )) AS stval        
  UNION ALL
  SELECT LTRIM(SUBSTRING (stval , 0, CHARINDEX( ',', stval))),
         CAST(STUFF (stval , 1, CHARINDEX( ',' , stval ), '' ) AS nvarchar(100 ))        
  FROM ParsSearchIndex
  WHERE stval != ''
  ), max_ParsSearchIndex AS
 (
  SELECT val, COUNT(*) OVER() AS cnt
  FROM ParsSearchIndex
  WHERE val != ''
  ), cte AS
 (
  SELECT i.name AS index_name, c.name , ic .is_included_column,
         MAX(ic .index_column_id) OVER( PARTITION BY i. index_id) AS maxIndex_column_id
  FROM sys.indexes i LEFT JOIN sys. index_columns ic ON i.object_id = ic.object_id AND i.index_id = ic.index_id
                     LEFT JOIN sys. columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id
  WHERE i.object_id = OBJECT_ID(@tableName )
  ), cte2 AS
 (
  SELECT c2.index_name , COUNT (*) AS cnt, c2. maxIndex_column_id,
    STUFF((SELECT ',' + c .name
           FROM cte c
           WHERE c .index_name = c2 .index_name        
  FOR XML PATH , TYPE ).value( '.[1]', 'nvarchar(max)'), 1 , 1, '') [columns]
  FROM cte c2
  WHERE c2.is_included_column != 1 AND EXISTS (
                         SELECT 1
                         FROM max_ParsSearchIndex p
                         WHERE c2 .name = p .val AND p .cnt = c2.maxIndex_column_id
                         )
  GROUP BY c2 .index_name, c2. maxIndex_column_id
  )
  SELECT index_name, [columns]
  FROM cte2
  WHERE cnt = maxIndex_column_id
于 2013-02-21T15:16:16.537 に答える
0

これは私にとってはうまくいきました。

DECLARE @searchIndex nvarchar(100) = 'age,name',
        @schemeName nvarchar(100) = 'dbo',
        @tableName nvarchar(100) = 'tableName'

SELECT
    name
FROM (SELECT
    indexes.name,
    (SELECT
            c1.name + ','
    FROM sys.schemas s1
    INNER JOIN sys.tables t1
        ON s1.schema_id = t1.schema_id
        AND tables.object_id = t1.object_id
    INNER JOIN sys.columns c1
        ON t1.object_id = c1.object_id
    INNER JOIN sys.indexes i1
        ON t1.object_id = i1.object_id
        AND indexes.index_id = i1.index_id
    INNER JOIN sys.index_columns ic1
        ON t1.object_id = ic1.object_id
        AND i1.index_id = ic1.index_id
        AND c1.column_id = ic1.column_id
    WHERE schemas.schema_id = s1.schema_id
    ORDER BY c1.name
    FOR xml PATH (''))
    AS colName
FROM sys.schemas
INNER JOIN sys.tables
    ON schemas.schema_id = tables.schema_id
    AND tables.name = @tableName
INNER JOIN sys.indexes
    ON tables.object_id = indexes.object_id
WHERE schemas.name = @schemeName) A
WHERE A.colName = @searchIndex + ',' 
于 2013-02-21T15:29:52.670 に答える