Solved

SELECT @@identity does not work

Posted on 2011-03-10
19
489 Views
Last Modified: 2012-05-11
For some reason this doesn't work. It returns this error.

Element NEWPAYMENTID is undefined in NEWPAY

Please advise.
<cftransaction>
<cfquery name="newpay" datasource="#request.dataSource#">
INSERT INTO payments
(payment_type, payment_date, payment_amount, payment_notes, uid)
VALUES (
	   <cfqueryparam value="#FORM.payment_type#" cfsqltype="cf_sql_integer">,
       <cfqueryparam value="#FORM.pay_date#" cfsqltype="cf_sql_varchar">,
       <cfqueryparam value="#FORM.payment_amount#" cfsqltype="cf_sql_float">,
       <cfqueryparam value="#FORM.payment_notes#" cfsqltype="cf_sql_varchar">,
       <cfqueryparam value="#SESSION.auth.user_id#" cfsqltype="cf_sql_integer">
       )

SELECT @@identity AS newpaymentid
</cfquery>

<cfset npid = newpay.newpaymentid>

<cfquery name="newpay2" datasource="#request.dataSource#">
<cfset invList = "#FORM.invoice_id#">
<cfloop list="#invList#" index="invID">
INSERT INTO invoice_payments
(inv_id, p_id)
VALUES (
	   <cfqueryparam value="#invID#" cfsqltype="cf_sql_integer">,
       <cfqueryparam value="#npid#" cfsqltype="cf_sql_integer">
	   )
</cfloop>
</cfquery>

<cfquery name="upinvoice" datasource="#request.datasource#"> 
UPDATE invoice
SET invoice_status = '1',
	paid_by = <cfqueryparam value="#FORM.payment_type#" cfsqltype="cf_sql_integer">,
    check_number = <cfqueryparam value="#FORM.check_number#" cfsqltype="cf_sql_integer">
