• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

getting mSSQL Err:

I have the following insert and doing insert in mSSQL:

getting error:

Error Executing Database Query.  
[Macromedia][SQLServer JDBC Driver][SQLServer]Incorrect syntax near ','.  
 
The error occurred in C:\inetpub\wwwroot\project2\admin\com\insert.cfc: line 302
 
300 :     (prodID,catID)
301 :     VALUES
302 :     <cfloop from="1" to="#listlen(arguments.structform.category)#" index="x">
303 :     (#NewProductID#, #listgetat(arguments.structform.category, x)#)
304 :     <cfif x lt listlen(arguments.structform.category)>,</cfif>
 

<cfquery datasource="#request.dsn#" name="myset" username="#request.user#" password="#request.pass#">      
    SET NOCOUNT ON
    INSERT INTO products(productName, prodDesc, seller_ID, productaddon, status, prodShortDesc) 
    VALUES(<cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.structform.productname#" maxlength="255">,
    <cfqueryparam cfsqltype="cf_sql_longvarchar" value="#arguments.structform.productDesc#">,
    <cfqueryparam cfsqltype="cf_sql_numeric" value="#arguments.structform.chooseSeller#">,
    <cfqueryparam cfsqltype="cf_sql_timestamp" value="#CreateODBCDateTime(now())#">,
    <cfqueryparam cfsqltype="cf_sql_numeric" value="#arguments.structform.status#">,
    <cfqueryparam cfsqltype="cf_sql_longvarchar" value="#arguments.structform.productShortDesc#">)
    SET NOCOUNT OFF 
    select scope_identity() as productID;
    </cfquery>
    <cfset NewProductID = #myset.productID#>
    <cfquery name="insertItemCats" datasource="#request.dsn#" username="#request.user#" password="#request.pass#">
    INSERT INTO ProductCategories   
    (prodID,catID) 
    VALUES
    <cfloop from="1" to="#listlen(arguments.structform.category)#" index="x">
    (#NewProductID#, #listgetat(arguments.structform.category, x)#)
    <cfif x lt listlen(arguments.structform.category)>,</cfif>
    </cfloop>
    </cfquery>

Open in new window

0
Gurpreet Singh Randhawa
Asked:
Gurpreet Singh Randhawa
  • 3
1 Solution
 
gdemariaCommented:
Have you looked at the debug output to see what your query looks like when it's being run?

The way you have the loop, that will look like...

      INSERT INTO ProductCategories (prodID,catID)
      VALUES
         (1, 21)
         (1, 25)
         (1, 30)
         (1, 36)
   

Is that a valid SQL statment in your database?
0
 
gdemariaCommented:
oops, missed the comma, it looks like this..

      INSERT INTO ProductCategories (prodID,catID)
      VALUES (1, 21), (1, 25), (1, 30), (1, 36)

0
 
gdemariaCommented:
Try this..

    <cfquery name="insertItemCats" datasource="#request.dsn#" username="#request.user#" password="#request.pass#">
       <cfloop index="x" list="#arguments.structform.category#">
         INSERT INTO ProductCategories (prodID,catID) values (#NewProductID#, #x#);
       </cfloop>
    </cfquery>

0
 
Gurpreet Singh RandhawaWeb DeveloperAuthor Commented:
Thanks This worked..

My Approach worked in Mysql but it failed in MSSQL Server..

Why So can u explain a bit on it

Regards
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now