Skip to content

Commit 3d88d52

Browse files
committed
manage schema scoped tables in describe table
1 parent a7f973e commit 3d88d52

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ AzureSQLPromptFlowSamples/src/sql-promptflow-demo/deployment/model.yaml
1919
AzureSQLPromptFlowSamples/src/sql-promptflow-demo/promptflow_v2/flow.dag.yaml
2020
AzureSQLPromptFlowSamples/src/sql-promptflow-demo/promptflow_v2/.promptflow/
2121
AzureSQLPromptFlowSamples/src/sql-promptflow-demo/promptflow_v2/__pycache__/
22+
.vs/

MssqlMcp/dotnet/MssqlMcp/Tools/DescribeTable.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,31 @@ public partial class Tools
1919
public async Task<DbOperationResult> DescribeTable(
2020
[Description("Name of table")] string name)
2121
{
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+
var parts = 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+
}
2233
// Query for table metadata
2334
const string TableInfoQuery = @"SELECT t.object_id AS id, t.name, s.name AS [schema], p.value AS description, t.type, u.name AS owner
2435
FROM sys.tables t
2536
INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
2637
LEFT JOIN sys.extended_properties p ON p.major_id = t.object_id AND p.minor_id = 0 AND p.name = 'MS_Description'
2738
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) ";
2940

3041
// Query for columns
3142
const string ColumnsQuery = @"SELECT c.name, ty.name AS type, c.max_length AS length, c.precision, c.is_nullable AS nullable, p.value AS description
3243
FROM sys.columns c
3344
INNER JOIN sys.types ty ON c.user_type_id = ty.user_type_id
3445
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 ) )";
3647

3748
// Query for indexes
3849
const string IndexesQuery = @"SELECT i.name, i.type_desc AS type, p.value AS description,
@@ -41,15 +52,15 @@ FROM sys.columns c
4152
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
4253
FROM sys.indexes i
4354
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";
4556

4657
// Query for constraints
4758
const string ConstraintsQuery = @"SELECT kc.name, kc.type_desc AS type,
4859
STUFF((SELECT ',' + c.name FROM sys.index_columns ic
4960
INNER JOIN sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id
5061
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
5162
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 ) )";
5364

5465
var conn = await _connectionFactory.GetOpenConnectionAsync();
5566
try
@@ -61,6 +72,7 @@ FROM sys.key_constraints kc
6172
using (var cmd = new SqlCommand(TableInfoQuery, conn))
6273
{
6374
var _ = cmd.Parameters.AddWithValue("@TableName", name);
75+
_ = cmd.Parameters.AddWithValue("@TableSchema", schema == null ? DBNull.Value : schema);
6476
using var reader = await cmd.ExecuteReaderAsync();
6577
if (await reader.ReadAsync())
6678
{
@@ -83,6 +95,7 @@ FROM sys.key_constraints kc
8395
using (var cmd = new SqlCommand(ColumnsQuery, conn))
8496
{
8597
var _ = cmd.Parameters.AddWithValue("@TableName", name);
98+
_ = cmd.Parameters.AddWithValue("@TableSchema", schema == null ? DBNull.Value : schema);
8699
using var reader = await cmd.ExecuteReaderAsync();
87100
var columns = new List<object>();
88101
while (await reader.ReadAsync())
@@ -103,6 +116,7 @@ FROM sys.key_constraints kc
103116
using (var cmd = new SqlCommand(IndexesQuery, conn))
104117
{
105118
var _ = cmd.Parameters.AddWithValue("@TableName", name);
119+
_ = cmd.Parameters.AddWithValue("@TableSchema", schema == null ? DBNull.Value : schema);
106120
using var reader = await cmd.ExecuteReaderAsync();
107121
var indexes = new List<object>();
108122
while (await reader.ReadAsync())
@@ -121,6 +135,7 @@ FROM sys.key_constraints kc
121135
using (var cmd = new SqlCommand(ConstraintsQuery, conn))
122136
{
123137
var _ = cmd.Parameters.AddWithValue("@TableName", name);
138+
_ = cmd.Parameters.AddWithValue("@TableSchema", schema == null ? DBNull.Value : schema);
124139
using var reader = await cmd.ExecuteReaderAsync();
125140
var constraints = new List<object>();
126141
while (await reader.ReadAsync())

0 commit comments

Comments
 (0)