#1 Fri 19 January 2024 15:28
- BadWolf
- Participant occasionnel
- Date d'inscription: 27 Jun 2019
- Messages: 35
Requete porstgresql appelant une table qui n'existe pas.
Bonjour à tous,
Je vous soliciste pour savoir si il est possible d'exécuter une requête porstgresql en appelant une table qui n'existe pas. Je ne veux pas de message d'erreur qui bloque le resultat. A la limite un message d'information.
Est ce possible ?
Je vous en remercie.
Hors ligne
#2 Fri 19 January 2024 15:54
- Nicolas Ribot
- Membre
- Lieu: Toulouse
- Date d'inscription: 9 Sep 2005
- Messages: 1566
Re: Requete porstgresql appelant une table qui n'existe pas.
Bonjour,
Non pas possible.
Mais vous pouvez faire une fonction ou un bloc anonyme plpgsl qui n'execute une requete SQL qu'après avoir vérifié si la table existe.
C'est en général ce qu'on fait dans ce cas.
Nicolas
Hors ligne
#3 Fri 19 January 2024 16:14
- BadWolf
- Participant occasionnel
- Date d'inscription: 27 Jun 2019
- Messages: 35
Re: Requete porstgresql appelant une table qui n'existe pas.
Bonjour
Merci pour ta reponse mais comment fait on ca ? On peut l'intégrer a une requête avec par exemple une fonction ou c'est apart ?
Hors ligne
#4 Fri 19 January 2024 16:39
- Nicolas Ribot
- Membre
- Lieu: Toulouse
- Date d'inscription: 9 Sep 2005
- Messages: 1566
Re: Requete porstgresql appelant une table qui n'existe pas.
Oui s'il faut l'intégrer à une requete, on peut faire un fonction et ensuite l'appeler avec:
Code:
select * from fonction();
ref: https://www.postgresql.org/docs/16/plpgsql.html
Nicolas
Hors ligne
#5 Sat 20 January 2024 08:16
- ChristopheV
- Membre
- Lieu: Ajaccio
- Date d'inscription: 7 Sep 2005
- Messages: 3224
- Site web
Re: Requete porstgresql appelant une table qui n'existe pas.
Bonjour,
Je vous donne ci-dessous une partie de mon code.
Vous trouverez les instructions SQL pour vérifier l'existence d'une BD, d'un schéma, d'une table, et l'on peut faire de même avec les colonnes, index, pk, fk ......
Si en plus vous respectez une convention de nommage, vous pouvez faire du traitement automatique.
Exemple pour toute table :
pk = "id_" & nomtable
gix_nomtable = index spatial
idx_nomtable = index btree
Code:
Protected Function Database_Exist() As Boolean
m_cmd = New Npgsql.NpgsqlCommand
m_cmd.CommandText = "SELECT COUNT(*) FROM pg_catalog.pg_database WHERE datname = :database_name"
m_cmd.Connection = mPostGisCnn
m_cmd.Parameters.Add(New Npgsql.NpgsqlParameter(":database_name", mNomBase))
Return m_cmd.ExecuteScalar > 0
m_cmd.Dispose()
End Function
Protected Function schema_exists(schemaname As String) As Boolean
Dim ds As New DataSet
m_cmd = New NpgsqlCommand
m_cmd.Connection = mPostGisCnn
m_cmd.CommandText = "SELECT schema_name FROM information_schema.schemata WHERE schema_name='" & schemaname & "';"
m_da = New NpgsqlDataAdapter
m_da.SelectCommand = m_cmd
m_da.Fill(ds)
m_da.Dispose()
m_cmd.Dispose()
If ds.Tables(0).Rows.Count > 0 Then
Return True
Else
Return False
End If
End Function
Protected Sub Create_schema(schemaname As String)
If schema_exists(schemaname) Then
Exit Sub
End If
If schemaname <> "public" Then
m_cmd = New NpgsqlCommand
m_cmd.Connection = mPostGisCnn
m_cmd.CommandText = "CREATE SCHEMA " & schemaname & ";"
m_cmd.ExecuteNonQuery()
End If
End Sub
Protected Function table_exists(nomschema As String, nomtable As String) As Boolean
Dim ds As New DataSet
m_cmd = New NpgsqlCommand
m_cmd.Connection = mPostGisCnn
m_cmd.CommandText = "select table_name from information_schema.tables WHERE table_schema=:p AND table_name=:p1;"
Dim p As New NpgsqlParameter("p", nomschema)
m_cmd.Parameters.Add(p)
Dim p1 As New NpgsqlParameter("p1", nomtable)
m_cmd.Parameters.Add(p1)
m_da = New NpgsqlDataAdapter
m_da.SelectCommand = m_cmd
m_da.Fill(ds)
m_da.Dispose()
m_cmd.Dispose()
If ds.Tables(0).Rows.Count > 0 Then
Return True
Else
Return False
End If
End FunctionChristophe
L'avantage d'être une île c'est d'être une terre topologiquement close
Hors ligne


