Skip to main content
Inspiring
May 26, 2009
Answered

insert into within cfloop list

  • May 26, 2009
  • 2 replies
  • 2934 views

I was trying to loop aover a list and do a simple insert and I failed!!!

Have I done something wrong with my simple code below? Please help!

The column years is varchar 4

<cfset

myList="2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020,2021,2022,2023,2024,2025">

<cfloop

list="#myList#" index="i">

   <cfquery name="x" datasource="#MyDSN#">

   Insert Into

    Tblyears (years)

   value('#i#')

   </cfquery>

</cfloop>

The Error :

Error executing database query

[Macromedia][Sybase JDBC Driver][Sybase] Incorrect syntax near 'value'

This topic has been closed for replies.
Correct answer ilssac

mega_L wrote:

value('#i#')

IIRC the SQL command is 'values' even if you only have one to insert.

<cfset myList="2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020,2021,2022 ,2023,2024,2025">

<cfloop list="#myList#" index="i">

   <cfquery name="x" datasource="#MyDSN#">

   Insert Into

    Tblyears (years)

   values('#i#')

   </cfquery>

</cfloop>

2 replies

Inspiring
May 26, 2009

More efficient in some databases

insert into yourtable

(years)

<cfloop list="#yourlist#" index="i">

select #i# from somesmalltable

union

</cfloop>

select 0 from somesmalltable where 1 = 2

ilssac
ilssacCorrect answer
Inspiring
May 26, 2009

mega_L wrote:

value('#i#')

IIRC the SQL command is 'values' even if you only have one to insert.

<cfset myList="2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020,2021,2022 ,2023,2024,2025">

<cfloop list="#myList#" index="i">

   <cfquery name="x" datasource="#MyDSN#">

   Insert Into

    Tblyears (years)

   values('#i#')

   </cfquery>

</cfloop>

mega_LAuthor
Inspiring
May 27, 2009

Thank you for pointing into my foolishness