473,586 Members | 2,754 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Foreach in MSSQL 2005

dcharnigo
20 New Member
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 4504
ck9663
2,878 Recognized Expert Specialist
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
dcharnigo
20 New Member
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 Recognized Expert Specialist
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
23865
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 tracks jobs for me, and we
1
1446
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 to test MARS feature following the example in the msdn website as the url "http://msdn2.microsoft.com/en-us/library/yf1a7f4f" to see what the...
0
1509
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 depositing user name and passwords for MSSQL of the server in "connection string" (with open text) b) Windows Authentication and to send rights to SQL...
1
2541
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 Studio 2005 Professional Eval before which installed an MSSQL Express instance. Since then I didn't get 'Enterprise Manager' (excuse my ignorance, I...
0
1444
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 tried installing MSSQL 2005, it allows only the client tools. I have revert the process and successfully install MSSQL 2005 server but now the MSDE...
7
6500
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 Microsoft.SqlServer.Management.Smo; Unfortunately, MSSQL 2005 and MSSQL Express keep them in different place. For MSSQL2005 they are located in C:\Program...
0
1117
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 secuirity option" when you script the object that you want but in MSSQL 2005 I cant find it. Thanks in advance.
0
1221
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 tried to import it, the data and tables came in just fine, but the properties such as Identity Values, Column Defaults, Indexes did not import. Also...
0
1769
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 install ODBC MySQL 3.51 configuring system DSN add a linked server on MSSQL 2005
0
7839
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8338
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7954
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3836
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.