import java.io.FileInputStream; import java.sql.*; import java.util.Properties; public class IndexSurvey { public static void main(String[] args) throws Exception { Properties props = new Properties(); try (FileInputStream in = new FileInputStream(args[0])) { props.load(in); } try (Connection conn = DriverManager.getConnection(props.getProperty("url"), props.getProperty("username"), props.getProperty("password"))) { String sql = "SELECT OBJECT_NAME(i.object_id) AS tbl, i.name AS idx, i.type_desc AS itype, " + " i.is_unique, i.is_primary_key, " + " STUFF((SELECT ',' + c.name FROM sys.index_columns ic " + " JOIN sys.columns c ON c.object_id = ic.object_id AND c.column_id = ic.column_id " + " WHERE ic.object_id = i.object_id AND ic.index_id = i.index_id " + " ORDER BY ic.key_ordinal FOR XML PATH('')), 1, 1, '') AS cols " + "FROM sys.indexes i " + "WHERE i.object_id IN (SELECT object_id FROM sys.tables WHERE name LIKE 's' + char(95) + '%') " + " AND i.type IN (1,2) AND i.is_disabled = 0 " + "ORDER BY tbl, i.index_id"; try (Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(sql)) { while (rs.next()) { System.out.printf("%-22s %-32s %-12s unique=%s pk=%s cols=%s%n", rs.getString("tbl"), rs.getString("idx"), rs.getString("itype"), rs.getBoolean("is_unique"), rs.getBoolean("is_primary_key"), rs.getString("cols")); } } } } }