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

LVL 16
Gurpreet Singh RandhawaCEOAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gurpreet Singh RandhawaCEOAuthor 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Servers

From novice to tech pro — start learning today.