Showing posts with label delete. Show all posts
Showing posts with label delete. Show all posts

Wednesday, March 21, 2012

How to eliminate duplicate data

I have a table with 68 columns. If all the columns hold the same value except for one which is a datetime column I want to delete all but one of the duplicate rows. Preferably the latest one but that is not important. Can someone show me how to accomplish this?

You can use the Group By fucntion

SELECT col_a, col_b, col_c From Table GROUP BY col_a, col_b, col_c

If you want the newest date - you can also using HAVING Clause

SELECT col_a, col_b, col_c From Table GROUP BY col_a, col_b, col_c HAVING max(col_date)

WHERE col_a - col_c are the columns with the same data and col_date is your date column

AWAL

|||Is this a easier process if I manually delete them? I just want to view the duplicate data but my problem is that the DateTime column in my table is unique unlike all the other columns with the same values.|||

John -

I'm not quite certain what you mean but if your datetime column is not unique but you want dups of all the other columns just don't group by the datetime column.

AWAL

|||

Assuming that your data isn't too big, you can use a technique like this:

drop table removeDups
go
create table removeDups(
column1 int,
column2 int,
column3 int,
column4 int,
column5 int,
column6 int,
column7 int,
column8 int,
column9 int,
datevalue datetime)

insert removeDups
select 1,1,1,1,1,1,1,1,1,getdate()
waitfor delay '00:00:01'
insert removeDups
select 1,1,1,1,1,1,1,1,1,getdate()
waitfor delay '00:00:01'
insert removeDups
select 1,1,1,1,1,1,1,1,1,getdate()
waitfor delay '00:00:01'

insert removeDups
select 2,2,2,2,2,2,2,2,2,getdate()
waitfor delay '00:00:01'
insert removeDups
select 2,2,2,2,2,2,2,2,2,getdate()
waitfor delay '00:00:01'
insert removeDups
select 3,3,3,3,3,3,3,3,3,getdate()
waitfor delay '00:00:01'


delete from removeDups
where not exists
(select *
from ( select min(dateValue) as dateValue,column1,column2,column3,column4,column5,column6, column7, column8, column9
from removeDups
group by column1,column2,column3,column4,column5,column6, column7, column8, column9) as mins
where mins.dateValue = removeDups.dateValue
and mins.column1 = removeDups.column1
and mins.column2 = removeDups.column2
and mins.column3 = removeDups.column3
and mins.column4 = removeDups.column4
and mins.column5 = removeDups.column5
and mins.column6 = removeDups.column6
and mins.column7 = removeDups.column7
and mins.column8 = removeDups.column8
and mins.column9 = removeDups.column9)

select *
from removeDups

I figure once you finish this process you will probably want to hurt the person who gave you this design, even if it is yourself. Try to identify a key amongst the 68 columns and add a unique constraint so you can never get in this position again :)

Friday, March 9, 2012

How to drop a user defined database role in 2005?

Using Studio, I created a user defined database role but I can not delete it because

"TITLE: Microsoft SQL Server Management Studio

Drop failed for DatabaseRole 'test1'. (Microsoft.SqlServer.Smo)

ADDITIONAL INFORMATION:

An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)

The database principal owns a schema in the database, and cannot be dropped. (Microsoft SQL Server, Error: 15138)

I am quite annoyed because the "owned schema" is db_owner, which can not be unselected. Quite an innovation. How do I drop this relationship?

i think the DB_Owner schema is owned by this role

run this statement to see the schema and owner ... check whether this role is the owner of any schema

SELECT s.name SchemaName, d.name SchemaOwnerName FROM sys.schemas s INNER JOIN sys.database_principals d ON s.principal_id= d.principal_id

if this role owner of DB_Owner Schema run the below statment to transfer the owner ship..

ALTER AUTHORIZATION ON SCHEMA::[db_owner] TO [db_owner]

Drop role test1

Madhu

|||Nice and simple. Does the trick. Thanks Madhu.

Cheers,

