Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. How to query all of the tab_name in the same database

How to query all of the tab_name in the same database

Scheduled Pinned Locked Moved Database
databasec++helptutorial
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nibabug
    wrote on last edited by
    #1

    when my program run, i want to creat a new table, but i do not know if the name is exist. so, i want to check that if the name can be used or no, but i don't know how to query all the table names give me a help, please i am just starting lerning SQL and VC++ thanks a lot

    wuhuaiji

    K V G 3 Replies Last reply
    0
    • N nibabug

      when my program run, i want to creat a new table, but i do not know if the name is exist. so, i want to check that if the name can be used or no, but i don't know how to query all the table names give me a help, please i am just starting lerning SQL and VC++ thanks a lot

      wuhuaiji

      K Offline
      K Offline
      Krish KP
      wrote on last edited by
      #2

      IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'table_name') CREATE TABLE table_name ( .... "sysobjects" is the table which contains all objects created

      Regards KP

      1 Reply Last reply
      0
      • N nibabug

        when my program run, i want to creat a new table, but i do not know if the name is exist. so, i want to check that if the name can be used or no, but i don't know how to query all the table names give me a help, please i am just starting lerning SQL and VC++ thanks a lot

        wuhuaiji

        V Offline
        V Offline
        Virendrak
        wrote on last edited by
        #3

        IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' AND TABLE_NAME='Student') SELECT 'Student exists.' ELSE SELECT 'Student does not exist.' -- or SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='Student' -- or SELECT * FROM sysobjects WHERE name='Student' --OR IF OBJECT_ID('Student') IS NOT NULL PRINT 'Student exists.' ELSE PRINT 'Student does not exist.'

        Never Think That You Have Failed Instead Always Think That u hav Better Chance Next Time...

        1 Reply Last reply
        0
        • N nibabug

          when my program run, i want to creat a new table, but i do not know if the name is exist. so, i want to check that if the name can be used or no, but i don't know how to query all the table names give me a help, please i am just starting lerning SQL and VC++ thanks a lot

          wuhuaiji

          G Offline
          G Offline
          GuyThiebaut
          wrote on last edited by
          #4

          Hi, I created a function to check if a table exists, as I need to check for this sort of thing frequently. Here is the function code:

          CREATE function dbo.DoesObjectExist(@Name varchar(200), @Type char(1))
          returns varchar(7)
          
          begin
          declare @Result varchar(7)
          
          if exists (select * from sysobjects
          where name = @Name
          and xtype = @Type)
          	select @Result = 'present'
          else	
          	select @Result = ''
          
          return @Result
          end
          

          This is how you check if the OffVansQtyOrdered table exists using this function:

          if dbo.DoesObjectExist('OffVansQtyOrdered','u') = 'present'
          

          I hope this is of help...

          You always pass failure on the way to success.
          N 1 Reply Last reply
          0
          • G GuyThiebaut

            Hi, I created a function to check if a table exists, as I need to check for this sort of thing frequently. Here is the function code:

            CREATE function dbo.DoesObjectExist(@Name varchar(200), @Type char(1))
            returns varchar(7)
            
            begin
            declare @Result varchar(7)
            
            if exists (select * from sysobjects
            where name = @Name
            and xtype = @Type)
            	select @Result = 'present'
            else	
            	select @Result = ''
            
            return @Result
            end
            

            This is how you check if the OffVansQtyOrdered table exists using this function:

            if dbo.DoesObjectExist('OffVansQtyOrdered','u') = 'present'
            

            I hope this is of help...

            You always pass failure on the way to success.
            N Offline
            N Offline
            nibabug
            wrote on last edited by
            #5

            firstly thanks again, by the way,i am so sorry, that i do not really understand what you write above, and i do not know in VC++ , how to create a table, just like the question i write "a problem about m_pConnection->Execute", have you a good way to resolve it, or can can you tell me which problem exist in my program

            wuhuaiji

            G 1 Reply Last reply
            0
            • N nibabug

              firstly thanks again, by the way,i am so sorry, that i do not really understand what you write above, and i do not know in VC++ , how to create a table, just like the question i write "a problem about m_pConnection->Execute", have you a good way to resolve it, or can can you tell me which problem exist in my program

              wuhuaiji

              G Offline
              G Offline
              GuyThiebaut
              wrote on last edited by
              #6

              Hi, The first example is what is called a "user defined function". This is a means for SQL to process data based on parameters passed to the function and then return a value(or a table). In this case the function takes an object and object type as input. The object type, in the bottom piece of code, is "u" which means that the function only looks for tables. The advantage of a function is that you can create this code once and reuse it many times by just calling it with dbo.DoesObjectExist('tablename','u'). In terms of creating a SQL table in C++ I'm sorry to say that I cannot help you with that yet as I am learning C# at the moment. What I can recommend is google and searches with strings such as "create SQL table with C++" and "SQL create table". As a tool google is invaluable and has proved to be just this in my work - as there is always going to be an example somewhere out there of what you are trying to do. Good luck.

              You always pass failure on the way to success.
              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups