Large Language Models (LLMs) are revolutionizing data management systems with their advanced semantic understanding and exceptional generalization capabilities. In this article we will explain how to leverage existing web client procedure support in SAP HANA Cloud, data lake Relational Engine (HDLRE) to integrate LLMs. With this feature, customers can now harness the power of LLMs for enhanced database management and analytics.
SAP AI Core, a service within the SAP Business Technology Platform, is designed to manage the execution and operations of AI assets in a standardized and scalable manner. While this article primarily focuses on SAP AI Core as the LLM service, please note that the feature is highly adaptable and can be integrated with any LLM service that best meets your requirements.
To deploy an LLM model within SAP AI Core, follow the instructions provided at initial setup for SAP AI core service to enable the service, generate a service key, and obtain an access token. Subsequently, you can deploy a language model, such as GPT-5 or Cloud Sonnet 4, and retrieve the deployment URL. Detailed instructions for this process are available at create deployment for generative AI model in SAP AI core.
In HDLRE, you can create a user-defined web client procedure to make HTTP requests to an HTTP server. This procedure will be used to invoke LLM services. For a more detailed explanation, please refer to the documentation to create a procedure (web service).
In our example, we have developed a web client procedure named AIPOST to make AI API calls. Ensure that the API version is specified according to the relevant AI API documentation.
CREATE OR REPLACE PROCEDURE AIPOST (
data LONG VARCHAR
)
RESULT(c1 long varchar, c2 long varchar)
URL ‘$YOUR_DEPLOYMENT_URL/chat/completions?$API_VERSION’
TYPE ‘HTTP:POST:application/json’
HEADER ‘Authorization:Bearer $YOUR_ACCESS_TOKEN’
SET ‘HTTP(VERSION=1.1)’;
To call the procedure for an introduction to SAP HDLRE, use the following example:
CALL AIPOST(‘{ “messages”: [ { “role”: “user”, “content”: “What is SAP HDLRE” } ] }’);
Building on this, more advanced and practical stored procedures can be developed to leverage LLMs effectively. For instance, if we have an expense table, with some potentially suspicious round numbers( for testing), we can employ AI to detect any irregular data patterns that may indicate potential fraud. In this procedure, we will gather all data from the expense table and submit it, along with a query, to the LLM service.
Please be advised that the stored procedure sp_parse_json will be available in HDLRE version QRC4/2025.
CREATE OR REPLACE PROCEDURE ask_ai_fraud_detection(
IN user_question LONG VARCHAR
)
BEGIN
DECLARE _payload LONG VARCHAR;
DECLARE @expense_data LONG VARCHAR DEFAULT ”;
DECLARE _response LONG VARCHAR;
DECLARE @clean_question LONG VARCHAR;
IF user_question LIKE ‘%detection%’ OR user_question LIKE ‘%fraud%’ THEN
IF EXISTS (SELECT 1 FROM SYSTABLE WHERE table_name = ‘expense’ AND table_type = ‘BASE’) THEN
SELECT ‘All Expense Records: ‘ +
LIST(‘ID:’ + CAST(ISNULL(expense_id, 0) AS VARCHAR) +
‘|Desc:’ + ISNULL(expense_description, ‘N/A’) +
‘|Amount:’ + CAST(ISNULL(amount, 0) AS VARCHAR) +
‘|Date:’ + CAST(ISNULL(expense_date, TODAY()) AS VARCHAR) +
‘|Category:’ + ISNULL(category, ‘N/A’),
‘ || ‘)
INTO @expense_data
FROM (
SELECT expense_id, expense_description, amount, expense_date, category
FROM expense
ORDER BY expense_date DESC
) AS recent_expenses;
ELSE
SET @expense_data = ‘Expense table not found in database’;
END IF;
END IF;
— Clean the user question for JSON (escape quotes and newlines)
SET @clean_question = REPLACE(REPLACE(REPLACE(REPLACE(
user_question + ‘ [Database Context: ‘ + @expense_data + ‘]’,
‘”‘, ‘\”‘), CHAR(10), ‘\n’), CHAR(13), ‘\r’), CHAR(9), ‘\t’);
— Build complete JSON payload directly
SET _payload = ‘{“messages”:[{“role”:”user”,”content”:”‘ + @clean_question + ‘”}]}’;
SELECT c2 INTO _response
FROM (AIPOST(@json_payload))
WHERE c1 LIKE ‘%Body%’;
— Parse the JSON and extract the content
CALL sp_parse_json(‘ai_parsed’, _response);
   — Display the response
   SELECT ai_parsed.choices[[1]].[message].[content]; 
END;
call ask_ai_fraud_detection(‘Can you detect any fraud in the expenses table, apply Benford law if needed’);
Once the response is parsed from its JSON format, we obtain the analysis from the LLM service. Initially, it explains Benford’s law, and then identifies the dataset as suspicious, and offers recommendations.
Here is another example of leveraging AI to redact sensitive personal data and generate an alternative treatment plan for patients. Similar to the previous example, this stored procedure retrieves data from the patient_records table and sends it to the LLM service.
CREATE OR REPLACE PROCEDURE ask_ai_redact(
IN user_question LONG VARCHAR
)
BEGIN
DECLARE _payload LONG VARCHAR;
DECLARE _data LONG VARCHAR DEFAULT ”;
DECLARE _response LONG VARCHAR;
DECLARE @clean_question LONG VARCHAR;
— Get medical data from the specified table
IF EXISTS (SELECT 1 FROM SYSTABLE WHERE table_name = ‘patient_records’ AND table_type = ‘BASE’) THEN
— Build medical data string with all patient information
SELECT ‘Medical Records Data: ‘ +
LIST(‘Record_’ + CAST(record_id AS VARCHAR) +
‘|Name:’ + ISNULL(patient_name, ‘Unknown’) +
‘|DOB:’ + ISNULL(CAST(birth_date AS VARCHAR), ‘Unknown’) +
‘|Gender:’ + ISNULL(gender, ‘Unknown’) +
‘|Phone:’ + ISNULL(phone_number, ‘Unknown’) +
‘|Email:’ + ISNULL(email_address, ‘Unknown’) +
‘|Address:’ + ISNULL(street_address, ”) + ‘, ‘ + ISNULL(city_name, ”) + ‘, ‘ + ISNULL(province_code, ”) + ‘ ‘ + ISNULL(postal_code, ”) +
‘|SIN:’ + ISNULL(sin, ‘Unknown’) +
‘|OHIP:’ + ISNULL(ohip_number, ‘Unknown’) +
‘|Diagnosis:’ + ISNULL(CAST(diagnosis_description AS VARCHAR(100)), ‘Unknown’) +
‘|Treatment:’ + ISNULL(CAST(treatment_notes AS VARCHAR(100)), ‘Unknown’) +
‘|Doctor:’ + ISNULL(doctor_name, ‘Unknown’) +
‘|VisitDate:’ + ISNULL(CAST(visit_date AS VARCHAR), ‘Unknown’) +
‘ || ‘)
INTO _data
FROM (
SELECT * FROM patient_records ORDER BY record_id
) AS sample_records;
ELSE
SET _data = ‘No medical records table found’;
END IF;
— Clean the user question for JSON (escape quotes and newlines)
SET @clean_question = REPLACE(REPLACE(REPLACE(REPLACE(
user_question + ‘ [Database Context: ‘ + _data + ‘]’,
‘”‘, ‘\”‘), CHAR(10), ‘\n’), CHAR(13), ‘\r’), CHAR(9), ‘\t’);
— Build complete JSON payload directly
SET _payload = ‘{“messages”:[{“role”:”user”,”content”:”‘ + @clean_question + ‘”}]}’;
SELECT c2 INTO _response
FROM (AIPOST(@json_payload))
WHERE c1 LIKE ‘%Body%’;
— Parse the JSON and extract the content
CALL sp_parse_json(‘ai_parsed’, _response);
   — Display the response
   SELECT ai_parsed.choices[[1]].[message].[content]; 
