Skip to main content

RFC Protocol Deep Dive

This comprehensive guide covers the RFC (Remote Function Call) protocol in ERPL. Learn how to read SAP tables, call function modules, and handle metadata like a pro.

What is RFC?

RFC (Remote Function Call) is SAP's protocol for calling functions across system boundaries. It's the foundation of SAP integration and allows you to access SAP tables and execute function modules remotely.

RFC Architecture

Core RFC Functions

sap_rfc_read_table()

The primary function for reading SAP tables.

-- Basic table reading
SELECT * FROM sap_rfc_read_table('KNA1', LIMIT => 100);

-- With field selection
SELECT KUNNR, NAME1, LAND1
FROM sap_rfc_read_table('KNA1', LIMIT => 50);

-- With WHERE conditions
SELECT * FROM sap_rfc_read_table('VBAK', LIMIT => 100)
WHERE ERDAT >= '2024-01-01';

Parameters

ParameterTypeDescriptionExample
table_nameStringSAP table name'KNA1'
LIMITIntegerMaximum rows to returnLIMIT => 1000
FIELDSStringComma-separated field listFIELDS => 'KUNNR,NAME1'
WHEREStringWHERE clause conditionsWHERE => 'LAND1 = "DE"'
ORDER_BYStringSort orderORDER_BY => 'KUNNR'

sap_rfc_invoke()

Call SAP function modules and BAPIs.

-- Call a simple function module
SELECT * FROM sap_rfc_invoke(
'RFC_SYSTEM_INFO',
path => '/RFCINFO'
);

-- Call BAPI for customer creation
SELECT * FROM sap_rfc_invoke(
'BAPI_CUSTOMER_CREATEFROMDATA1',
{'CUSTOMERDATA': {...}}
);

-- Call BAPI with specific path
SELECT * FROM sap_rfc_invoke(
'BAPI_FLIGHT_GETLIST',
path => '/FLIGHT_LIST'
);

-- Call BAPI with parameters
SELECT * FROM sap_rfc_invoke(
'BAPI_FLIGHT_GETDETAIL',
{'AIRLINEID': 'LH', 'CONNECTIONID': '0400', 'FLIGHTDATE': '2016-11-18'::DATE}
);

Parameters

ParameterTypeDescriptionExample
function_nameStringSAP function module name'BAPI_FLIGHT_GETLIST'
pathStringJSON path to specific return structurepath => '/FLIGHT_LIST'
parametersJSONFunction input parameters{'AIRLINEID': 'LH'}

sap_rfc_describe_function()

Get detailed information about RFC function parameters.

-- Describe function parameters
SELECT * FROM sap_rfc_describe_function('BAPI_FLIGHT_GETLIST');

-- Get parameter details
SELECT
name,
import[1].name AS import_params,
export[1].name AS export_params
FROM sap_rfc_describe_function('STFC_CONNECTION');

sap_rfc_describe_fields()

Get table structure and field information.

-- Get table structure
SELECT * FROM sap_rfc_describe_fields('KNA1');

-- Get field details for specific table
SELECT
field_name,
field_type,
field_length,
decimals
FROM sap_rfc_describe_fields('VBAK');

sap_rfc_describe_references()

List all referenced tables in the SAP system.

-- List all referenced tables
SELECT * FROM sap_rfc_describe_references();

sap_rfc_show_function()

List all available RFC functions.

-- List all RFC functions
SELECT * FROM sap_rfc_show_function();

sap_rfc_show_groups()

List all RFC function groups.

-- List all function groups
SELECT * FROM sap_rfc_show_groups();

sap_rfc_show_tables()

List all available SAP tables.

-- List all tables
SELECT * FROM sap_rfc_show_tables();

Common SAP Tables

Master Data Tables

-- Customer Master Data
SELECT
KUNNR AS customer_number,
NAME1 AS customer_name,
LAND1 AS country,
REGIO AS region,
ORT01 AS city
FROM sap_rfc_read_table('KNA1', LIMIT => 1000)
WHERE LAND1 = 'DE';

-- Material Master Data
SELECT
MATNR AS material_number,
MTART AS material_type,
MEINS AS base_unit,
MATKL AS material_group
FROM sap_rfc_read_table('MARA', LIMIT => 1000)
WHERE MTART = 'FERT'; -- Finished goods

-- Vendor Master Data
SELECT
LIFNR AS vendor_number,
NAME1 AS vendor_name,
LAND1 AS country,
REGIO AS region
FROM sap_rfc_read_table('LFA1', LIMIT => 1000);

Transaction Data Tables

-- Sales Document Header
SELECT
VBELN AS sales_document,
ERDAT AS document_date,
KUNNR AS customer,
NETWR AS net_value,
WAERK AS currency
FROM sap_rfc_read_table('VBAK', LIMIT => 1000)
WHERE ERDAT >= '2024-01-01';

-- Sales Document Items
SELECT
VBELN AS sales_document,
POSNR AS item_number,
MATNR AS material,
KWMENG AS quantity,
NETWR AS net_value
FROM sap_rfc_read_table('VBAP', LIMIT => 1000)
WHERE VBELN IN ('0000000001', '0000000002');

Financial Tables

-- Accounting Document Header
SELECT
BUKRS AS company_code,
BELNR AS document_number,
GJAHR AS fiscal_year,
BLART AS document_type,
BUDAT AS posting_date
FROM sap_rfc_read_table('BKPF', LIMIT => 1000)
WHERE GJAHR = '2024';

