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. SQL to get DataTypes

SQL to get DataTypes

Scheduled Pinned Locked Moved Database
databasequestion
11 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.
  • R Offline
    R Offline
    RichardBerry
    wrote on last edited by
    #1

    Hi Is it possible to write a query that will return the datatype of each field as opposed to actual data? tks Richard

    C P J 3 Replies Last reply
    0
    • R RichardBerry

      Hi Is it possible to write a query that will return the datatype of each field as opposed to actual data? tks Richard

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Look at the INFORMATION_SCHEMA.COLUMNS to find the data type of columns in the database. (Assums SQL Server - You didn't say, but it is the most common database used on this forum)


      Upcoming FREE developer events: * Glasgow: db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website

      R 1 Reply Last reply
      0
      • R RichardBerry

        Hi Is it possible to write a query that will return the datatype of each field as opposed to actual data? tks Richard

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        I use ExecuteReader and then ask the DataReader for the DataTypes. DataReader.GetSchemaTable() or DataReader.GetFieldType()

        1 Reply Last reply
        0
        • R RichardBerry

          Hi Is it possible to write a query that will return the datatype of each field as opposed to actual data? tks Richard

          J Offline
          J Offline
          John ph
          wrote on last edited by
          #4

          RichardBerry wrote:

          Is it possible to write a query that will return the datatype of each field

          In sql Server, there is a system stored procedure (sp_columns) which returns column information for the specified table or view. You can execute that to get the DataType of the columns.

          Exec sp_columns [@TableName]
          

          Regards
          J O H N :rose:
          "Even eagles need a push." David McNally


          R 1 Reply Last reply
          0
          • C Colin Angus Mackay

            Look at the INFORMATION_SCHEMA.COLUMNS to find the data type of columns in the database. (Assums SQL Server - You didn't say, but it is the most common database used on this forum)


            Upcoming FREE developer events: * Glasgow: db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website

            R Offline
            R Offline
            RichardBerry
            wrote on last edited by
            #5

            Hi Colin Yes Using SQL server, but how do I do this? I looked, but don't seem to find a table with this name (INFORMATION_SCHEMA.COLUMNS)?

            C 1 Reply Last reply
            0
            • J John ph

              RichardBerry wrote:

              Is it possible to write a query that will return the datatype of each field

              In sql Server, there is a system stored procedure (sp_columns) which returns column information for the specified table or view. You can execute that to get the DataType of the columns.

              Exec sp_columns [@TableName]
              

              Regards
              J O H N :rose:
              "Even eagles need a push." David McNally


              R Offline
              R Offline
              RichardBerry
              wrote on last edited by
              #6

              Hi John Thanks for your reply. I tried running that stored procedure from MS Query, and get the following fields returned,but with no data. TABLE_QUALIFIER, TABLE_OWNER, TABLE_NAME, COLUMN_NAME, DATA_TYPE..... Etc It would actually be great if I could get all table data for the database in the above format. When executing 'Exec sp_columns [@TableName]' I tried replacing 'TableName' with various strings but cant seem to get any data back. I tried *, vektron.scheme.units, scheme.units, units. But none seemed to work. Typically to execute a normal Select query, I would use: SELECT * FROM vektron.scheme.units Any idea what I could be doing wrong?

              J 1 Reply Last reply
              0
              • R RichardBerry

                Hi John Thanks for your reply. I tried running that stored procedure from MS Query, and get the following fields returned,but with no data. TABLE_QUALIFIER, TABLE_OWNER, TABLE_NAME, COLUMN_NAME, DATA_TYPE..... Etc It would actually be great if I could get all table data for the database in the above format. When executing 'Exec sp_columns [@TableName]' I tried replacing 'TableName' with various strings but cant seem to get any data back. I tried *, vektron.scheme.units, scheme.units, units. But none seemed to work. Typically to execute a normal Select query, I would use: SELECT * FROM vektron.scheme.units Any idea what I could be doing wrong?

                J Offline
                J Offline
                John ph
                wrote on last edited by
                #7

                RichardBerry wrote:

                I tried running that stored procedure from MS Query, and get the following fields returned,but with no data.

                return's no data? How come? It works fine in my system... I have a table with the name tblProducts and I executed the stored proc as below...It return's the complete information... Exec sp_columns [tblProducts]

                Regards
                J O H N :rose:
                "Even eagles need a push." David McNally


                R 1 Reply Last reply
                0
                • J John ph

                  RichardBerry wrote:

                  I tried running that stored procedure from MS Query, and get the following fields returned,but with no data.

                  return's no data? How come? It works fine in my system... I have a table with the name tblProducts and I executed the stored proc as below...It return's the complete information... Exec sp_columns [tblProducts]

                  Regards
                  J O H N :rose:
                  "Even eagles need a push." David McNally


                  R Offline
                  R Offline
                  RichardBerry
                  wrote on last edited by
                  #8

                  Hi John Got it right - my syntax was wrong - I was putting in the '@' before the table name. BTW, is there a way to get more the info on all tables at once? I tried Exec sp_columns [*] but that gave no data back? Thanks for your help!!

                  J 1 Reply Last reply
                  0
                  • R RichardBerry

                    Hi John Got it right - my syntax was wrong - I was putting in the '@' before the table name. BTW, is there a way to get more the info on all tables at once? I tried Exec sp_columns [*] but that gave no data back? Thanks for your help!!

                    J Offline
                    J Offline
                    John ph
                    wrote on last edited by
                    #9

                    RichardBerry wrote:

                    BTW, is there a way to get more the info on all tables at once?

                    EXEC sp_tables will list all the tables in the current Database

                    Regards
                    J O H N :rose:
                    "Even eagles need a push." David McNally


                    1 Reply Last reply
                    0
                    • R RichardBerry

                      Hi Colin Yes Using SQL server, but how do I do this? I looked, but don't seem to find a table with this name (INFORMATION_SCHEMA.COLUMNS)?

                      C Offline
                      C Offline
                      Colin Angus Mackay
                      wrote on last edited by
                      #10

                      RichardBerry wrote:

                      Yes Using SQL server, but how do I do this? I looked, but don't seem to find a table with this name (INFORMATION_SCHEMA.COLUMNS)?

                      It is a view, not a table. It is on the Master database and will be picked up regardless of which database you are in. I'm also curious, did it not occur to you just to type SELECT * FROM INFORMATION_SCHEMA.COLUMNS or search for references to it on the internet?


                      Upcoming FREE developer events: * Glasgow: db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website

                      R 1 Reply Last reply
                      0
                      • C Colin Angus Mackay

                        RichardBerry wrote:

                        Yes Using SQL server, but how do I do this? I looked, but don't seem to find a table with this name (INFORMATION_SCHEMA.COLUMNS)?

                        It is a view, not a table. It is on the Master database and will be picked up regardless of which database you are in. I'm also curious, did it not occur to you just to type SELECT * FROM INFORMATION_SCHEMA.COLUMNS or search for references to it on the internet?


                        Upcoming FREE developer events: * Glasgow: db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website

                        R Offline
                        R Offline
                        RichardBerry
                        wrote on last edited by
                        #11

                        Hi Colin Thanks that was exactly what I was looking for.

                        Colin Angus Mackay wrote:

                        I'm also curious, did it not occur to you just to type SELECT * FROM INFORMATION_SCHEMA.COLUMNS or search for references to it on the internet?

                        No it did not occur to me, since I looked through all the databases on the server, and could not find a table with that name, so I thought perhaps this was a table that was not in my database. I had never heard of a 'view' before, so I am currently GOOGLING 'SQL View' to learn more about that. It is sometimes difficult for a NOVICE (myself) to ask the right questions or look in the right places for information.

                        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