END;
call ask_ai_redact(‘Replace sensitive personal data with placeholder values like [REDACTED_NAME]. Return the redacted data in the same format. Also provide an alternative treatment plan for each patient.’);
The response confirms that all sensitive data has been redacted, and a recommended treatment plan is provided.
Additionally, we can develop auxiliary stored procedures to simulate context windows, preserving chat history like chat-based AI models like ChatGPT. We can also create procedures that leverage LLMs for data usage predictions, schema analysis, and more. This feature allows us to fully capitalize on the immense potential of both the database and the LLMs. We hope you find this feature valuable and enjoy the numerous opportunities it brings for enhanced database management and analytics!
 Large Language Models (LLMs) are revolutionizing data management systems with their advanced semantic understanding and exceptional generalization capabilities. In this article we will explain how to leverage existing web client procedure support in SAP HANA Cloud, data lake Relational Engine (HDLRE) to integrate LLMs. With this feature, customers can now harness the power of LLMs for enhanced database management and analytics. SAP AI Core, a service within the SAP Business Technology Platform, is designed to manage the execution and operations of AI assets in a standardized and scalable manner. While this article primarily focuses on SAP AI Core as the LLM service, please note that the feature is highly adaptable and can be integrated with any LLM service that best meets your requirements. To deploy an LLM model within SAP AI Core, follow the instructions provided at initial setup for SAP AI core service to enable the service, generate a service key, and obtain an access token. Subsequently, you can deploy a language model, such as GPT-5 or Cloud Sonnet 4, and retrieve the deployment URL. Detailed instructions for this process are available at create deployment for generative AI model in SAP AI core. In HDLRE, you can create a user-defined web client procedure to make HTTP requests to an HTTP server. This procedure will be used to invoke LLM services. For a more detailed explanation, please refer to the documentation to create a procedure (web service). In our example, we have developed a web client procedure named AIPOST to make AI API calls. Ensure that the API version is specified according to the relevant AI API documentation. CREATE OR REPLACE PROCEDURE AIPOST (
data LONG VARCHAR
)
RESULT(c1 long varchar, c2 long varchar)
URL ‘$YOUR_DEPLOYMENT_URL/chat/completions?$API_VERSION’
TYPE ‘HTTP:POST:application/json’
HEADER ‘Authorization:Bearer $YOUR_ACCESS_TOKEN’
SET ‘HTTP(VERSION=1.1)’;
To call the procedure for an introduction to SAP HDLRE, use the following example: CALL AIPOST(‘{ “messages”: [ { “role”: “user”, “content”: “What is SAP HDLRE” } ] }’); Building on this, more advanced and practical stored procedures can be developed to leverage LLMs effectively. For instance, if we have an expense table, with some potentially suspicious round numbers( for testing), we can employ AI to detect any irregular data patterns that may indicate potential fraud. In this procedure, we will gather all data from the expense table and submit it, along with a query, to the LLM service. Please be advised that the stored procedure sp_parse_json will be available in HDLRE version QRC4/2025. CREATE OR REPLACE PROCEDURE ask_ai_fraud_detection(
IN user_question LONG VARCHAR
)
BEGIN
DECLARE _payload LONG VARCHAR;
DECLARE @expense_data LONG VARCHAR DEFAULT ”;
DECLARE _response LONG VARCHAR;
DECLARE @clean_question LONG VARCHAR;
IF user_question LIKE ‘%detection%’ OR user_question LIKE ‘%fraud%’ THEN
IF EXISTS (SELECT 1 FROM SYSTABLE WHERE table_name = ‘expense’ AND table_type = ‘BASE’) THEN
SELECT ‘All Expense Records: ‘ +
LIST(‘ID:’ + CAST(ISNULL(expense_id, 0) AS VARCHAR) +
‘|Desc:’ + ISNULL(expense_description, ‘N/A’) +
‘|Amount:’ + CAST(ISNULL(amount, 0) AS VARCHAR) +
‘|Date:’ + CAST(ISNULL(expense_date, TODAY()) AS VARCHAR) +
‘|Category:’ + ISNULL(category, ‘N/A’),
‘ || ‘)
INTO @expense_data
FROM (
SELECT expense_id, expense_description, amount, expense_date, category
FROM expense
ORDER BY expense_date DESC
) AS recent_expenses;
ELSE
SET @expense_data = ‘Expense table not found in database’;
END IF;
END IF;
— Clean the user question for JSON (escape quotes and newlines)
SET @clean_question = REPLACE(REPLACE(REPLACE(REPLACE(
user_question + ‘ [Database Context: ‘ + @expense_data + ‘]’,
‘”‘, ‘\”‘), CHAR(10), ‘\n’), CHAR(13), ‘\r’), CHAR(9), ‘\t’);
— Build complete JSON payload directly
SET _payload = ‘{“messages”:[{“role”:”user”,”content”:”‘ + @clean_question + ‘”}]}’;
SELECT c2 INTO _response
FROM (AIPOST(@json_payload))
WHERE c1 LIKE ‘%Body%’;
— Parse the JSON and extract the content
CALL sp_parse_json(‘ai_parsed’, _response);
   — Display the response
   SELECT ai_parsed.choices[[1]].[message].[content]; 