Sameer.

How to drop a user defined database role in 2005?

Using Studio, I created a user defined database role but I can not delete it because

"TITLE: Microsoft SQL Server Management Studio

Drop failed for DatabaseRole 'test1'. (Microsoft.SqlServer.Smo)

ADDITIONAL INFORMATION:

An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)

The database principal owns a schema in the database, and cannot be dropped. (Microsoft SQL Server, Error: 15138)

I am quite annoyed because the "owned schema" is db_owner, which can not be unselected. Quite an innovation. How do I drop this relationship?

i think the DB_Owner schema is owned by this role

run this statement to see the schema and owner ... check whether this role is the owner of any schema

SELECT s.name SchemaName, d.name SchemaOwnerName FROM sys.schemas s INNER JOIN sys.database_principals d ON s.principal_id= d.principal_id

if this role owner of DB_Owner Schema run the below statment to transfer the owner ship..

ALTER AUTHORIZATION ON SCHEMA::[db_owner] TO [db_owner]

Drop role test1

Madhu

|||Nice and simple. Does the trick. Thanks Madhu.

Cheers,

Sameer.

Sunday, February 19, 2012

How to do bulk Delete

I have a table that has a primary key made of 3 fields (we don't want to use a surrogate key in this situation). In a particular process there is a work table that contains these 3 PK fields and we want to bulk delete them from the base table. Without looping thru the work table, how can I write a Delete statement to delete records in the base table using the work table rows as the criteria?

This syntax illustrates what I want to do, but is not allowed by SQL.

DELETE bt.* FROM basetable bt
INNER JOIN #work wk On bt.fld1 = wk.fld1 And bt.fld2 = wk.fld2 And bt.fld3 = wk.fld3

The correct DELETE statement using TSQL extension is below:

DELETE basetable

FROM basetable bt
INNER JOIN #work wk

ON bt.fld1 = wk.fld1 And bt.fld2 = wk.fld2 And bt.fld3 = wk.fld3

But best is to use the ANSI SQL syntax which doesn't have any ambiguity:

DELETE FROM basetable

WHERE EXISTS(SELECT * FROM #work as wk

WHERE wk.fld1 = basetable.fld1

AND wk.fld2 = basetable.fld2

AND wk.fld3 = basetable.fld3)

|||Ahhh!!! I tried so many variations .... except that one. Thanks.Big Smile

how to do a complete backup of SQL Server 2000

Hi,
we are starting to deploy SQL Server 2000 and in the SQL
Server Book it said that regular backups of SQL Server are
important in order to delete transaction logs.
I have done a backup of 2 of our databases, but the
transaction logs remain the same size - why is that?
The second thing I would like to ask, is backup possible
only for single databases? And how can I for example
backup the security logins,...?
Thanks for reply.
Bodo> I have done a backup of 2 of our databases, but the
> transaction logs remain the same size - why is that?
A backup of the database does nothing to the data within the transaction
log, you need to issue backup log command for this. If you do not require
transactional recovery, set your database to simple recovery mode.
> The second thing I would like to ask, is backup possible
> only for single databases?
Yes, backup database, see "BACKUP, BACKUP (described)" in BOL for syntax
>And how can I for example
> backup the security logins,...?
This page is the best for that, it will script the permissions, users, etc.
http://support.microsoft.com/default.aspx?scid=kb;en-us;246133
HTH
--
Ray Higdon MCSE, MCDBA, CCNA
--
"bodo" <bodobecker@.hotmail.com> wrote in message
news:019001c370c4$60f81d80$a501280a@.phx.gbl...
> Hi,
> we are starting to deploy SQL Server 2000 and in the SQL
> Server Book it said that regular backups of SQL Server are
> important in order to delete transaction logs.
> I have done a backup of 2 of our databases, but the
> transaction logs remain the same size - why is that?
> The second thing I would like to ask, is backup possible
> only for single databases? And how can I for example
> backup the security logins,...?
> Thanks for reply.
> Bodo