Use when needing to retrieve the T-SQL definition text of stored procedures, views, or functions from the source SQL Server database for analyzing migration failures, creating test cases, or debugging conversion issues
Retrieves the original T-SQL definition text of database objects (stored procedures, views, functions) from the source SQL Server database using Python script, enabling analysis and troubleshooting of migration issues.
Need source object definition?
├── Is it a stored procedure, view, or function?
│ ├── Yes → Use this skill
│ └── No → Use other methods (tables, triggers, etc.)
└── Purpose?
├── Debug migration error → Use this skill
├── Create unit test → Use this skill
├── Compare before/after → Use this skill
└── Just browsing → Consider SSMS instead
Use cases:
When NOT to use:
INFORMATION_SCHEMA or SSMS)# Open SSMS, navigate to object, right-click, modify, copy script
# Issues: Slow, can't automate, doesn't integrate with workflow
# Quick, scriptable, integrates with debugging workflow
python .claude/skills/load-source-define/get_object_definition.py \
--proc usp_GetCustomerData \
--output original_tsql.sql
| Task | Command Example |
|---|---|
| Get stored procedure | python ... --proc usp_MyProc |
| Get view definition | python ... --view vw_Summary |
| Get function definition | python ... --func ufn_Calculate |
| Save to file | Add --output filename.sql |
| Display on screen | Omit --output |
.claude/skills/load-source-define/get_object_definition.py
pip install pyodbc
Note: Requires ODBC Driver 17 for SQL Server to be installed.
The script uses hardcoded database connection information configured in get_object_definition.py:
192.168.1.111\server2008pmsmasterdevjxdjxd598To modify the connection, edit the DB_CONNECTION_STRING in the script.
Example 1: Get stored procedure definition
python .claude/skills/load-source-define/get_object_definition.py \
--proc usp_GetCustomerData \
--output usp_GetCustomerData_original.sql
Example 2: Get view definition
python .claude/skills/load-source-define/get_object_definition.py \
--view vw_SalesSummary \
--output vw_SalesSummary_original.sql
Example 3: Get function definition and display on screen
python .claude/skills/load-source-define/get_object_definition.py \
--func fn_CalculateTax
| Argument | Required | Description |
|---|---|---|
--proc | No* | Stored procedure name |
--view | No* | View name |
--func | No* | Function name |
--output, -o | No | Output file path (if omitted, prints to stdout) |
*One of --proc, --view, or --func is required
Connects using pyodbc
DB_CONNECTION_STRING in script)Executes sp_helptext system stored procedure
EXEC sp_helptext 'dbo.usp_MyProcedure'
Handles line splitting at 255 characters
sp_helptext splits long lines at exactly 255 charactersOutput options
--output: Saves to file with UTF-8 encodingContext: Migration log shows "存储过程 usp_CalculateTax 迁移失败"
Workflow:
# Step 1: Get original T-SQL from source database
python .claude/skills/load-source-define/get_object_definition.py \
--proc usp_CalculateTax \
--output usp_CalculateTax_original.sql
# Step 2: Check log file for converted PostgreSQL script
# (通常在 DatabaseMigration/bin/Debug/net9.0-windows/logs/ 目录下)
# Step 3: Compare and identify the problematic statement
# 原始 T-SQL 可能包含不支持的语法,如:
# - 临时表的特殊用法
# - 特定的 T-SQL 函数
# - 复杂的 CTE 或子查询
Context: Adding support for new T-SQL syntax feature (e.g., CTE with UNION)
Workflow:
# Step 1: Find real objects using the feature
python .claude/skills/load-source-define/get_object_definition.py \
--view vw_WithUnion \
--output test_data_cte_union.sql
# Step 2: Create unit test using real definition
# Edit: DatabaseMigrationTest/TSqlFragmentExtension_Xxx_Tests.cs
[Theory]
[InlineData("vw_WithUnion")] // Test data from real object
public void ConvertSelectWithUnion_Tests(string viewName)
{
// Load real definition from test data file
string tsql = File.ReadAllText($"test_data_{viewName}.sql");
// Test conversion...
}
Context: Need to understand how an object works before fixing conversion logic
Workflow:
# Get definition and analyze patterns
python .claude/skills/load-source-define/get_object_definition.py \
--proc usp_ComplexReport \
--output analysis.sql
# Analyze specific patterns
grep -i "tempdb..#" analysis.sql # Uses temp tables?
grep -i "DECLARE CURSOR" analysis.sql # Uses cursors?
grep -i "WITH" analysis.sql # Uses CTE?
# ❌ Error: ModuleNotFoundError: No module named 'pyodbc'
python get_object_definition.py --proc MyProc
# ✅ Fix: Install dependency first
pip install pyodbc
python get_object_definition.py --proc MyProc
Note: Also ensure ODBC Driver 17 for SQL Server is installed on your system.
# ❌ Wrong: Assumes dbo schema, but object is in custom schema
python get_object_definition.py --proc MyProc
# Returns: 未找到对象 'MyProc'
# ✅ Correct: Specify full schema name
python get_object_definition.py --proc "myschema.MyProc"
# ❌ Wrong: sp_helptext doesn't work for tables
python get_object_definition.py --proc Customers
# Returns: 未找到对象 'Customers'
# ✅ Correct: Use other methods for tables
# Use SSMS or query INFORMATION_SCHEMA.COLUMNS
# ❌ Wrong: Object name with spaces breaks command parsing
python get_object_definition.py --proc My Proc
# ✅ Correct: Use quotes
python get_object_definition.py --proc "My Proc"
# ❌ Wrong: Connection fails with outdated credentials
python get_object_definition.py --proc MyProc
# Returns: Login failed for user 'jxd'
# ✅ Fix: Update DB_CONNECTION_STRING in get_object_definition.py
# Edit the file and modify the connection parameters
This skill integrates with the existing migration workflow described in CLAUDE.md:
DatabaseMigration/Migration/StoredProcedureMigrator.cs:53 - Uses MigrationUtils.GetObjectDefinition() (C# equivalent)DatabaseMigration/Migration/ViewMigrator.cs:80 - Uses MigrationUtils.GetObjectDefinition() (C# equivalent)The Python script provides the same functionality as:
// C# code in MigrationUtils.cs
string definition = MigrationUtils.GetObjectDefinition(connection, "MyProc");
But offers advantages:
DB_CONNECTION_STRING within the script