END;
call ask_ai_fraud_detection(‘Can you detect any fraud in the expenses table, apply Benford law if needed’); Once the response is parsed from its JSON format, we obtain the analysis from the LLM service. Initially, it explains Benford’s law, and then identifies the dataset as suspicious, and offers recommendations. Here is another example of leveraging AI to redact sensitive personal data and generate an alternative treatment plan for patients. Similar to the previous example, this stored procedure retrieves data from the patient_records table and sends it to the LLM service. CREATE OR REPLACE PROCEDURE ask_ai_redact(
IN user_question LONG VARCHAR
)
BEGIN
DECLARE _payload LONG VARCHAR;
DECLARE _data LONG VARCHAR DEFAULT ”;
DECLARE _response LONG VARCHAR;
DECLARE @clean_question LONG VARCHAR;
— Get medical data from the specified table
IF EXISTS (SELECT 1 FROM SYSTABLE WHERE table_name = ‘patient_records’ AND table_type = ‘BASE’) THEN
— Build medical data string with all patient information
SELECT ‘Medical Records Data: ‘ +
LIST(‘Record_’ + CAST(record_id AS VARCHAR) +
‘|Name:’ + ISNULL(patient_name, ‘Unknown’) +
‘|DOB:’ + ISNULL(CAST(birth_date AS VARCHAR), ‘Unknown’) +
‘|Gender:’ + ISNULL(gender, ‘Unknown’) +
‘|Phone:’ + ISNULL(phone_number, ‘Unknown’) +
‘|Email:’ + ISNULL(email_address, ‘Unknown’) +
‘|Address:’ + ISNULL(street_address, ”) + ‘, ‘ + ISNULL(city_name, ”) + ‘, ‘ + ISNULL(province_code, ”) + ‘ ‘ + ISNULL(postal_code, ”) +
‘|SIN:’ + ISNULL(sin, ‘Unknown’) +
‘|OHIP:’ + ISNULL(ohip_number, ‘Unknown’) +
‘|Diagnosis:’ + ISNULL(CAST(diagnosis_description AS VARCHAR(100)), ‘Unknown’) +
‘|Treatment:’ + ISNULL(CAST(treatment_notes AS VARCHAR(100)), ‘Unknown’) +
‘|Doctor:’ + ISNULL(doctor_name, ‘Unknown’) +
‘|VisitDate:’ + ISNULL(CAST(visit_date AS VARCHAR), ‘Unknown’) +
‘ || ‘)
INTO _data
FROM (
SELECT * FROM patient_records ORDER BY record_id
) AS sample_records;
ELSE
SET _data = ‘No medical records table found’;
END IF;
— Clean the user question for JSON (escape quotes and newlines)
SET @clean_question = REPLACE(REPLACE(REPLACE(REPLACE(
user_question + ‘ [Database Context: ‘ + _data + ‘]’,
‘”‘, ‘\”‘), CHAR(10), ‘\n’), CHAR(13), ‘\r’), CHAR(9), ‘\t’);
— Build complete JSON payload directly
SET _payload = ‘{“messages”:[{“role”:”user”,”content”:”‘ + @clean_question + ‘”}]}’;
SELECT c2 INTO _response
FROM (AIPOST(@json_payload))
WHERE c1 LIKE ‘%Body%’;
— Parse the JSON and extract the content
CALL sp_parse_json(‘ai_parsed’, _response);
   — Display the response
   SELECT ai_parsed.choices[[1]].[message].[content]; 
END;
call ask_ai_redact(‘Replace sensitive personal data with placeholder values like [REDACTED_NAME]. Return the redacted data in the same format. Also provide an alternative treatment plan for each patient.’); The response confirms that all sensitive data has been redacted, and a recommended treatment plan is provided. Additionally, we can develop auxiliary stored procedures to simulate context windows, preserving chat history like chat-based AI models like ChatGPT. We can also create procedures that leverage LLMs for data usage predictions, schema analysis, and more. This feature allows us to fully capitalize on the immense potential of both the database and the LLMs. We hope you find this feature valuable and enjoy the numerous opportunities it brings for enhanced database management and analytics! Read More Technology Blog Posts by SAP articles
#SAP
#SAPTechnologyblog
 
                                     
                                     
                                     
                                     
                             
                             
                                                 
                                                 
                                                 
                                                
