Solved

count Number of Items in a temp table

Posted on 2010-09-07
5
435 Views
Last Modified: 2012-05-10
Hi all  
I have a temp table #myTable

 I am looking for a way to do this
      select @intcount = count(*)            
      from #myTable
Note I have a conditional processing away from the temp Table. i.e
Create table  #LCHItems(bla, bla)
Insert into #myTable  exec(@sql_trans)

if (myVariable = newVar)
begin
select @intcount = count(*)            
      from #myTable
end

else


begin
end
0
Comment
Question by:ZURINET
5 Comments
 
LVL 22

Expert Comment

by:Om Prakash
ID: 33616345
Use Global Temporary Tables
Global temporary tables operate much like local temporary tables; they are created in tempdb and cause less locking and logging than permanent tables

More info/example:
http://databases.aspfaq.com/database/should-i-use-a-temp-table-or-a-table-variable.html
0
 

Author Comment

by:ZURINET
ID: 33616552
Hi prakash

I have more than 200 users accessing the query concurrently.

how can Global Temporary Table help me..?
0
 
LVL 5

Expert Comment

by:spikelly
ID: 33617356
In that case use Table Variable.
Syntax : DECLARE @myTable TABLE (var1 int, var2 varchar, etc...)
This is faster and create less locking on the server. the table is dropped automatically at the end of the statement execution.
More info here:
1- http://www.sqlservercentral.com/articles/Temporary+Tables/66720/
2- http://www.sql-server-performance.com/articles/per/temp_tables_vs_variables_p1.aspx

Regards.
0
 
LVL 11

Expert Comment

by:Larissa T
ID: 33617720
You can get row count from @@rowcount, getting it after you execute statement

decalare @cnt int
exec(@sql_trans)
set @cnt=@@rowcount
0
 
LVL 11

Accepted Solution

by:
JoeNuvo earned 500 total points
ID: 33670298
just uses @@rowcount as above comment

Create table  #LCHItems(bla, bla)
Insert into #myTable  exec(@sql_trans)
SET @intcount = @@ROWCOUNT

then you can have value of @intcount right away

if this still can't work it out in your situation, you should provide more additional details
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sql query 7 36
Creating Alerts in sql sever 2 13
SQL Server can be started but not accessed 1 16
Slow SQL query 12 26
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

760 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

21 Experts available now in Live!

Get 1:1 Help Now