473,395 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Foreach in MSSQL 2005

dcharnigo
I am writing a purge routine for a database that I have. I select all records that have aged to a certain threshold into a temp table, now I want to remove some of these items, then in the end after everything is sorted remove the remaining records from the original table. Each record has a unique guid.

So to delete at the end I think I just need to do:

DELETE * FROM orig_table where orig_table.guid IN temp_table

My Question is after I select the records into the temp table I need a count foreach record on field CIFPan, in C# I would do something like this:

foreach ( CIFPan var in temp_table ) {
@count = select count(*) from orginal_table where CIFPan = var
if @count > 1 DELETE * FROM orig_table WHERE CIFPan = var
}

Obviously just sudo code but can I do something like that in the SQL server stored procedure? I was thinking maybe Some sort of UNION/JOIN/SELECT combination?

Thanks for the Help.
Mar 31 '08 #1
3 4492
ck9663
2,878 Expert 2GB
I am writing a purge routine for a database that I have. I select all records that have aged to a certain threshold into a temp table, now I want to remove some of these items, then in the end after everything is sorted remove the remaining records from the original table. Each record has a unique guid.

So to delete at the end I think I just need to do:

DELETE * FROM orig_table where orig_table.guid IN temp_table

My Question is after I select the records into the temp table I need a count foreach record on field CIFPan, in C# I would do something like this:

foreach ( CIFPan var in temp_table ) {
@count = select count(*) from orginal_table where CIFPan = var
if @count > 1 DELETE * FROM orig_table WHERE CIFPan = var
}

Obviously just sudo code but can I do something like that in the SQL server stored procedure? I was thinking maybe Some sort of UNION/JOIN/SELECT combination?

Thanks for the Help.
Try:

Expand|Select|Wrap|Line Numbers
  1. select * into ForDeletionTmp from OrigTable where SomeConditionofAge = CertainValue
  2.  
  3. select CIFPan, count(*) as cnt from ForDeletionTmp group by CIFPan 
This code is not complete. I left the part where you delete the records from your Original Table. Your code shows that you only delete records from Original Table if there are multiple records (based on CIFPan). If there's only one record, it looks like you're going to keep the record if @count > 1 DELETE * FROM orig_table WHERE CIFPan = var is this right?

If yes, proceed with this additional code:

Expand|Select|Wrap|Line Numbers
  1. delete from OrigTable 
  2. where CIFPan in (select CIFPan, count(*) as cnt from ForDeletionTmp group by CIFPan having count(*) > 1)
  3.  
Replace table names as neccesary.

-- CK
Mar 31 '08 #2
Thanks for the help, You gave me a good start I will get it to work. I guess whenever I need to do a "foreach" I need to select into a temp table and then execute the next select on the temp table.

Thanks,

Dan
Apr 12 '08 #3
ck9663
2,878 Expert 2GB
Yes. But depending on what you need, there are other ways you can do it. So if you hit a wall, post it here and we'll try to crack it.

Happy Coding.

-- CK
Apr 12 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: mj | last post by:
Hello, thanks for the help. I am running a WinXP Pro w/ SP2 (my home computer, with ZoneAlarm firewall) Apache 2.0.52 MySQL 4.1.7 PHP 5.1.0-dev I have developed a PHP/MySQL web app that...
1
by: Optimus | last post by:
Hi everyone, I've got issue about MARS feature support in ASP.NET 2.0 and MSSQL 2.0 How to get things work? What i've done is I got vs.net 2.0 and MSSQL 2005 installed on my pc. Then I tried...
0
by: DariuszK | last post by:
I am greeting I would like to consult the better solution(with security consideration) to the access is for MSSQL(2005) from ASP(Classic) in the system of the intranet. a) SQL mixed mode and...
1
by: theintrepidfox | last post by:
Dear Group. Wondered if any of you has any suggestion for the following? Trying to install SQL Server 2005 Eval on a 'clean' machine. Well, my mistake was probably that I had installed Visual...
0
by: Derftics | last post by:
Hi Guys, Is there anyone who have tried installing MSSQL 2000 and MSSQL 2005 servers in one desktop computer? I have tried using MSDE and successfully install the MSSQL 2000 server but when I...
7
by: Olegus | last post by:
Hello, in order to perform backup/restore MSSQL database using SMO, one needs to reference several namespaces in a backup class : using Microsoft.SqlServer.Management.Common; using...
0
by: Derftics | last post by:
Hi Guys, Anyone knows how to script all the users with permission and access level to an object in MSSQL 2005? In MSSQL 2000 you can use the scripting in EM just go to the option "script...
0
by: 123bargains | last post by:
Hello, I have a question on importing a database from MSSQL 2000 to MSSQL 2005. I hope someone on here can help me answer it. I am trying to import a database from MSSQL 2000 to 2005. But, when I...
0
by: Gosth in the shell | last post by:
Hi there, i need some help I got a software to backend is MSSQL 2005, but a provider software requires an UPDATE on his table with MySQL 5.0.56a backend, so i made the next: download and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.