In this Microsoft SQL Server 2025 series:
- Dec 01: Microsoft SQL Server 2025 – Introduction and installation
- Dec 02: Microsoft SQL Server 2025 – New T-SQL functions – native JSON data type and some functions
- Dec 03: Microsoft SQL Server 2025 – New T-SQL functions – JSON Index
- Dec 04: Microsoft SQL Server 2025 – New T-SQL functions – Product()
- Dec 05: Microsoft SQL Server 2025 – New T-SQL functions – BASE64_ENCODE() and BASE64_DECODE()
- Dec 06: Microsoft SQL Server 2025 – New T-SQL functions – REGEXP_LIKE()
- Dec 07: Microsoft SQL Server 2025 – New T-SQL functions – REGEXP_SUBSTR() and REGEXP_REPLACE()
- Dec 08: Microsoft SQL Server 2025 – New T-SQL functions – REGEXP_INSTR() and REGEXP_COUNT()
- Dec 09: Microsoft SQL Server 2025 – New T-SQL functions – REGEXP_MATCHES() and REGEXP_SPLIT_TO_TABLE()
- Dec 10: Microsoft SQL Server 2025 – External REST endpoint invocation
After short introduction into the sp_invoke_external_rest_endpoint we will look into creating a REST endpoint for using LLM.
Deploy LLM model
In your Azure tenant, go to Azure AI Foundry and deploy a model of your choice.
Save the endpoint invocation and API key. This will become handy when defining the sp_invoke_external_rest_endpoint function.
You can use any other flavour of LLM, that you decide to choose. Azure Foundry is for the purpose of this blog-post handy and easy to deploy.
SQL Server 2025 prerequisites
We need to grant access to endpoint and create a master key on the database.
USE Master;
GO
GRANT EXECUTE ANY EXTERNAL ENDPOINT TO dbo;
GO
USE db_06_api;
GO
IF NOT EXISTS (SELECT 1 FROM sys.symmetric_keys WHERE name = '##MS_DatabaseMasterKey##')
BEGIN
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'Very_Super_Strong_P@ssw0rd1!';
END
GO
After setting up the master key we need to create database scope credential for connection to LLM.
IF NOT EXISTS (SELECT 1 FROM sys.database_scoped_credentials WHERE name = 'https://tomaz-m821w85v-eastus2.cognitiveservices.azure.com')
BEGIN
CREATE DATABASE SCOPED CREDENTIAL [https://tomaz-m821w85v-eastus2.cognitiveservices.azure.com]
WITH IDENTITY = 'HTTPEndpointHeaders',
SECRET = '{"api-key":"your_api_key_from_Azure_AI_foundry_api_key"}';
END
GO
This concludes all the necessary setup.
Calling LLM from SQL Server
We will also create a stored procedure to call the endpoint:
CREATE OR ALTER PROCEDURE dbo.sp_call_my_Foundry_LLM (
@Prompt VARCHAR(MAX),
@Fl_Debug BIT = 0
)
AS
BEGIN
-- Executa a API
DECLARE
@ret INT,
@response NVARCHAR(MAX),
@payload VARCHAR(MAX) = '{
"messages": [
{
"role": "system",
"content": [
{
"type": "text",
"text": "I am reading this blog. ' + @Prompt + '"
}
]
}
],
"temperature": 0.1,
"top_p": 0.1,
"max_tokens": 8000
}'
EXEC @ret = sys.sp_invoke_external_rest_endpoint
@method = 'POST',
@headers = '{"Content-Type":"application/json"}',
@url = N'https://tomaz-m821w85v-eastus2.cognitiveservices.azure.com/openai/deployments/gpt-4o/chat/completions?api-version=2025-01-01-preview',
@payload = @payload,
--@credential = [<OPENAI_URL>],
@credential = [https://tomaz-m821w85v-eastus2.cognitiveservices.azure.com],
@response = @response OUTPUT
-- PRINT @response
DECLARE @API_RETURN VARCHAR(MAX)
SET @API_RETURN = JSON_VALUE(@response, '$.result.choices[0].message.content')
SET @API_RETURN = REPLACE(REPLACE(@API_RETURN, '```sql', ''), '```', '')
PRINT @API_RETURN
IF (LEN(TRIM(@API_RETURN)) > 0 AND @Fl_Debug = 0)
EXEC(@API_RETURN)
END
Once we have the API and stored procedure create, we can call
-- EXECUTE API
EXEC dbo.sp_call_my_Foundry_LLM 'Where can I buy book on SQL Server 2025?'
Tomorrow we will look into the new vector data type, and how to use this data type. This will later be relevant also for using embeddings and LLM in SQL Server 2025.
As always, the code is available at my Github: https://github.com/tomaztk/SQLServer2025
Happy coding!





[…] Dec 11: Microsoft SQL Server 2025 – External REST endpoint invocation using LLM […]
LikeLike
[…] Dec 11: Microsoft SQL Server 2025 – External REST endpoint invocation using LLM […]
LikeLike
[…] Dec 11: Microsoft SQL Server 2025 – External REST endpoint invocation using LLM […]
LikeLike
[…] Dec 11: Microsoft SQL Server 2025 – External REST endpoint invocation using LLM […]
LikeLike
[…] Dec 11: Microsoft SQL Server 2025 – External REST endpoint invocation using LLM […]
LikeLike
[…] Dec 11: Microsoft SQL Server 2025 – External REST endpoint invocation using LLM […]
LikeLike
[…] Dec 11: Microsoft SQL Server 2025 – External REST endpoint invocation using LLM […]
LikeLike
[…] Dec 11: Microsoft SQL Server 2025 – External REST endpoint invocation using LLM […]
LikeLike
[…] Dec 11: Microsoft SQL Server 2025 – External REST endpoint invocation using LLM […]
LikeLike
[…] Dec 11: Microsoft SQL Server 2025 – External REST endpoint invocation using LLM […]
LikeLike
[…] Dec 11: Microsoft SQL Server 2025 – External REST endpoint invocation using LLM […]
LikeLike
[…] Dec 11: Microsoft SQL Server 2025 – External REST endpoint invocation using LLM […]
LikeLike
[…] Dec 11: Microsoft SQL Server 2025 – External REST endpoint invocation using LLM […]
LikeLike