Advent of 2025, Day 11 – SQL Server 2025 – External REST endpoint invocation using LLM

In this Microsoft SQL Server 2025 series:

  1. Dec 01: Microsoft SQL Server 2025 – Introduction and installation
  2. Dec 02: Microsoft SQL Server 2025 – New T-SQL functions – native JSON data type and some functions
  3. Dec 03: Microsoft SQL Server 2025 – New T-SQL functions – JSON Index
  4. Dec 04: Microsoft SQL Server 2025 – New T-SQL functions – Product()
  5. Dec 05: Microsoft SQL Server 2025 – New T-SQL functions – BASE64_ENCODE() and BASE64_DECODE()
  6. Dec 06: Microsoft SQL Server 2025 – New T-SQL functions – REGEXP_LIKE()
  7. Dec 07: Microsoft SQL Server 2025 – New T-SQL functions – REGEXP_SUBSTR() and REGEXP_REPLACE()
  8. Dec 08: Microsoft SQL Server 2025 – New T-SQL functions – REGEXP_INSTR() and REGEXP_COUNT()
  9. Dec 09: Microsoft SQL Server 2025 – New T-SQL functions – REGEXP_MATCHES() and REGEXP_SPLIT_TO_TABLE()
  10. 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!

Tagged with: , , , , , , ,
Posted in Azure AI, Azure Machine Learning, SQL Server, Uncategorized
13 comments on “Advent of 2025, Day 11 – SQL Server 2025 – External REST endpoint invocation using LLM
  1. […] Dec 11: Microsoft SQL Server 2025 – External REST endpoint invocation using LLM […]

    Like

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

    Like

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

    Like

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

    Like

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

    Like

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

    Like

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

    Like

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

    Like

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

    Like

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

    Like

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

    Like

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

    Like

Leave a comment

Follow TomazTsql on WordPress.com
Programs I Use: SQL Search
Programs I Use: R Studio
Programs I Use: Plan Explorer
Rdeči Noski – Charity

Rdeči noski

100% of donations made here go to charity, no deductions, no fees. For CLOWNDOCTORS - encouraging more joy and happiness to children staying in hospitals (http://www.rednoses.eu/red-noses-organisations/slovenia/)

€2.00

Top SQL Server Bloggers 2018
TomazTsql

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Discover WordPress

A daily selection of the best content published on WordPress, collected for you by humans who love to read.

Revolutions

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Reeves Smith's SQL & BI Blog

A blog about SQL Server and the Microsoft Business Intelligence stack with some random Non-Microsoft tools thrown in for good measure.

SQL Server

for Application Developers

Business Analytics 3.0

Data Driven Business Models

SQL Database Engine Blog

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Search Msdn

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

R-bloggers

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Data Until I Die!

Data for Life :)

Paul Turley's SQL Server BI Blog

sharing my experiences with the Microsoft data platform, Fabric, enterprise Power BI, SQL Server BI, Data Modeling, SSAS Design, SSRS, Dashboards & Visualization since 2009

Grant Fritchey

Intimidating Databases and Code

Madhivanan's SQL blog

A modern business theme

Alessandro Alpi's Blog

DevOps could be the disease you die with, but don’t die of.

Paul te Braak

Business Intelligence Blog

Sql Insane Asylum (A Blog by Pat Wright)

Information about SQL (PostgreSQL & SQL Server) from the Asylum.

Gareth's Blog

A blog about Life, SQL & Everything ...

SQLPam's Blog

Life changes fast and this is where I occasionally take time to ponder what I have learned and experienced. A lot of focus will be on SQL and the SQL community – but life varies.

William Durkin

William Durkin a blog on SQL Server, Replication, Performance Tuning and whatever else.

$hell Your Experience !!!

As aventuras de um DBA usando o Poder do $hell

Design a site like this with WordPress.com
Get started