You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: MssqlMcp/dotnet/MssqlMcp/Tools/DescribeTable.cs
+70-4Lines changed: 70 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -19,20 +19,31 @@ public partial class Tools
19
19
publicasyncTask<DbOperationResult>DescribeTable(
20
20
[Description("Name of table")]stringname)
21
21
{
22
+
string?schema=null;
23
+
if(name.Contains('.'))
24
+
{
25
+
// If the table name contains a schema, split it into schema and table name
26
+
varparts=name.Split('.');
27
+
if(parts.Length>1)
28
+
{
29
+
name=parts[1];// Use only the table name part
30
+
schema=parts[0];// Use the first part as schema
31
+
}
32
+
}
22
33
// Query for table metadata
23
34
conststringTableInfoQuery=@"SELECT t.object_id AS id, t.name, s.name AS [schema], p.value AS description, t.type, u.name AS owner
24
35
FROM sys.tables t
25
36
INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
26
37
LEFT JOIN sys.extended_properties p ON p.major_id = t.object_id AND p.minor_id = 0 AND p.name = 'MS_Description'
27
38
LEFT JOIN sys.sysusers u ON t.principal_id = u.uid
28
-
WHERE t.name = @TableName";
39
+
WHERE t.name = @TableName and (s.name = @TableSchema or @TableSchema IS NULL) ";
29
40
30
41
// Query for columns
31
42
conststringColumnsQuery=@"SELECT c.name, ty.name AS type, c.max_length AS length, c.precision, c.is_nullable AS nullable, p.value AS description
32
43
FROM sys.columns c
33
44
INNER JOIN sys.types ty ON c.user_type_id = ty.user_type_id
34
45
LEFT JOIN sys.extended_properties p ON p.major_id = c.object_id AND p.minor_id = c.column_id AND p.name = 'MS_Description'
35
-
WHERE c.object_id = (SELECT object_id FROM sys.tables WHERE name = @TableName)";
46
+
WHERE c.object_id = (SELECT object_id FROM sys.tables t INNER JOIN sys.schemas s ON t.schema_id = s.schema_id WHERE t.name = @TableName and (s.name = @TableSchema or @TableSchema IS NULL ) )";
36
47
37
48
// Query for indexes
38
49
conststringIndexesQuery=@"SELECT i.name, i.type_desc AS type, p.value AS description,
@@ -41,16 +52,43 @@ FROM sys.columns c
41
52
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 keys
42
53
FROM sys.indexes i
43
54
LEFT JOIN sys.extended_properties p ON p.major_id = i.object_id AND p.minor_id = i.index_id AND p.name = 'MS_Description'
44
-
WHERE i.object_id = (SELECT object_id FROM sys.tables WHERE name = @TableName) AND i.is_primary_key = 0 AND i.is_unique_constraint = 0";
55
+
WHERE i.object_id = (SELECT object_id FROM sys.tables t INNER JOIN sys.schemas s ON t.schema_id = s.schema_id WHERE t.name = @TableName and (s.name = @TableSchema or @TableSchema IS NULL ) ) AND i.is_primary_key = 0 AND i.is_unique_constraint = 0";
45
56
46
57
// Query for constraints
47
58
conststringConstraintsQuery=@"SELECT kc.name, kc.type_desc AS type,
48
59
STUFF((SELECT ',' + c.name FROM sys.index_columns ic
49
60
INNER JOIN sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id
50
61
WHERE ic.object_id = kc.parent_object_id AND ic.index_id = kc.unique_index_id ORDER BY ic.key_ordinal FOR XML PATH('')), 1, 1, '') AS keys
51
62
FROM sys.key_constraints kc
52
-
WHERE kc.parent_object_id = (SELECT object_id FROM sys.tables WHERE name = @TableName)";
63
+
WHERE kc.parent_object_id = (SELECT object_id FROM sys.tables t INNER JOIN sys.schemas s ON t.schema_id = s.schema_id WHERE t.name = @TableName and (s.name = @TableSchema or @TableSchema IS NULL ) )";
64
+
53
65
66
+
conststringForeignKeyInformation=@"SELECT
67
+
fk.name AS name,
68
+
SCHEMA_NAME(tp.schema_id) AS [schema],
69
+
tp.name AS table_name,
70
+
STRING_AGG(cp.name, ', ') WITHIN GROUP (ORDER BY fkc.constraint_column_id) AS column_names,
71
+
SCHEMA_NAME(tr.schema_id) AS referenced_schema,
72
+
tr.name AS referenced_table,
73
+
STRING_AGG(cr.name, ', ') WITHIN GROUP (ORDER BY fkc.constraint_column_id) AS referenced_column_names
74
+
FROM
75
+
sys.foreign_keys AS fk
76
+
JOIN
77
+
sys.foreign_key_columns AS fkc ON fk.object_id = fkc.constraint_object_id
78
+
JOIN
79
+
sys.tables AS tp ON fkc.parent_object_id = tp.object_id
80
+
JOIN
81
+
sys.columns AS cp ON fkc.parent_object_id = cp.object_id AND fkc.parent_column_id = cp.column_id
82
+
JOIN
83
+
sys.tables AS tr ON fkc.referenced_object_id = tr.object_id
84
+
JOIN
85
+
sys.columns AS cr ON fkc.referenced_object_id = cr.object_id AND fkc.referenced_column_id = cr.column_id
86
+
WHERE
87
+
( SCHEMA_NAME(tp.schema_id) = @TableSchema OR @TableSchema IS NULL )
0 commit comments