Q: Drop table and check if table exists

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Geoff Jones

    #1

    Q: Drop table and check if table exists

    Hi

    I'm trying to drop a table by using:

    Dim cmd As New OleDbCommand("D ROP TABLE IF EXISTS books", myconnection)

    cmd.ExecuteNonQ uery()

    but I get a syntax error: "Syntax error in DROP TABLE or DROP INDEX"

    Can anybody tell me how to check whether a table exists before deleting or
    dropping it?

    Also, could somebody tell me how to create a new table with the same
    structure as an existing table but not the data within it (within VB.net
    that is - I can do it in Access but I need to do it in VB code).

    Thanks in advance

    Geoff


  • Cor Ligthert

    #2
    Re: Drop table and check if table exists

    Hi Geoff,

    Did I not send that code in a message of yours?

    However when that drops works than you do not need that in my opinion.

    Cor



    Comment

    • Geoff Jones

      #3
      Re: Drop table and check if table exists

      Hi Cor

      No, you did send a link with details about DROP TABLE but that didn't cover
      any details as to whether the table existed.

      Geoff

      "Cor Ligthert" <notfirstname@p lanet.nl> wrote in message
      news:uSnhXSWdEH A.3560@TK2MSFTN GP10.phx.gbl...[color=blue]
      > Hi Geoff,
      >
      > Did I not send that code in a message of yours?
      >
      > However when that drops works than you do not need that in my opinion.
      >
      > Cor
      >
      >
      >[/color]


      Comment

      • Cor Ligthert

        #4
        Re: Drop table and check if table exists

        Geoff,

        See this message from Ken for your first question
        http://www.google.com/groups?selm=ea$ qP6mcEHA.716@TK 2MSFTNGP11.phx. gbl

        And this from me for your second
        http://www.google.com/groups?threadm= 41064d3b$0$2696 3$cc9e4d1f@news .dial.pipex.com

        I hope this helps?

        Cor



        Comment

        • Geoff Jones

          #5
          Re: Drop table and check if table exists

          Sorry Cor, I don't follow you. The link you give is to create a table rather
          than a way to find out if a table already exists.

          Am I missing something obvious?

          Geoff

          "Cor Ligthert" <notfirstname@p lanet.nl> wrote in message
          news:uyfGnbXdEH A.1184@TK2MSFTN GP11.phx.gbl...[color=blue]
          > Geoff,
          >
          > See this message from Ken for your first question
          > http://www.google.com/groups?selm=ea$ qP6mcEHA.716@TK 2MSFTNGP11.phx. gbl
          >
          > And this from me for your second
          >[/color]
          http://www.google.com/groups?threadm= 41064d3b$0$2696 3$cc9e4d1f@news .dial.pipex.com[color=blue]
          >
          > I hope this helps?
          >
          > Cor
          >
          >
          >[/color]


          Comment

          • Cor Ligthert

            #6
            Re: Drop table and check if table exists

            > Sorry Cor, I don't follow you. The link you give is to create a table
            rather[color=blue]
            > than a way to find out if a table already exists.
            >
            > Am I missing something obvious?
            >[/color]
            Using the code from Ken (the first link) you can see if a table exist.
            Where is that debug.write(... ..)
            You can make someting as

            For Each dr In dtTableNames.Ro ws
            If dr("TABLE_NAME" ).ToString = "MyTable" Then
            cmd.CommandText = "DROP TABLE MyTable"
            cmd.ExecuteNonQ uery()
            exit for
            End If
            Next

            I hope this helps?

            Cor


            Comment

            • Geoff Jones

              #7
              Re: Drop table and check if table exists

              Hi Cor

              Ah, I see what you mean. However, do I take it that there is no IF EXISTS
              command that I could use in my original idea i.e.

              ' Drop the table books if it exists
              Dim cmd As New OleDbCommand("D ROP TABLE IF EXISTS books", myconnection)
              cmd.ExecuteNonQ uery()

              As far as I can see this is a valid SQL statement.

              Geoff

              "Cor Ligthert" <notfirstname@p lanet.nl> wrote in message
              news:euW$frZdEH A.3792@TK2MSFTN GP09.phx.gbl...[color=blue][color=green]
              > > Sorry Cor, I don't follow you. The link you give is to create a table[/color]
              > rather[color=green]
              > > than a way to find out if a table already exists.
              > >
              > > Am I missing something obvious?
              > >[/color]
              > Using the code from Ken (the first link) you can see if a table exist.
              > Where is that debug.write(... ..)
              > You can make someting as
              >
              > For Each dr In dtTableNames.Ro ws
              > If dr("TABLE_NAME" ).ToString = "MyTable" Then
              > cmd.CommandText = "DROP TABLE MyTable"
              > cmd.ExecuteNonQ uery()
              > exit for
              > End If
              > Next
              >
              > I hope this helps?
              >
              > Cor
              >
              >[/color]


              Comment

              • Cor Ligthert

                #8
                Re: Drop table and check if table exists

                Hi Geoff,

                I see not any documentation to it related to AdoNet, however it gives with
                me as well errors

                You can try it maybe in the AdoNet newsgroup, you cannot be the first one
                with this problem. I always try to avoid SQL because I hate that always
                errors giving scripting language.

                Adonet
                <news://msnews.microsof t.com/microsoft.publi c.dotnet.framew ork.adonet>

                Web interface:
                <http://communities2.microsoft.com/co...s/?dg=microsof
                t.public.dotnet .framework.adon et>

                Cor
                [color=blue]
                > Hi Cor
                >
                > Ah, I see what you mean. However, do I take it that there is no IF EXISTS
                > command that I could use in my original idea i.e.
                >
                > ' Drop the table books if it exists
                > Dim cmd As New OleDbCommand("D ROP TABLE IF EXISTS books", myconnection)
                > cmd.ExecuteNonQ uery()
                >
                > As far as I can see this is a valid SQL statement.
                >
                > Geoff
                >
                > "Cor Ligthert" <notfirstname@p lanet.nl> wrote in message
                > news:euW$frZdEH A.3792@TK2MSFTN GP09.phx.gbl...[color=green][color=darkred]
                > > > Sorry Cor, I don't follow you. The link you give is to create a table[/color]
                > > rather[color=darkred]
                > > > than a way to find out if a table already exists.
                > > >
                > > > Am I missing something obvious?
                > > >[/color]
                > > Using the code from Ken (the first link) you can see if a table exist.
                > > Where is that debug.write(... ..)
                > > You can make someting as
                > >
                > > For Each dr In dtTableNames.Ro ws
                > > If dr("TABLE_NAME" ).ToString = "MyTable" Then
                > > cmd.CommandText = "DROP TABLE MyTable"
                > > cmd.ExecuteNonQ uery()
                > > exit for
                > > End If
                > > Next
                > >
                > > I hope this helps?
                > >
                > > Cor
                > >
                > >[/color]
                >
                >[/color]


                Comment

                • Geoff Jones

                  #9
                  Re: Drop table and check if table exists

                  Hi Cor

                  Many thanks again for your help.

                  Yes, this is a bit strange. I would have thought it an essential command.
                  However, as with so many things, it doesn't appear to be.

                  Thanks again for your help. I'll try a post to the ADO.NET group as you
                  suggest.

                  Geoff

                  "Cor Ligthert" <notfirstname@p lanet.nl> wrote in message
                  news:%23GQntthd EHA.3392@tk2msf tngp13.phx.gbl. ..[color=blue]
                  > Hi Geoff,
                  >
                  > I see not any documentation to it related to AdoNet, however it gives with
                  > me as well errors
                  >
                  > You can try it maybe in the AdoNet newsgroup, you cannot be the first one
                  > with this problem. I always try to avoid SQL because I hate that always
                  > errors giving scripting language.
                  >
                  > Adonet
                  > <news://msnews.microsof t.com/microsoft.publi c.dotnet.framew ork.adonet>
                  >
                  > Web interface:
                  >[/color]
                  <http://communities2.microsoft.com/co...s/?dg=microsof[color=blue]
                  > t.public.dotnet .framework.adon et>
                  >
                  > Cor
                  >[color=green]
                  > > Hi Cor
                  > >
                  > > Ah, I see what you mean. However, do I take it that there is no IF[/color][/color]
                  EXISTS[color=blue][color=green]
                  > > command that I could use in my original idea i.e.
                  > >
                  > > ' Drop the table books if it exists
                  > > Dim cmd As New OleDbCommand("D ROP TABLE IF EXISTS books", myconnection)
                  > > cmd.ExecuteNonQ uery()
                  > >
                  > > As far as I can see this is a valid SQL statement.
                  > >
                  > > Geoff
                  > >
                  > > "Cor Ligthert" <notfirstname@p lanet.nl> wrote in message
                  > > news:euW$frZdEH A.3792@TK2MSFTN GP09.phx.gbl...[color=darkred]
                  > > > > Sorry Cor, I don't follow you. The link you give is to create a[/color][/color][/color]
                  table[color=blue][color=green][color=darkred]
                  > > > rather
                  > > > > than a way to find out if a table already exists.
                  > > > >
                  > > > > Am I missing something obvious?
                  > > > >
                  > > > Using the code from Ken (the first link) you can see if a table exist.
                  > > > Where is that debug.write(... ..)
                  > > > You can make someting as
                  > > >
                  > > > For Each dr In dtTableNames.Ro ws
                  > > > If dr("TABLE_NAME" ).ToString = "MyTable" Then
                  > > > cmd.CommandText = "DROP TABLE MyTable"
                  > > > cmd.ExecuteNonQ uery()
                  > > > exit for
                  > > > End If
                  > > > Next
                  > > >
                  > > > I hope this helps?
                  > > >
                  > > > Cor
                  > > >
                  > > >[/color]
                  > >
                  > >[/color]
                  >
                  >[/color]


                  Comment

                  • Paul Clement

                    #10
                    Re: Drop table and check if table exists

                    On Fri, 30 Jul 2004 10:29:20 +0100, "Geoff Jones" <geoff@NODAMNSP AM.com> wrote:

                    ¤ Hi Cor
                    ¤
                    ¤ Ah, I see what you mean. However, do I take it that there is no IF EXISTS
                    ¤ command that I could use in my original idea i.e.
                    ¤
                    ¤ ' Drop the table books if it exists
                    ¤ Dim cmd As New OleDbCommand("D ROP TABLE IF EXISTS books", myconnection)
                    ¤ cmd.ExecuteNonQ uery()
                    ¤
                    ¤ As far as I can see this is a valid SQL statement.

                    No, the SQL statement EXISTS will not work in this manner. You could either trap the exception that
                    occurs when executing the DROP statement or use GetOleDbSchemaT able:

                    Dim DatabaseConnect ion As New System.Data.Ole Db.OleDbConnect ion
                    Dim SchemaTable As DataTable

                    DatabaseConnect ion.ConnectionS tring = "Provider=Micro soft.Jet.OLEDB. 4.0;" & _
                    "Data Source=E:\My Documents\db1.m db"

                    DatabaseConnect ion.Open()

                    SchemaTable =
                    DatabaseConnect ion.GetOleDbSch emaTable(System .Data.OleDb.Ole DbSchemaGuid.Co lumns, _
                    New Object() {Nothing, Nothing, "books"})

                    If SchemaTable.Row s.Count <> 0 Then
                    'table exists
                    End If

                    DatabaseConnect ion.Close()


                    Paul ~~~ pclement@amerit ech.net
                    Microsoft MVP (Visual Basic)

                    Comment

                    • Geoff Jones

                      #11
                      Re: Drop table and check if table exists

                      Many thanks Paul

                      "Paul Clement" <UseAdddressAtE ndofMessage@sws pectrum.com> wrote in message
                      news:3nlkg0hfqs depfutu5o32nr79 b5qjvvla4@4ax.c om...[color=blue]
                      > On Fri, 30 Jul 2004 10:29:20 +0100, "Geoff Jones" <geoff@NODAMNSP AM.com>[/color]
                      wrote:[color=blue]
                      >
                      > ¤ Hi Cor
                      > ¤
                      > ¤ Ah, I see what you mean. However, do I take it that there is no IF[/color]
                      EXISTS[color=blue]
                      > ¤ command that I could use in my original idea i.e.
                      > ¤
                      > ¤ ' Drop the table books if it exists
                      > ¤ Dim cmd As New OleDbCommand("D ROP TABLE IF EXISTS books", myconnection)
                      > ¤ cmd.ExecuteNonQ uery()
                      > ¤
                      > ¤ As far as I can see this is a valid SQL statement.
                      >
                      > No, the SQL statement EXISTS will not work in this manner. You could[/color]
                      either trap the exception that[color=blue]
                      > occurs when executing the DROP statement or use GetOleDbSchemaT able:
                      >
                      > Dim DatabaseConnect ion As New System.Data.Ole Db.OleDbConnect ion
                      > Dim SchemaTable As DataTable
                      >
                      > DatabaseConnect ion.ConnectionS tring =[/color]
                      "Provider=Micro soft.Jet.OLEDB. 4.0;" & _[color=blue]
                      > "Data Source=E:\My Documents\db1.m db"
                      >
                      > DatabaseConnect ion.Open()
                      >
                      > SchemaTable =
                      >[/color]
                      DatabaseConnect ion.GetOleDbSch emaTable(System .Data.OleDb.Ole DbSchemaGuid.Co l
                      umns, _[color=blue]
                      > New Object() {Nothing, Nothing, "books"})
                      >
                      > If SchemaTable.Row s.Count <> 0 Then
                      > 'table exists
                      > End If
                      >
                      > DatabaseConnect ion.Close()
                      >
                      >
                      > Paul ~~~ pclement@amerit ech.net
                      > Microsoft MVP (Visual Basic)[/color]


                      Comment

                      Working...