51 lines
3.1 KiB
Java
51 lines
3.1 KiB
Java
import java.io.FileInputStream;
|
|
import java.sql.*;
|
|
import java.util.Properties;
|
|
public class OrgIdSurvey {
|
|
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"))) {
|
|
System.out.println("== tables with b_org_id column ==");
|
|
String sql = "SELECT t.name AS tbl, c.name AS col, c.is_nullable, c.max_length, dc.definition AS def, t2.name AS dtype " +
|
|
"FROM sys.tables t " +
|
|
"JOIN sys.columns c ON c.object_id = t.object_id " +
|
|
"JOIN sys.types t2 ON t2.user_type_id = c.user_type_id " +
|
|
"LEFT JOIN sys.default_constraints dc ON dc.parent_object_id = c.object_id AND dc.parent_column_id = c.column_id " +
|
|
"WHERE c.name = 'b_org_id' ORDER BY t.name";
|
|
try (Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(sql)) {
|
|
boolean any = false;
|
|
while (rs.next()) {
|
|
any = true;
|
|
System.out.printf(" %-22s %-10s null=%-5s type=%-10s default=%s%n",
|
|
rs.getString("tbl"), rs.getString("col"), rs.getBoolean("is_nullable"),
|
|
rs.getString("dtype"), rs.getString("def"));
|
|
}
|
|
if (!any) System.out.println(" (none)");
|
|
}
|
|
System.out.println("\n== all user tables + row counts ==");
|
|
try (Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(
|
|
"SELECT t.name, p.rows FROM sys.tables t " +
|
|
"JOIN sys.partitions p ON p.object_id = t.object_id AND p.index_id IN (0,1) " +
|
|
"WHERE t.is_ms_shipped = 0 ORDER BY t.name")) {
|
|
while (rs.next()) System.out.printf(" %-22s rows=%s%n", rs.getString("name"), rs.getLong("rows"));
|
|
}
|
|
System.out.println("\n== sample distinct b_org_id values per table ==");
|
|
try (Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(
|
|
"SELECT t.name AS tbl FROM sys.tables t JOIN sys.columns c ON c.object_id = t.object_id " +
|
|
"WHERE c.name = 'b_org_id' AND t.is_ms_shipped = 0")) {
|
|
while (rs.next()) {
|
|
String tbl = rs.getString("tbl");
|
|
try (Statement st2 = conn.createStatement(); ResultSet rs2 = st2.executeQuery(
|
|
"SELECT COUNT(*) AS total, COUNT(DISTINCT b_org_id) AS distinct_orgs, MIN(b_org_id) AS min_org, MAX(b_org_id) AS max_org FROM [" + tbl + "]")) {
|
|
rs2.next();
|
|
System.out.printf(" %-22s rows=%-6s distinct_org=%-3s min=%s max=%s%n",
|
|
tbl, rs2.getLong("total"), rs2.getLong("distinct_orgs"),
|
|
rs2.getString("min_org"), rs2.getString("max_org"));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|