Skip to main content
Glama

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
NameRequiredDescriptionDefault
table_nameYesName of the table to export
databaseNoDatabase name (optional)
schemaNoSchema name (optional, defaults to dbo)
limitNoMaximum number of rows to export (optional)
whereNoWHERE 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; } }
  • 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); } }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/egarcia74/warp-sql-server-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server