-- Accounting Document Items
SELECT
BUKRS AS company_code,
BELNR AS document_number,
GJAHR AS fiscal_year,
BUZEI AS line_item,
HKONT AS gl_account,
DMBTR AS amount_in_local_currency
FROM sap_rfc_read_table('BSEG', LIMIT => 1000)
WHERE HKONT LIKE '1%'; -- Asset accounts

Advanced RFC Techniques

Metadata Handling

-- Get table structure
SELECT * FROM sap_rfc_describe_fields('KNA1');

-- Get function module parameters
SELECT * FROM sap_rfc_describe_function('BAPI_CUSTOMER_GETDETAIL');

-- List all available functions
SELECT * FROM sap_rfc_show_function();

-- List function groups
SELECT * FROM sap_rfc_show_groups();

-- List all tables
SELECT * FROM sap_rfc_show_tables();

Batch Processing

-- Process multiple tables in one call
SELECT
'KNA1' AS table_name,
COUNT(*) AS record_count
FROM sap_rfc_read_table('KNA1', LIMIT => 10000)

UNION ALL

SELECT
'VBAK' AS table_name,
COUNT(*) AS record_count
FROM sap_rfc_read_table('VBAK', LIMIT => 10000);

Error Handling

-- Check for errors in function calls
SELECT
function_name,
return_code,
error_message
FROM sap_rfc_invoke('RFC_SYSTEM_INFO')
WHERE return_code <> '0';

Performance Optimization

Connection Pooling

-- Reuse connections for multiple queries
ATTACH 'sap://your-host:3300' AS sap_pool (
USER 'username',
PASSWORD 'password',
POOL_SIZE => 5
);

-- Multiple queries use the same connection pool
SELECT * FROM sap_rfc_read_table('KNA1', LIMIT => 100);
SELECT * FROM sap_rfc_read_table('VBAK', LIMIT => 100);

Parallel Processing

-- Read multiple tables in parallel
SELECT 'KNA1' AS table_name, COUNT(*) AS count
FROM sap_rfc_read_table('KNA1', LIMIT => 10000)

UNION ALL

SELECT 'VBAK' AS table_name, COUNT(*) AS count
FROM sap_rfc_read_table('VBAK', LIMIT => 10000)

UNION ALL

SELECT 'MARA' AS table_name, COUNT(*) AS count
FROM sap_rfc_read_table('MARA', LIMIT => 10000);

Real-World Examples

Daily Sales Report

-- Extract daily sales data
WITH daily_sales AS (
SELECT
ERDAT AS sales_date,
COUNT(*) AS order_count,
SUM(NETWR) AS total_value,
AVG(NETWR) AS avg_order_value
FROM sap_rfc_read_table('VBAK', LIMIT => 100000)
WHERE ERDAT >= CURRENT_DATE - INTERVAL '30' DAY
GROUP BY ERDAT
)
SELECT
sales_date,
order_count,
total_value,
avg_order_value,
ROUND(avg_order_value, 2) AS avg_order_value_rounded
FROM daily_sales
ORDER BY sales_date DESC;

Customer Analysis

-- Analyze customer distribution by country
SELECT
LAND1 AS country,
COUNT(*) AS customer_count,
COUNT(*) * 100.0 / SUM(COUNT(*)) OVER() AS percentage
FROM sap_rfc_read_table('KNA1', LIMIT => 100000)
GROUP BY LAND1
ORDER BY customer_count DESC
LIMIT 10;

Material Master Analysis

-- Analyze material types and groups
SELECT
MTART AS material_type,
MATKL AS material_group,
COUNT(*) AS material_count
FROM sap_rfc_read_table('MARA', LIMIT => 100000)
GROUP BY MTART, MATKL
ORDER BY material_count DESC
LIMIT 20;

Troubleshooting

Common Issues

Table Not Found

-- Check table name (case-sensitive)
-- Verify table exists in your SAP system
SELECT * FROM sap_rfc_read_table('KNA1', LIMIT => 1);

Permission Denied

-- Check user permissions
-- Verify RFC access rights
SELECT * FROM sap_rfc_call_function('RFC_SYSTEM_INFO');

Connection Timeout

-- Increase timeout settings
ATTACH 'sap://your-host:3300' AS sap_system (
USER 'username',
PASSWORD 'password',
TIMEOUT => 300 -- 5 minutes
);

Debugging Tips

-- Enable debug mode
SET debug_mode = true;

-- Check connection status
SELECT * FROM sap_rfc_get_connection_info();

-- Monitor query performance
EXPLAIN SELECT * FROM sap_rfc_read_table('KNA1', LIMIT => 100);

-- Test function call
SELECT * FROM sap_rfc_invoke('RFC_SYSTEM_INFO');

For SAP Experts

Advanced RFC Internals

This section covers RFC protocol internals for SAP Basis administrators and developers.

RFC Protocol Details

RFC uses a binary protocol over TCP/IP with the following characteristics:

  • Port Range: 33xx (where xx is the instance number)
  • Protocol: Binary over TCP/IP
  • Authentication: Username/password or certificate-based
  • Compression: Optional data compression
  • Unicode: Full Unicode support

Performance Considerations

  1. Connection Pooling: Reuse connections to reduce overhead
  2. Batch Size: Optimize LIMIT parameter for your network
  3. Field Selection: Use FIELDS parameter to reduce data transfer
  4. Parallel Processing: Use multiple connections for large datasets

Security Best Practices

  1. Network Security: Use VPN or secure network connections
  2. Authentication: Use strong passwords or certificates
  3. Authorization: Implement proper RFC user roles
  4. Audit Logging: Enable RFC audit logs in SAP

Next Steps

🚀 Ready for More?

🔧 Advanced Topics

💡 Examples


Need help? Check our troubleshooting guide or browse more examples.