Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

getting @@identity and @@rowcount

Guest
Apr 23, 2009 Apr 23, 2009

When I do this

<cfquery name="test" datasource="#ds#">

   insert into tbltest (name,weight) values ('#thename#','#thewwight#')

   select @@ as idadded
   select @@rowcount as rowsadded

</cfquery>

<cfoutput>

   #itest.idadded#,#test.rowsadded#

</cfoutput>

testtest.idadded shows but test.rowsadded is not defined-I get an error.

1) How do I get both test.idadded and test.rowsadded to show ?

2) If done with cfstoredproc and a stored procedure (sqlserver), both of the values show up. Why is this ?

672
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Apr 23, 2009 Apr 23, 2009

well for that kind of insert the row count will always be 1 & as the other folks

have suggested there are other ways to skin this particular cat but if you

really need to do it in your cfquery maybe something like:

SELECT @@IDENTITY as idadded, @@ROWCOUNT as rowsadded

Translate
Valorous Hero ,
Apr 23, 2009 Apr 23, 2009

<cfquery...> can only return one result set.  You can't have two selects like that in it.  For something like that you would need to go to stored procedures and the <cfstoredproc...>, <cfprocparam...> and <cfprocresult...> tags.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Apr 23, 2009 Apr 23, 2009

As mentioned, cfquery returns a single resultset.

lwfg wrote:

When I do this

<cfquery name="test" datasource="#ds#">

   insert into tbltest (name,weight) values ('#thename#','#thewwight#')

   select @@ as idadded
   select @@rowcount as rowsadded

</cfquery>


Is there a reason you need the rowcount for a single record insert?

Also, you should look into cfquery's "result" attribute.  It can return the rows affected, and the identity value from a _single record_ insert statement.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 23, 2009 Apr 23, 2009
LATEST

well for that kind of insert the row count will always be 1 & as the other folks

have suggested there are other ways to skin this particular cat but if you

really need to do it in your cfquery maybe something like:

SELECT @@IDENTITY as idadded, @@ROWCOUNT as rowsadded

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources