kulltc/chatgpt-sql

Dynamic list of tables

Opened this issue · 0 comments

Hi! I saw your comment at the startMessageStack on chatgpt.py about dynamic tables.
However my python is rusty and I don't want to break anything, so I didn't create a PR.

I am doing that kind of thing in an Sql bot, by teaching gpt to search for the database schema using:

-- List table names
SELECT name FROM SYSOBJECTS WHERE xtype = 'U' ORDER BY name;

and then table schema by using:

-- List column names for table 'Aluno'
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Aluno';

also, for some columns that are enums and need additional context, I use extended properties, so I can append some context to my schema directly on the DB, and query it as such:

-- Search meaning of 'ColumnName" field of  "TableName' table.
SELECT
    ep.value AS ExtendedPropertyValue
FROM
    sys.tables t
INNER JOIN
    sys.columns c ON t.object_id = c.object_id
INNER JOIN
    sys.extended_properties ep ON ep.major_id = c.object_id AND ep.minor_id = c.column_id
WHERE
    t.name = 'TableName' AND c.name = 'ColumnName';

This is a part of my own "startMessageStack", as an example:

ChatMessage.FromAssistant(@"[EXEC]
-- Buscar significado do campo Status da tabela Redacao.
SELECT
    ep.value AS ExtendedPropertyValue
FROM
    sys.tables t
INNER JOIN
    sys.columns c ON t.object_id = c.object_id
INNER JOIN
    sys.extended_properties ep ON ep.major_id = c.object_id AND ep.minor_id = c.column_id
WHERE
    t.name = 'Redacao' AND c.name = 'Status';"),
	
ChatMessage.FromSystem(@"ExtendedPropertyValue
Salvo = 0,
AguardandoCorrecao = 1
EmCorrecao = 2
Corrigido = 3
ComProblema = 4
NaoEnviado = 5
EmAnalise = 6
AlunoNaoEncontrado = 7
Ilegivel = 8
EscaneadoErrado = 9
AguardandoPlagio = 10
ProvavelPlagio = 11
Plagio = 12");

Hope this is of any help ❤️