Showing posts with label rowcount. Show all posts
Showing posts with label rowcount. Show all posts

Monday, March 12, 2012

how to get rowcount from table

Hi guys, can anybody help to solve this problem.


set @.count=0
Insert into User_t (userid, counter) select userid, count+1 from resultset is not working

0/p: bhasker 1
bhanu 1
kishore 1


but o/p must be

bhasker 1
bhanu 2
kishore 3

You can't do it with a query like that. Here are two alternatives

1. Declare the counter field as an identity integer (it'll increment by one each time)

or

2. cursor around the resultset, inserting one record at a time and incrementing by one within the loop.

|||

Here's a neat trick.

Create a table variable with an additional column to store the counter value. And insert into the table from your SELECT. do an UPDATE on the Table variable as follows.

Declare @.TTable (useridvarchar(10), Counterint)Insert into @.TSelect'bhasker',0unionallSelect'bhanu',0unionallSelect'kishore',0select *from @.tDeclare @.iintSet @.i = 0Update @.TSet @.i = Counter = @.i + 1Select *from @.t
|||

Have a look at this. But I think you can do somthing like this.

http://support.microsoft.com/kb/186133

select rank=count(*), a1.name from addresses a1, addresses a2
where a1.name >= a2.name
group by a1.name
order by rank


|||

My exact question is:

INSERTINTO UserChargeDetail(UserID, Offering, Counter,OfferingDetail,

Comment, ValueInput, CostCenterInput,Month)

SELECT UserID, tierOrder,rowsequence,NULL,NULL, 1,'',GetDate()FROM CostCenter

how do I get rowsequence

rowsequence: 1,2,3,4,5

|||

You were shown at least 2 or more working solutions on getting the sequence number. Please put some effort in trying out those solutions. If you are still unable to resolve your issue, please post what you have tried so we can guide you into solving the issue.

How to get rowcount

Hi,

How to get the row count for a particular query being executed within a session and scope. is there any option to get the value being displayed in the messages tab when a query is executed (no of rows affected). is there anything equivalent to scope_identity for getting the identity value inserted in a session and scope.

Vivek S

to obtain number of rows affected...

@.@.RowCount (return an int)

RowCount_Big() (return a big int)

to obtain last genereted identiy value...

@.@.IDENTITY, SCOPE_IDENTITY, and IDENT_CURRENT are similar functions because they all return the last value inserted into the IDENTITY column of a table.

@.@.IDENTITY and SCOPE_IDENTITY return the last identity value generated in any table in the current session. However, SCOPE_IDENTITY returns the value only within the current scope; @.@.IDENTITY is not limited to a specific scope.

IDENT_CURRENT is not limited by scope and session; it is limited to a specified table. IDENT_CURRENT returns the identity value generated for a specific table in any session and any scope. For more information, see IDENT_CURRENT (Transact-SQL).

The scope of the @.@.IDENTITY function is current session on the local server on which it is executed. This function cannot be applied to remote or linked servers. To obtain an identity value on a different server, execute a stored procedure on that remote or linked server and have that stored procedure (which is executing in the context of the remote or linked server) gather the identity value and return it to the calling connection on the local server.

|||did this help?