WHERE invoice_id IN (<cfqueryparam value="#FORM.invoice_id#" cfsqltype="cf_sql_integer" list="yes"> 
</cfquery>

</cftransaction>

Open in new window

0
Comment
Question by:aka_FATCAT
  • 5
  • 5
  • 3
  • +4
19 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
please try:
<cftransaction>
<cfquery name="newpay" datasource="#request.dataSource#">
SET NOCOUNT ON
INSERT INTO payments
(payment_type, payment_date, payment_amount, payment_notes, uid)
VALUES (
	   <cfqueryparam value="#FORM.payment_type#" cfsqltype="cf_sql_integer">,
       <cfqueryparam value="#FORM.pay_date#" cfsqltype="cf_sql_varchar">,
       <cfqueryparam value="#FORM.payment_amount#" cfsqltype="cf_sql_float">,
       <cfqueryparam value="#FORM.payment_notes#" cfsqltype="cf_sql_varchar">,
       <cfqueryparam value="#SESSION.auth.user_id#" cfsqltype="cf_sql_integer">
       )

SELECT SCOPE_IDENTITY() AS newpaymentid
</cfquery>

<cfset npid = newpay.newpaymentid>

<cfquery name="newpay2" datasource="#request.dataSource#">
<cfset invList = "#FORM.invoice_id#">
<cfloop list="#invList#" index="invID">
INSERT INTO invoice_payments
(inv_id, p_id)
VALUES (
	   <cfqueryparam value="#invID#" cfsqltype="cf_sql_integer">,
       <cfqueryparam value="#npid#" cfsqltype="cf_sql_integer">
	   )
</cfloop>
</cfquery>

<cfquery name="upinvoice" datasource="#request.datasource#"> 
UPDATE invoice
SET invoice_status = '1',
	paid_by = <cfqueryparam value="#FORM.payment_type#" cfsqltype="cf_sql_integer">,
    check_number = <cfqueryparam value="#FORM.check_number#" cfsqltype="cf_sql_integer">
WHERE invoice_id IN (<cfqueryparam value="#FORM.invoice_id#" cfsqltype="cf_sql_integer" list="yes"> 
</cfquery>

</cftransaction>

Open in new window

0
 

Author Comment

by:aka_FATCAT
Comment Utility
Now I'm getting this error.

Incorrect syntax near the keyword 'select'.

0
 
LVL 11

Expert Comment

by:Brijesh Chauhan
Comment Utility
<cftransaction>
<cfquery name="newpay" datasource="#request.dataSource#">
SET NOCOUNT ON
INSERT INTO payments
(payment_type, payment_date, payment_amount, payment_notes, uid)
VALUES (
	   <cfqueryparam value="#FORM.payment_type#" cfsqltype="cf_sql_integer">,
       <cfqueryparam value="#FORM.pay_date#" cfsqltype="cf_sql_varchar">,
       <cfqueryparam value="#FORM.payment_amount#" cfsqltype="cf_sql_float">,
       <cfqueryparam value="#FORM.payment_notes#" cfsqltype="cf_sql_varchar">,
       <cfqueryparam value="#SESSION.auth.user_id#" cfsqltype="cf_sql_integer">
       )
SELECT newpaymentid = SCOPE_IDENTITY()
</cfquery>

<cfset npid = newpay.newpaymentid>

<cfquery name="newpay2" datasource="#request.dataSource#">
<cfset invList = "#FORM.invoice_id#">
<cfloop list="#invList#" index="invID">
INSERT INTO invoice_payments
(inv_id, p_id)
VALUES (
	   <cfqueryparam value="#invID#" cfsqltype="cf_sql_integer">,
       <cfqueryparam value="#npid#" cfsqltype="cf_sql_integer">
	   )
</cfloop>
</cfquery>

<cfquery name="upinvoice" datasource="#request.datasource#"> 
UPDATE invoice
SET invoice_status = '1',
	paid_by = <cfqueryparam value="#FORM.payment_type#" cfsqltype="cf_sql_integer">,
    check_number = <cfqueryparam value="#FORM.check_number#" cfsqltype="cf_sql_integer">
WHERE invoice_id IN (<cfqueryparam value="#FORM.invoice_id#" cfsqltype="cf_sql_integer" list="yes"> 
</cfquery>

</cftransaction>

Open in new window

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
sorry.

if this does not work, try to remove the set nocount on completely ....
<cftransaction>
<cfquery name="newpay" datasource="#request.dataSource#">
SET NOCOUNT ON;
INSERT INTO payments
(payment_type, payment_date, payment_amount, payment_notes, uid)
VALUES (
           <cfqueryparam value="#FORM.payment_type#" cfsqltype="cf_sql_integer">,
       <cfqueryparam value="#FORM.pay_date#" cfsqltype="cf_sql_varchar">,
       <cfqueryparam value="#FORM.payment_amount#" cfsqltype="cf_sql_float">,
       <cfqueryparam value="#FORM.payment_notes#" cfsqltype="cf_sql_varchar">,
       <cfqueryparam value="#SESSION.auth.user_id#" cfsqltype="cf_sql_integer">
       )
;
SELECT SCOPE_IDENTITY() AS newpaymentid
</cfquery>

<cfset npid = newpay.newpaymentid>

<cfquery name="newpay2" datasource="#request.dataSource#">
<cfset invList = "#FORM.invoice_id#">
<cfloop list="#invList#" index="invID">
INSERT INTO invoice_payments
(inv_id, p_id)
VALUES (
           <cfqueryparam value="#invID#" cfsqltype="cf_sql_integer">,
       <cfqueryparam value="#npid#" cfsqltype="cf_sql_integer">
           )
</cfloop>
</cfquery>

<cfquery name="upinvoice" datasource="#request.datasource#"> 
UPDATE invoice
SET invoice_status = '1',
        paid_by = <cfqueryparam value="#FORM.payment_type#" cfsqltype="cf_sql_integer">,
    check_number = <cfqueryparam value="#FORM.check_number#" cfsqltype="cf_sql_integer">
WHERE invoice_id IN (<cfqueryparam value="#FORM.invoice_id#" cfsqltype="cf_sql_integer" list="yes"> 
</cfquery>

</cftransaction>

Open in new window

0
 
LVL 39

Expert Comment

by:Pratima Pharande
Comment Utility
try with

<cfquery name="newpay" datasource="#request.dataSource#">
SET NOCOUNT ON
INSERT INTO payments
(payment_type, payment_date, payment_amount, payment_notes, uid)
VALUES (
         <cfqueryparam value="#FORM.payment_type#" cfsqltype="cf_sql_integer">,
       <cfqueryparam value="#FORM.pay_date#" cfsqltype="cf_sql_varchar">,
       <cfqueryparam value="#FORM.payment_amount#" cfsqltype="cf_sql_float">,
       <cfqueryparam value="#FORM.payment_notes#" cfsqltype="cf_sql_varchar">,
       <cfqueryparam value="#SESSION.auth.user_id#" cfsqltype="cf_sql_integer">
       );

SELECT SCOPE_IDENTITY() AS newpaymentid;
</cfquery>
0
 

Author Comment

by:aka_FATCAT
Comment Utility
Tried it both ways.

I get the same error. either not found or incorrect syntax.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
0
 
LVL 19

Expert Comment

by:Bhavesh Shah
Comment Utility
Hi,

Try this way..
This should work

- Bhavesh
<cftransaction>
<cfset invList = "#FORM.invoice_id#">

<cfquery name="newpay" datasource="#request.dataSource#">
INSERT INTO payments
(payment_type, payment_date, payment_amount, payment_notes, uid)
VALUES (
           <cfqueryparam value="#FORM.payment_type#" cfsqltype="cf_sql_integer">,
       <cfqueryparam value="#FORM.pay_date#" cfsqltype="cf_sql_varchar">,
       <cfqueryparam value="#FORM.payment_amount#" cfsqltype="cf_sql_float">,
       <cfqueryparam value="#FORM.payment_notes#" cfsqltype="cf_sql_varchar">,
       <cfqueryparam value="#SESSION.auth.user_id#" cfsqltype="cf_sql_integer">
       )

DECLARE @NPID Int	   
SELECT @npid = @@identity

<cfloop list="#invList#" index="invID">
INSERT INTO invoice_payments
(inv_id, p_id)
VALUES (
           <cfqueryparam value="#invID#" cfsqltype="cf_sql_integer">,
       @NPID
           )
</cfloop>

UPDATE invoice
SET invoice_status = '1',
        paid_by = <cfqueryparam value="#FORM.payment_type#" cfsqltype="cf_sql_integer">,
    check_number = <cfqueryparam value="#FORM.check_number#" cfsqltype="cf_sql_integer">
WHERE invoice_id IN (<cfqueryparam value="#FORM.invoice_id#" cfsqltype="cf_sql_integer" list="yes"> 

</cfquery>
</cftransaction>

Open in new window

0
 
LVL 15

Accepted Solution

by:
myselfrandhawa earned 500 total points
Comment Utility
ok are you using Coldfusion 8, If yes do the Following

<cftransaction>
<cfquery name="newpay" datasource="#request.dataSource#" result="LastID">
INSERT INTO payments
(payment_type, payment_date, payment_amount, payment_notes, uid)
VALUES (
         <cfqueryparam value="#FORM.payment_type#" cfsqltype="cf_sql_integer">,
       <cfqueryparam value="#FORM.pay_date#" cfsqltype="cf_sql_varchar">,
       <cfqueryparam value="#FORM.payment_amount#" cfsqltype="cf_sql_float">,
       <cfqueryparam value="#FORM.payment_notes#" cfsqltype="cf_sql_varchar">,
       <cfqueryparam value="#SESSION.auth.user_id#" cfsqltype="cf_sql_integer">
       )
</cfquery>
<cfset newpaymentid = LastID.IDENTITYCOL>
<cfset npid = newpaymentid>

<cfquery name="newpay2" datasource="#request.dataSource#">
<cfset invList = "#FORM.invoice_id#">
<cfloop list="#invList#" index="invID">
INSERT INTO invoice_payments
(inv_id, p_id)
VALUES (
         <cfqueryparam value="#invID#" cfsqltype="cf_sql_integer">,
       <cfqueryparam value="#npid#" cfsqltype="cf_sql_integer">
         )
</cfloop>
</cfquery>

<cfquery name="upinvoice" datasource="#request.datasource#">
UPDATE invoice
SET invoice_status = '1',
      paid_by = <cfqueryparam value="#FORM.payment_type#" cfsqltype="cf_sql_integer">,
    check_number = <cfqueryparam value="#FORM.check_number#" cfsqltype="cf_sql_integer">
WHERE invoice_id IN (<cfqueryparam value="#FORM.invoice_id#" cfsqltype="cf_sql_integer" list="yes">)
</cfquery>

</cftransaction>

I think this work as you were missing the last ")" in your last Query
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 19

Expert Comment

by:Bhavesh Shah
Comment Utility
Hi,

Secondly

<cfquery name="newpay" datasource="#request.dataSource#">
INSERT INTO payments
(payment_type, payment_date, payment_amount, payment_notes, uid)
VALUES (
           <cfqueryparam value="#FORM.payment_type#" cfsqltype="cf_sql_integer">,
       <cfqueryparam value="#FORM.pay_date#" cfsqltype="cf_sql_varchar">,
       <cfqueryparam value="#FORM.payment_amount#" cfsqltype="cf_sql_float">,
       <cfqueryparam value="#FORM.payment_notes#" cfsqltype="cf_sql_varchar">,
       <cfqueryparam value="#SESSION.auth.user_id#" cfsqltype="cf_sql_integer">
       )

SELECT @@identity AS newpaymentid;
</cfquery>

<cfset npid = newpay.newpaymentid>


This should works too....

Its working in my application.

By the way, which version of coldfusion/sql server you using?
0
 
LVL 39

Expert Comment

by:gdemaria
Comment Utility
Let's first make sure that the table actually has an identity column, can you verify?

If you do an INSERT into payments using SQL Manager, and then select from it, do you see the value in the primary key field added automatically?


INSERT INTO payments
(payment_type, payment_date, payment_amount, payment_notes, uid)
  VALUES (... put in some values here..    )


0
 
LVL 39

Expert Comment

by:gdemaria
Comment Utility

here are the two ways that I have done it..
** ADD COMMA BEFORE SELECT SCOPE...
<cfquery name="newpay" datasource="#request.dataSource#">
  INSERT INTO payments
  (payment_type, payment_date, payment_amount, payment_notes, uid)  
  VALUES (
       <cfqueryparam value="#FORM.payment_type#" cfsqltype="cf_sql_integer">,
       <cfqueryparam value="#FORM.pay_date#" cfsqltype="cf_sql_varchar">,
       <cfqueryparam value="#FORM.payment_amount#" cfsqltype="cf_sql_float">,
       <cfqueryparam value="#FORM.payment_notes#" cfsqltype="cf_sql_varchar">,
       <cfqueryparam value="#SESSION.auth.user_id#" cfsqltype="cf_sql_integer">
       )
  ;
  select SCOPE_IDENTITY( ) as newpaymentid;
</cfquery>
<cfset npid = newpay.newPaymentId>


**** SEPERATE CFQUERY FOR IDENTITY...
<cfquery name="newpay" datasource="#request.dataSource#">
  INSERT INTO payments
  (payment_type, payment_date, payment_amount, payment_notes, uid)  
  VALUES (
       <cfqueryparam value="#FORM.payment_type#" cfsqltype="cf_sql_integer">,
       <cfqueryparam value="#FORM.pay_date#" cfsqltype="cf_sql_varchar">,
       <cfqueryparam value="#FORM.payment_amount#" cfsqltype="cf_sql_float">,
       <cfqueryparam value="#FORM.payment_notes#" cfsqltype="cf_sql_varchar">,
       <cfqueryparam value="#SESSION.auth.user_id#" cfsqltype="cf_sql_integer">
       )
</cfquery>
<cfquery name="GetPkey" datasource="#request.datasource#">
 select SCOPE_IDENTITY( ) as ID
</cfquery>  
<cfset npid = GetPkey.ID>

Open in new window

0
 
LVL 15

Expert Comment

by:myselfrandhawa
Comment Utility
@author!

Have you actually tried the way we have provided the info, Try the way i have provided and see if it matches ur requirement or not
0
 
LVL 39

Expert Comment

by:gdemaria
Comment Utility
yes, i agree, you should try myselfrandhawa's method using the result on the query
0
 

Author Closing Comment

by:aka_FATCAT
Comment Utility
"I think this work as you were missing the last ")" in your last Query"

Once again, it was 4am, and my brain had stopped functioning.

All of the previous suggestions worked as well, but it was that damn missing ")" that was the true culprit.

Thank you so much.
0
 
LVL 39

Expert Comment

by:gdemaria
Comment Utility
How were you getting the error:
 Element NEWPAYMENTID is undefined in NEWPAY

because you were missing a ) in a query that came AFTER the error statement?


You must have fixed the error you posted about and not told us
0
 

Author Comment

by:aka_FATCAT
Comment Utility
That was mu thought exactly, but as you can see, it was the missing ")" that was causing the error.

I have attached the working code.
<cfquery name="newpay" datasource="#request.dataSource#">
  INSERT INTO payments
  (payment_type, payment_date, payment_amount, payment_notes, uid)  
  VALUES (
       <cfqueryparam value="#FORM.payment_type#" cfsqltype="cf_sql_integer">,
       <cfqueryparam value="#FORM.pay_date#" cfsqltype="cf_sql_varchar">,
       <cfqueryparam value="#FORM.payment_amount#" cfsqltype="cf_sql_float">,
       <cfqueryparam value="#FORM.payment_notes#" cfsqltype="cf_sql_varchar">,
       <cfqueryparam value="#SESSION.auth.user_id#" cfsqltype="cf_sql_integer">
       )
</cfquery>
<cfquery name="GetPkey" datasource="#request.datasource#">
 SELECT @@identity AS ID
</cfquery>  
<cfset npid = GetPkey.ID>

<cfquery name="newpay2" datasource="#request.dataSource#">
<cfset invList = "#FORM.invoice_id#">
<cfloop list="#invList#" index="invID">
INSERT INTO invoice_payments
(inv_id, p_id)
VALUES (
	   <cfqueryparam value="#invID#" cfsqltype="cf_sql_integer">,
       <cfqueryparam value="#npid#" cfsqltype="cf_sql_integer">
	   )
</cfloop>
</cfquery>

<cfquery name="upinvoice" datasource="#request.datasource#"> 
UPDATE invoice
SET invoice_status = '1',
	paidby_id = <cfqueryparam value="#FORM.payment_type#" cfsqltype="cf_sql_integer">,
    check_number = <cfqueryparam value="#FORM.check_number#" cfsqltype="cf_sql_integer">
WHERE invoice_id IN (<cfqueryparam value="#FORM.invoice_id#" cfsqltype="cf_sql_integer" list="yes">) 
</cfquery>

Open in new window

0
 

Author Comment

by:aka_FATCAT
Comment Utility
This is the original version, with a separate query for the @@identity.

Works great, again it was failing because of the missing ")" at the very end.
<cftransaction>
<cfquery name="newpay" datasource="#request.dataSource#">
INSERT INTO payments
(payment_type, payment_date, payment_amount, payment_notes, uid)
VALUES (
	   <cfqueryparam value="#FORM.payment_type#" cfsqltype="cf_sql_integer">,
       <cfqueryparam value="#FORM.pay_date#" cfsqltype="cf_sql_varchar">,
       <cfqueryparam value="#FORM.payment_amount#" cfsqltype="cf_sql_float">,
       <cfqueryparam value="#FORM.payment_notes#" cfsqltype="cf_sql_varchar">,
       <cfqueryparam value="#SESSION.auth.user_id#" cfsqltype="cf_sql_integer">
       )
</cfquery>

<cfquery name="GetPkey" datasource="#request.datasource#">
 SELECT @@identity AS ID
</cfquery>  
<cfset npid = GetPkey.ID>

<cfquery name="newpay2" datasource="#request.dataSource#">
<cfset invList = "#FORM.invoice_id#">
<cfloop list="#invList#" index="invID">
INSERT INTO invoice_payments
(inv_id, p_id)
VALUES (
	   <cfqueryparam value="#invID#" cfsqltype="cf_sql_integer">,
       <cfqueryparam value="#npid#" cfsqltype="cf_sql_integer">
	   )
</cfloop>
</cfquery>

<cfquery name="upinvoice" datasource="#request.datasource#"> 
UPDATE invoice
SET invoice_status = '1',
	paidby_id = <cfqueryparam value="#FORM.payment_type#" cfsqltype="cf_sql_integer">,
    check_number = <cfqueryparam value="#FORM.check_number#" cfsqltype="cf_sql_integer">
WHERE invoice_id IN (<cfqueryparam value="#FORM.invoice_id#" cfsqltype="cf_sql_integer" list="yes">) 
</cfquery>

</cftransaction>

Open in new window

0
 
LVL 39

Expert Comment

by:gdemaria
Comment Utility

Funny, you used my solution putting the select identity in a different cfquery


You're saying if you remove the ) and only make that one change you get the error

  Element NEWPAYMENTID is undefined in NEWPAY



I think you had multiple issues..


0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now