To get rid of redundant data in a table, my cleint will be providing
something like this:
IDtokeep Ids to delete
34 24,35,49
12 14,178,1457
54 32,65,68
I have to write a script for each of the above rows which looks like
this:
-----------------------------------
update sometable
set id = 34
where id in (24,35,49)
delete from sometable
where id in (24,35,49)
-----------------------------------
As I said I have to do this for EACH row. Can I somehow automate this
or will I need to write to same script for each row (there are about
5000 rows in this audit table)
Any help is highly appreciated.
Here is the DDL and inserts for the audit table.
IF object_id(N'dbo .dataclean','U' ) is not null
DROP TABLE [dbo].[dataclean]
GO
CREATE TABLE [dataclean] (
[IdTokeep] int NULL ,
[IdsTodelete] varchar (50) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL )
GO
INSERT INTO [dataclean] ([IdTokeep],[IdsTodelete])
VALUES(34,'24,3 5,49')
INSERT INTO [dataclean] ([IdTokeep],[IdsTodelete])
VALUES(12,'14,1 78,1457')
INSERT INTO [dataclean] ([IdTokeep],[IdsTodelete])
VALUES(54,'32,6 5,68')
GO 3 4877
If this is a one time thing then please use the following sql server
function to parse this.
The syntax would be:
update sometable set id = 34 where id in
dbo.fnStringToT able('24,35,49' ,',')
delete from sometable where id in dbo.fnStringToT able('24,35,49' ,',')
What I recommend is create a table in SQL from CSV file and populate
another table like the structure below. you can use this function to
populate this table.
MyTable:
IDToKeep IDToDelete
34 24
34 35
34 49
12 14
12 178
12 1457
and run the following statement
update sometable set id = b.idtokeep
from mytable where sometable.id=my table.idtodelet e
delete sometable where id in (select idtodelete from mytable)
The above script is not tested. so make sure you test them before you
do anythign with that. Below is the code to create the function
dbo.fnStringToT able. I hope this helps.
CREATE FUNCTION dbo.fnStringToT able
(
@str varchar(8000), @delim varchar(5)
)
RETURNS @ValueStr TABLE (value varchar(500))
AS
/*************** *************** *************** *************** *************** ***
** Name: fnStringToTable
** Desc: Parses the input parameter string with the delimiter
**
** Return values: table @valuestr (value varchar(500))
**
**
** Parameters:
** Input
** ----------
** @str - delimited string ex. . 1,2,3 max length is 8000 characters
** @delim - delimiter to parse @str ex. ",","-" max length is 5
characters
** Auth: Ramesh Thalluru
** Date: 07/29/2003
*************** *************** *************** *************** *************** ****
** Change History
*************** *************** *************** *************** *************** ****
** Date: Author: Description:
** -------- -------- -------------------------------------------
**
*************** *************** *************** *************** *************** ****/
BEGIN
declare @str1 varchar(2000), @len int, @endPos int, @stPos int,
@rightLen int, @tmpint int, @tmpstr varchar(8000)
-- if the string is empty or null return without anything
if ( @str=NULL or len(ltrim(rtrim (@str)))=0 )
return
select @str1=rtrim(ltr im(@str))
select @str=@str1
select @len=len(@str), @endPos=0, @stPos=-1, @rightLen=0
while @stPos <> 0
begin
select @str1=right(@st r, @len-@rightLen)
select @stPos=charinde x(@delim,@str1)
select @rightLen=@righ tLen+@stPos
if @stPos <> 0
begin
insert into @ValueStr(value )
select rtrim(ltrim(lef t(@str1,@stPos-1)))
end
else
begin
insert into @ValueStr(value )
select ltrim(rtrim(@st r1))
end
end
RETURN
END
muza...@hotmail .com wrote: To get rid of redundant data in a table, my cleint will be providing something like this:
IDtokeep Ids to delete 34 24,35,49 12 14,178,1457 54 32,65,68
I have to write a script for each of the above rows which looks like this: ----------------------------------- update sometable set id = 34 where id in (24,35,49)
delete from sometable where id in (24,35,49) ----------------------------------- As I said I have to do this for EACH row. Can I somehow automate this or will I need to write to same script for each row (there are about 5000 rows in this audit table)
Any help is highly appreciated.
Here is the DDL and inserts for the audit table.
IF object_id(N'dbo .dataclean','U' ) is not null DROP TABLE [dbo].[dataclean] GO
CREATE TABLE [dataclean] ( [IdTokeep] int NULL , [IdsTodelete] varchar (50) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL
) GO
INSERT INTO [dataclean] ([IdTokeep],[IdsTodelete]) VALUES(34,'24,3 5,49') INSERT INTO [dataclean] ([IdTokeep],[IdsTodelete]) VALUES(12,'14,1 78,1457') INSERT INTO [dataclean] ([IdTokeep],[IdsTodelete]) VALUES(54,'32,6 5,68') GO
I am sorry that you have sucha bad client. You should break this apart
in the front end, but if you are totally screwed, try this:
Passing a list of parmeters to a stored procedure can be done by
putting them into a string with a separator. I like to use the
traditional comma. Let's assume that you have a whole table full of
such parameter lists:
CREATE TABLE InputStrings
(keycol CHAR(10) NOT NULL PRIMARY KEY,
input_string VARCHAR(255) NOT NULL);
INSERT INTO InputStrings VALUES ('first', '12,34,567,896' );
INSERT INTO InputStrings VALUES ('second', '312,534,997,89 6');
...
This will be the table that gets the outputs, in the form of the
original key column and one parameter per row.
CREATE TABLE Parmlist
(keycol CHAR(10) NOT NULL PRIMARY KEY,
parm INTEGER NOT NULL);
It makes life easier if the lists in the input strings start and end
with a comma. You will need a talbe of sequential numbers -- a
standard SQL programming trick, Now, the real query, in SQL-92 syntax:
INSERT INTO ParmList (keycol, parm)
SELECT keycol,
CAST (SUBSTRING (I1.input_strin g
FROM S1.seq
FOR MIN(S2.seq) - S1.seq -1)
AS INTEGER)
FROM InputStrings AS I1, Sequence AS S1, Sequence AS S2
WHERE SUBSTRING ( ',' || I1.input_string || ',' FROM S1.seq FOR 1) =
','
AND SUBSTRING (',' || I1.input_string || ',' FROM S2.seq FOR 1) =
','
AND S1.seq < S2.seq
GROUP BY I1.keycol, I1.input_string , S1.seq;
The S1 and S2 copies of Sequence are used to locate bracketing pairs of
commas, and the entire set of substrings located between them is
extracted and cast as integers in one non-procedural step. The trick
is to be sure that the right hand comma of the bracketing pair is the
closest one to the first comma.
You can then write:
SELECT *
FROM Foobar
WHERE x IN (SELECT parm FROM Parmlist WHERE key_col = :something);
You would never write a T-SQL procedure, if you can avoid it.
(mu*****@hotmai l.com) writes: To get rid of redundant data in a table, my cleint will be providing something like this:
IDtokeep Ids to delete 34 24,35,49 12 14,178,1457 54 32,65,68
Undoubtedly it would be a whole lot easier if your client could just
give you plain tuples:
34 24
34 35
34 49
12 14
12 178
Then it's all a plain update statement and a plain delete.
With the current scheme, you need to run a string-to-table function,
and you need to loop row by row. (In SQL 2000. In SQL 2005 you can
do it in one statement, but you still need the string-to-table
function.)
I have to write a script for each of the above rows which looks like this: ----------------------------------- update sometable set id = 34 where id in (24,35,49)
delete from sometable where id in (24,35,49) ----------------------------------- As I said I have to do this for EACH row. Can I somehow automate this or will I need to write to same script for each row (there are about 5000 rows in this audit table)
Well, you can actually do it without the string-to-table function,
with some manual intervention:
SELECT 'UPDATE somtable SET id = ' + ltrim(str(IdTok eep)) +
' where id in (' +
IdsTodelete + ')
DELETE sometable where id in (' + IdsTodelete + ')'
FROM dataclean
And then cut and paste result.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se
Books Online for SQL Server SP3 at http://www.microsoft.com/sql/techinf...2000/books.asp This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Craig Keightley |
last post by:
is it possible to compare acomma separated list aginst another
eg comma list 1 => 1,2,3,4,5
comma list 2 => 3,5
can you check that 3 is in both, and 5 is in both, therfore they match???
the comparison is to check that if product a who supplies products 1,2,3,4,5
can be used instead of product b who supplies 3,5 as product a already
supplies them
|
by: Nath |
last post by:
Please help!?
I am new to writing html, javascript, pretty new to MySQL but quite
proficient at writing Perl and i'm a quick learner.
I am building a database driven website and i am a little stuck:
I have page of results obtained from a MySQL query presented as a table (the
first column having checkboxes for each of the rows in the table, and all
having the name "seqs"). I have set up a javascript (connected to a "toggle
all" checkbox)...
|
by: Craig Keightley |
last post by:
I have a mysql database with a list of companies who supply specific
products
tblSuppliers (simplified)
sID | sName | goodsRefs
1 | comp name | 1,2,3,4,5
2 | company 2 | 2,4
|
by: Shawn Odekirk |
last post by:
Some code I have inherited contains a macro like the following:
#define setState(state, newstate) \
(state >= newstate) ? \
(fprintf(stderr, "Illegal state\n"), TRUE) : \
(state = newstate, FALSE)
This macro is called like this:
setState(state, ST_Used);
|
by: Sick |
last post by:
My application populates a ListBox from a .TXT file. In the textfile there
are prices with both dots as well as comma's for decimal indication.
Example:
1234990; xg-tr-45; 1700,50; 0
2662666; hj-54-56; 1565.00; 0
8228880; 30-56-tw; 3295.50; 0
0022339; hs-sa-73; 2975,75; 0
.... etc
| |
by: dfetrow410 |
last post by:
I need make a comma seperated list, but whwn I build the list I get a
comma at the end. How do I remove it?
foreach (ListItem lst in REIPropertyType.Items)
{
if (lst.Selected == true)
{
REIPropertyTypeVal += lst.ToString() + ",";
}
|
by: CJK |
last post by:
this is what i have so far:
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
class Person
{
|
by: nehashri |
last post by:
i hv a database in access wid Asp as front end.
ven given a word in search command(of the front end) it shud go to a table in which each field has words serated by comma. each word shud b checked for the given user word n then only futher procedure shud happen.
this table has 2 columns one is library(colum has only one word present) is linked to another table of the same DB and other column has this synonyms (synonym of the word in...
|
by: (2b|!2b)==? |
last post by:
I have the following line in my code, which is supposed to skip commas
and white space and to read a comma seperated value string into the
appropriate variables:
sscanf(temp.c_str(),
"%f%*,%d%*,%f%*,%d%*,%d%*,%f%*,%f%*,%f%*,%f%*,%d%*,%f%*,%d%*,%d", &dt,
&ti, &lsp, &lst, &lsv, &o, &h, &l, &bd, &bv, &ak, &av, &cv) ;
I suspect the format specifiers are incorrect, since I am not reading
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |