Skill 3.3.2
Create a prompt by using the sp_invoke_external_rest_endpoint stored procedure
Key Concepts
A chat completions request contains a messages array: a system message with instructions (answer only from the provided data, cite IDs, say when the answer is not present), and a user message with the question and the retrieved rows. The body is built with JSON_OBJECT and JSON_ARRAY so values are escaped correctly, never by string concatenation of raw text, and includes settings such as max_completion_tokens or a response_format JSON schema.
EXEC @ret = sp_invoke_external_rest_endpoint @url = N'https://<resource>.openai.azure.com/openai/v1/chat/completions', @method = 'POST', @payload = @payload, @credential = [https://<resource>.openai.azure.com], @timeout = 120, @response = @response OUTPUT sends the request. The caller needs EXECUTE ANY EXTERNAL ENDPOINT and REFERENCES on the credential, the URL must be on the allow list in Azure SQL Database, and @retry_count retries 408, 429 and 5xx responses. The default timeout is 30 seconds, which may be too short for long answers.