export_table_csv
Export SQL Server table data to CSV format with optional filters for database, schema, row limits, and WHERE conditions.
Instructions
Export table data in CSV format
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| table_name | Yes | Name of the table to export | |
| database | No | Database name (optional) | |
| schema | No | Schema name (optional, defaults to dbo) | |
| limit | No | Maximum number of rows to export (optional) | |
| where | No | WHERE clause conditions (optional) |
Implementation Reference
- The primary handler function implementing export_table_csv tool. Exports table data as CSV, supports streaming for large tables, handles database/schema switching, CSV escaping, performance monitoring, and error handling.async exportTableCsv(tableName, database = null, schema = 'dbo', limit = null) { try { const pool = await this.getConnection(); const request = pool.request(); // Switch database if specified if (database) { await request.query(`USE [${database}]`); } // Use streaming handler for CSV export const streamingResult = await this.streamingHandler.streamTableExport(request, tableName, { schema, database, limit, outputFormat: 'csv' }); // Track performance if (this.performanceMonitor) { const stats = this.streamingHandler.getStreamingStats(streamingResult); this.performanceMonitor.recordQuery({ tool: 'export_table_csv', query: `SELECT${limit ? ` TOP ${limit}` : ''} * FROM [${schema}].[${tableName}]`, executionTime: streamingResult.performance?.duration || 0, success: true, database, streaming: stats.streaming, totalRows: stats.totalRows, memoryEfficient: stats.memoryEfficient, timestamp: new Date() }); } // Handle empty results if (streamingResult.totalRows === 0) { return [ { type: 'text', text: 'No data found in table' } ]; } // For streaming results, reconstruct CSV from chunks if (streamingResult.streaming && streamingResult.chunks) { const csvContent = this.streamingHandler.reconstructFromChunks( streamingResult.chunks, 'csv' ); return [ { type: 'text', text: csvContent } ]; } // For non-streaming results, convert recordset to CSV if (streamingResult.recordset && streamingResult.recordset.length > 0) { const headers = Object.keys(streamingResult.recordset[0]); const csvHeaders = headers.join(','); const csvRows = streamingResult.recordset.map(row => headers .map(header => { const value = row[header]; if (value === null || value === undefined) return ''; const stringValue = String(value); // Escape quotes and wrap in quotes if contains comma or quotes if ( stringValue.includes(',') || stringValue.includes('"') || stringValue.includes('\n') ) { return `"${stringValue.replace(/"/g, '""')}"`; } return stringValue; }) .join(',') ); const csvContent = [csvHeaders, ...csvRows].join('\n'); return [ { type: 'text', text: csvContent } ]; } return [ { type: 'text', text: 'No data found in table' } ]; } catch (error) { // Track failed query if (this.performanceMonitor) { this.performanceMonitor.recordQuery({ tool: 'export_table_csv', query: `SELECT${limit ? ` TOP ${limit}` : ''} * FROM [${schema}].[${tableName}]`, executionTime: 0, success: false, error: error.message, database, timestamp: new Date() }); } throw error; } }
- lib/tools/tool-registry.js:85-98 (schema)Tool schema definition including input validation schema for parameters: table_name (required), database, schema, limit, where.name: 'export_table_csv', description: 'Export table data in CSV format', inputSchema: { type: 'object', properties: { table_name: { type: 'string', description: 'Name of the table to export' }, database: { type: 'string', description: 'Database name (optional)' }, schema: { type: 'string', description: 'Schema name (optional, defaults to dbo)' }, limit: { type: 'number', description: 'Maximum number of rows to export (optional)' }, where: { type: 'string', description: 'WHERE clause conditions (optional)' } }, required: ['table_name'] } }
- index.js:298-306 (registration)MCP tool call handler registration in the main server switch statement, dispatching to DatabaseToolsHandler.exportTableCsv.case 'export_table_csv': return { content: await this.databaseTools.exportTableCsv( args.table_name, args.database, args.schema, args.limit ) };
- index.js:561-567 (registration)Proxy method in SqlServerMCP class that delegates to the DatabaseToolsHandler.exportTableCsv for backward compatibility or testing.async exportTableCsv(...args) { try { return { content: await this.databaseTools.exportTableCsv(...args) }; } catch (error) { throw new McpError(ErrorCode.InternalError, error.message); } }