Skip to main content
Participating Frequently
May 20, 2025
Answered

Retreiving SQL-Server database information

  • May 20, 2025
  • 1 reply
  • 323 views

With "myQuery.ColumnList", I get the list of columns in a database query.
Using GetMetaData(), I can then read the data type of each column using typeName.
Is there a way to get additional information, such as the length of varchar data types?

    Correct answer BKBK

    You can use cfdbinfo. I shall now give you a quick proof-of-concept.

    You can improve it using the struct or array functions.

    <cfdbinfo 
    type="columns" 
    table="myTable"
    datasource="myDSN" 
    name="tableColumnData"> 
    
    <!--- Dump the properties of all the columns of myTable. 'tableColumnData' is a query object. --->
    <!---<cfdump var="#tableColumnData#">--->
    
    <!---Get the size of the column named 'myCol'--->
    <cfloop index="i" from="1" to="#tableColumnData.recordcount#">
    	<cfset columnSize = tableColumnData["column_name"][i]=="myCol" ? tableColumnData["column_size"][i] : "Column not found or column-size cannot be determined.">
    	<cfif tableColumnData["column_name"][i]=="myCol">
    		<cfbreak>
    	</cfif>
    </cfloop>
    
    Size of column myCol:<cfoutput> #columnSize#</cfoutput>

    1 reply

    BKBK
    Community Expert
    BKBKCommunity ExpertCorrect answer
    Community Expert
    May 21, 2025

    You can use cfdbinfo. I shall now give you a quick proof-of-concept.

    You can improve it using the struct or array functions.

    <cfdbinfo 
    type="columns" 
    table="myTable"
    datasource="myDSN" 
    name="tableColumnData"> 
    
    <!--- Dump the properties of all the columns of myTable. 'tableColumnData' is a query object. --->
    <!---<cfdump var="#tableColumnData#">--->
    
    <!---Get the size of the column named 'myCol'--->
    <cfloop index="i" from="1" to="#tableColumnData.recordcount#">
    	<cfset columnSize = tableColumnData["column_name"][i]=="myCol" ? tableColumnData["column_size"][i] : "Column not found or column-size cannot be determined.">
    	<cfif tableColumnData["column_name"][i]=="myCol">
    		<cfbreak>
    	</cfif>
    </cfloop>
    
    Size of column myCol:<cfoutput> #columnSize#</cfoutput>
    PretorianAuthor
    Participating Frequently
    May 22, 2025

    Sometimes it's that simple.
    With "cfdbinfo," of course.
    Thanks for the tip.

    BKBK
    Community Expert
    Community Expert
    May 22, 2025

    My pleasure.