[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

loop through temp table w/o cursor

Posted on 2006-05-15
7
Medium Priority
?
4,283 Views
Last Modified: 2012-05-05
Experts say to avoid cursors at all costs.

How do you loop through a temp table w/o a cursor?

I need to delete certain records in my temp table if they don't meet a certain test.
0
Comment
Question by:g_johnson
7 Comments
 
LVL 42

Assisted Solution

by:dqmq
dqmq earned 1600 total points
ID: 16683904
> Experts say to avoid cursors at all costs.

That may be a bit of an overstatement.

But, set operations are what relational databases are designed for.  You can perfrom your delete to a subset of the table if you can describe the subset in a where clause:

Delete from YourTable where textcolumn <> '5' and numeric column <> 2

Of course, the where clause is usually much more complicated
0
 
LVL 4

Assisted Solution

by:johnclarke123
johnclarke123 earned 200 total points
ID: 16683911
DELETE FROM #MyTempTable
WHERE -- your condition: e.g. MyColumn <> 0 or whatever

If you can encapsulate the condition in a single statement (as opposed to having to do some procedural logic) you don't need a cursor.  This should be the case 99% of the time.  Cursors are really only needed where you want to do something quite complex to a row that can't fit in the format above.
0
 
LVL 2

Expert Comment

by:Omnibuzz
ID: 16683937
Can you elaborate on the certain test
with some info on the table structure?
0
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
LVL 11

Assisted Solution

by:Ved Prakash Agrawal
Ved Prakash Agrawal earned 200 total points
ID: 16683950
Hi,

you can use table variable which has Indentity column  and then insert the data into that table.

and use while loop on the basis of identity column .


0
 
LVL 11

Expert Comment

by:Ved Prakash Agrawal
ID: 16683959
There is no need of delete of certain test because you are featching records on the basis of identity column which will give you a unique records.

0
 
LVL 4

Author Comment

by:g_johnson
ID: 16684144
The delete if pretty complex (if you're me!   lol)

See this question
http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/Q_21850838.html

Since I don't have a working answer yet to that question, I was going to try to take a different approach
0
 
LVL 42

Accepted Solution

by:
dqmq earned 1600 total points
ID: 16692687
Here's a code snippet that uses a cursor to delete all the rows in all the user tables where screen_name = 'Gary'. Maybe you can adapt:

DECLARE csrName CURSOR FOR
   SELECT distinct so.Name
     FROM syscolumns sc INNER JOIN sysobjects so ON so.id = sc.id
   WHERE sc.name = 'Screen_Name'
   AND so.type = 'U'  -- user table only
   AND so.category = 1  

DECLARE @name NVARCHAR(128)
DECLARE @sql NVARCHAR(4000)

OPEN csrName
FETCH NEXT FROM csrName INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN
    SET @sql = N'Delete From ' + @name + N' where screen_name = ''Gary'''
    EXEC(@sql)
    FETCH NEXT FROM csrName INTO @name
END --WHILE
CLOSE csrName
DEALLOCATE csrName
0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Suggested Courses

829 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