Skip to main content
WolfShade
Legend
April 25, 2011
Question

CFPARAM many, many, many columns

  • April 25, 2011
  • 6 replies
  • 4595 views

Hello,

I no longer have the code I was working on - I got frustrated and deleted it.

I'm looking for a way to grab all the column names from a MS-SQL server database table, loop through them and CFPARAM them all depending upon the datatype (ie, "int,float" defaults to 0; "char, nchar, varchar, nvarchar" defaults to '', etc.

Any suggestions?

^_^

    This topic has been closed for replies.

    6 replies

    Inspiring
    April 29, 2011

    "
    over 125 database columns

    "

    Interesting problem, a generic tool might be a solution if such need is sort of frequent...

    I created such a tool back a few years ago, providing the ability of picking any user table for data view, manipulation (update) and search off MS SQL Server 2000/2005....

    Coding schema is similar to the following...

    -==cfSearching==-
    select * from information_schema.columns where table_name = 'x' ..

    knowledgenotebook (but this is different ...)

    Participating Frequently
    April 25, 2011

    Ok, since apparently it got eaten via email, here's the sys table query that will get you table info.

    This query will get you all the columns in the table, with datatype  information:

    <cfquery datasource="#variables.datasource#" name="columnInfo">
                SELECT c.colid,
                    c.name,
                    c.colorder,
                    c.prec,
                    c.xtype,
                    c.typestat,
                    c.xusertype,
                    c.isnullable,
                    t.name AS typename,
                    (    SELECT COUNT(*)
                        FROM sysindexkeys
                        WHERE id = syscolumns.id
                            AND indid = 1
                            AND colid = syscolumns.colid
                    ) AS isPrimary
                FROM syscolumns c LEFT JOIN
                    systypes t ON c.xtype = t.xtype
                        AND c.xusertype = t.xusertype
                WHERE c.id = OBJECT_ID(<cfqueryparam  cfsqltype="CF_SQL_VARCHAR" value="#variables.tableName#">)
                ORDER BY c.colorder
    </cfquery>

    Probably don't need all those columns, but you should be able to loop  over that and create a CFPARAM with the name based on the 'name' and the  type based on the 'typename' values.  Haven't tested this on SQL 2008  but it used to work great in SQL 2000, so I imagine it still would.

    WolfShade
    WolfShadeAuthor
    Legend
    April 26, 2011

    Thanks, JMF.  I'll give that a shot and report back my findings.

    ^_^

    Inspiring
    April 26, 2011

    God, no.  Don't!

    As per my earlier reply, use CFDBINFO if you can.

    And if you can't do that, use SP_HELP.  That's what it's there for.

    There's not need to query those tables by hand.

    --

    Adam

    WolfShade
    WolfShadeAuthor
    Legend
    April 25, 2011
    What is the purpose of this application?  Where will it be expected to live and work?
    Are you familar with your database management system of choice's meta tables?  I.E. the tables that contain the definitiosn of all the other tables.

    It's an internal corporate web app, nothing the public will see.  It's a form that will keep track of a lot of data.  Five fields wide by 25 fields high (125 fields just for numbers.)

    I am not familiar, no.

    This requirement makes me cringe a bit though... why are you needing to do it?

    Because I don't want to manually CFPARAM over 125 database columns, defaulting to either 0 or '' unless I absolutely POSITIVELY have to.

    If it were just 20 or 30 columns, then even I would say I'm lazy.  But not over 125.

    Thanks,

    ^_^

    ilssac
    Inspiring
    April 25, 2011

    WolfShade wrote:

    Because I don't want to manually CFPARAM over 125 database columns, defaulting to either 0 or '' unless I absolutely POSITIVELY have to.

    If it were just 20 or 30 columns, then even I would say I'm lazy.  But not over 125.

    Being a lazy programmer is usually a good thing.

    Creating a tool to create code is a common task.

    We were just concerned that you might think that this would be the kind of code that would be safe to leave in production systems.  I wouldn't want to do that.

    I do not know the details for MS SQL, but I'm sure search engines do.

    But there are meta tables that will describe the other tables.  It should be possilbe to query these meta tables to get all the data you need to generate the desired code.

    Maybe the poster who's code was eaten by the forum software will try again.

    Inspiring
    April 25, 2011

    Well... start by looking @ the SP_HELP proc.  Or perhaps CFDBINFO (which probably wraps up calls to SP_HELP under the hood).

    Tha'll give you the DB info you want, then it's just a matter of looping over the relevant info, and paramming the variable into the specific scope that you want.  You'll probably need to map the SQL Server data types to CF data types with a look-up struct or something (for the TYPE attribute of the CFPARAM).

    This requirement makes me cringe a bit though... why are you needing to do it?

    --

    Adam

    ilssac
    Inspiring
    April 25, 2011

    WolfShade wrote:

    Any suggestions?

    ^_^

    What are you having trouble with?

    What is the purpose of this application?  Where will it be expected to live and work?

    Are you familar with your database management system of choice's meta tables?  I.E. the tables that contain the definitiosn of all the other tables.

    Participating Frequently
    April 25, 2011

    This query will get you all the columns in the table, with datatype

    information:

    Probably don't need all those columns, but you should be able to loop

    over that and create a CFPARAM with the name based on the 'name' and the

    type based on the 'typename' values. Haven't tested this on SQL 2008

    but it used to work great in SQL 2000, so I imagine it still would.

    ilssac
    Inspiring
    April 25, 2011

    JMF wrote:

    This query will get you all the columns in the table, with datatype

    information:

    Looks like you replied by e-mail and the Jive software ate your code sample.

    Inspiring
    April 25, 2011

    Looks like you replied by e-mail and the Jive software ate your code sample.

    Come on Ian: give Jive a break.  At least people's posts are showing up today.  Which is a marked improvement on y/day.

    --

    Adam