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

Friday, March 30, 2012

How Can I Rename a Reportin Service Folder using Code

Using Reporting Service Web service

It is possible to create folder and delete folder. But I couldn't rename the folder using code it tried with setProperties method but name is ready only property. Please help me to resolve the issue.

Thanks in Advance.

Krishna kumar.M

hi all,

Later I found if use the rs.MoveItem we can achive the same

sql

How can I remove a duplicate row?

I have a table that has two rows in it that are identical and need to
delete one of them. Since any kind of where clause in a delete
statement will hit both of them, how can I remove one of these rows?
TIAPick the dups by grouping and put them in a temp table. remove all
duplicate records from the parent table and insert the groped
duplicates (in this table, you should have one occourance of each
record) from the temp table into this old table that dosent have any
rows of these duplicate records.
Hope I didnt loose you with my wording.. ..! (It would help if you had
given some DDL code...!|||http://www.aspfaq.com/2431
Now how about having a key and/or other constraints to prevent this from
happening again?
http://www.aspfaq.com/2509
--
http://www.aspfaq.com/
(Reverse address to reply.)
"Matthew Speed" <mspeed@.mspeed.net> wrote in message
news:7pgav09liocekdk7rdgd2leo1gnuct58n3@.4ax.com...
> I have a table that has two rows in it that are identical and need to
> delete one of them. Since any kind of where clause in a delete
> statement will hit both of them, how can I remove one of these rows?
> TIA|||On 24 Jan 2005 11:08:44 -0800, "QueryBuilder" <pg.242w@.gmail.com>
wrote:
>Pick the dups by grouping and put them in a temp table. remove all
>duplicate records from the parent table and insert the groped
>duplicates (in this table, you should have one occourance of each
>record) from the temp table into this old table that dosent have any
>rows of these duplicate records.
>Hope I didnt loose you with my wording.. ..! (It would help if you had
>given some DDL code...!
This makes sense. I was hoping I could remove the extra in place. (My
thinking is that if the DB knows there are two rows there must be
someway that it uniquely identifies them.)|||Matthew Speed <mspeed@.mspeed.net> wrote in
news:7pgav09liocekdk7rdgd2leo1gnuct58n3@.4ax.com:
> I have a table that has two rows in it that are identical and need to
> delete one of them. Since any kind of where clause in a delete
> statement will hit both of them, how can I remove one of these rows?
> TIA
set rowcount 1
delete from t1
where col1=1 and col2=1|||On Mon, 24 Jan 2005 14:08:04 -0500, "Aaron [SQL Server MVP]"
<ten.xoc@.dnartreb.noraa> wrote:
>http://www.aspfaq.com/2431
>Now how about having a key and/or other constraints to prevent this from
>happening again?
The keys are now in place. This was a client database I was hired to
do some work on. When I saw the problem my first thought was to add
constraints but one can't implement a unique constraint unless the
existing data is already unique.
>http://www.aspfaq.com/2509|||"Matthew Speed" <mspeed@.mspeed.net> wrote in message
news:cjmav0dg4s0uki574285s1rekljfod4lfa@.4ax.com...
> On Mon, 24 Jan 2005 14:08:04 -0500, "Aaron [SQL Server MVP]"
> <ten.xoc@.dnartreb.noraa> wrote:
> >http://www.aspfaq.com/2431
> >
> >Now how about having a key and/or other constraints to prevent this
from
> >happening again?
> The keys are now in place. This was a client database I was hired
to
> do some work on. When I saw the problem my first thought was to add
> constraints but one can't implement a unique constraint unless the
> existing data is already unique.
I add identitiy columns to tables all the time to get rid of dups. You
can leave the identitiy column around or simply remove it after you
are done. Another technique is to create a new table as select
distinct * from xxx, drop the old one and rename the new one as
appropriately.|||> I add identitiy columns to tables all the time to get rid of dups.
With sensible design, you won't have dupes at all. Does your data not have
a candidate key? Do you not use primary keys for any reason?|||"Aaron [SQL Server MVP]" <ten.xoc@.dnartreb.noraa> wrote in message
news:uVkkXtmAFHA.2792@.TK2MSFTNGP15.phx.gbl...
> > I add identitiy columns to tables all the time to get rid of dups.
> With sensible design, you won't have dupes at all. Does your data
not have
> a candidate key? Do you not use primary keys for any reason?
Data is often loaded in a database before being transformed and
duplicates removed. This is what happens in the real world.|||> Data is often loaded in a database before being transformed and
> duplicates removed. This is what happens in the real world.
The real world? What is that? Glad my job is inside a vacuum, where we
load data into a staging table, insert the NEW, NON-DUPLICATE rows into our
production tables, then blow away or archive the staging data. (Rather than
just throwing everything into our production tables, and removing duplicates
there.)|||"Aaron [SQL Server MVP]" <ten.xoc@.dnartreb.noraa> wrote in message
news:uHq19RnAFHA.2572@.TK2MSFTNGP10.phx.gbl...
> > Data is often loaded in a database before being transformed and
> > duplicates removed. This is what happens in the real world.
> The real world? What is that? Glad my job is inside a vacuum,
where we
> load data into a staging table
Now, think about your "staging" table and the tables I am talking
about. Is the light coming on yet?|||> Now, think about your "staging" table and the tables I am talking
> about. Is the light coming on yet?
Funny that you removed the rest of my description, which kind of usurps your
petty insult. Grow up.|||"Aaron [SQL Server MVP]" <ten.xoc@.dnartreb.noraa> wrote in message
news:eBY9JUoAFHA.1404@.TK2MSFTNGP11.phx.gbl...
> > Now, think about your "staging" table and the tables I am talking
> > about. Is the light coming on yet?
> Funny that you removed the rest of my description, which kind of
usurps your
> petty insult. Grow up.
Sorry if I struck a nerve. I only responded to your post in the same
"tone" that was directed at me. Concerning the part of your post that
was redacted it was simply irrelevant. If you still don't get it I'm
not sure I can dumb it down any further. Sorry :(
--== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==--
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
--= East/West-Coast Server Farms - Total Privacy via Encryption =--|||> was redacted it was simply irrelevant. If you still don't get it I'm
> not sure I can dumb it down any further. Sorry :(
Oh christ. You know what everybody? I am sick and tired of helping people
to end up dealing with this childish crap. I have better things to do with
my time than to be at the wrong end of someone's childish recess attacks.
See ya.|||"Aaron [SQL Server MVP]" <ten.xoc@.dnartreb.noraa> wrote in message
news:ebL2Z7oAFHA.1400@.TK2MSFTNGP11.phx.gbl...
> > was redacted it was simply irrelevant. If you still don't get it
I'm
> > not sure I can dumb it down any further. Sorry :(
> Oh christ. You know what everybody? I am sick and tired of helping
people
> to end up dealing with this childish crap. I have better things to
do with
> my time than to be at the wrong end of someone's childish recess
attacks.
> See ya.
You're actually trying to help?
Here's some helpful advice.
If you're going to post with this attitude of condescension, at least
get your facts right. You might consider thinking a bit more about the
questions and replys before posting. You also seem to enjoy putting
people on the defensive and when you can't get your way you end up
lashing out like a child.
I'll let you get you the last word in since you seem intent on
"helping" me until I give up.|||/*select duplicate rows
select count(trnno), trnno from dw_ndls_train
group by trnno
having count(trnno) > 1
/*del duplicate rows
delete from dw_ndls_train
where (trnno, trname)
not in
( select min(trnno), trname
from dw_ndls_train group by trname
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Monday, March 26, 2012

How can I programatically delete records in tables...

Ok,
In my c# app, i have a string with an invoice number: "str = 12345".
Using sqlserver2k:
first of all, i understand the c# requirments for putting a command
string in, but don't know the sql command.
this is what i need:
tblPrintInvoice has one record with this column_invoice_number#.
tblInvoiceDetails has maybe up to 50.
How can I delete str(12345) in both tables most efficiently?
Also, i may need to put a flag in each record instead of deleting
them...like:
column_invoice_flag for all 12345 records gets an "insert?" of (reprint
- don't delete) instead of deleting.
any help is appreciated.
thanks,
trintHere are two examples:
DELETE FROM tblInvoiceDetails
WHERE invoice_number = '12345'
UPDATE tblInvoiceDetails
SET deleted = 'Y'
WHERE invoice_number = '12345'
Lookup the DELETE and UPDATE statements in Books Online for the full
syntax.
David Portas
SQL Server MVP
--

Monday, March 12, 2012

How can I keep only the top ten rows and delete the rest

I have a table that I would like to only keep the top 10 rows for each username. How can I kep the top 10 and delete the rest?

You could do something like

DELETE FROM <table> where ID not in (SELECT TOP 10 id from <table>)

So if the results were 1-10 any row with an id column > 10 would be deleted.

|||

Delete MainTable where id not in (select top 10 id from MainTable)

I assumed that "id" is the primary key column

|||

Doug,

That helped, however, it is more complicated than that. Heres the summary; I have a table that keeps track of the clients records that users open. It puts the username, clientid, lastviewed (datetime), and the crvid (key) into table tblclientlastviewed. I want to periodically remove all but the top ten rows based on clientid for each username. The user could open a client several times in a day, so I don't want ten entries for the same client for that user. I can easily correct this in the UI by using a group by in the query. But the removing of the extra rows in the table is much more challenging.

Do you have any ideas?

|||

You DONT want to use IN.

You can get the top 10 rows into some temp table, empty the original table (you can use TRUNCATE if you dont have identity identity columns or resetting identity columns does not cause problems), then get the 10 rows back into this table from temp table.

|||

If I understand your question, I would actually do this differently than the other posts. I'm not sure what you mean by top 10, but I assume you want the last 10 rows to exist -- to only keep 10 rows at a time. If this is the case I would use a SQL trigger. Basically at any given time you want 10 rows max per user. So on Insert you would use a SQL trigger with the same SQL that others have posted to delete the other rows.

edit:
Seems as if this comment grew stale as it sat on my screen. by the time i posted it it wasn't really relevant anymore... sorry

|||

Are you looking for a single SQL statement? It seems like it would be an easy thing to handle with a TSQL script:

1. Create a cursor for the users:SELECT username, clientid, count(*) FROM table GROUP BY username, clientid HAVING count(*) > 10

2. Loop thru each user/client, and holdSELECT TOP 10 crvid FROM table WHERE username = @.username AND clientid = @.clientid ORDER BY lastviewed DESC in a temp table or table variable

3. Delete records for each user/client not in the holding table

It's not elegant, but it would get the job done.

|||

CURSORS/TRIGGERS ?

God!!! Save me !!! right now !!!

|||

This is actually quite an advanced query.

Yes, you could use IN queries, especially if you only want to perform the query for one user.

DELETE FROM tblclientlastviewed WHERE clientid=@.clientid AND crvid NOT IN (SELECT TOP 10 crvid FROM tblclientlastviewed WHERE clientid=@.clientid ORDER BY crvid DESC)

This query could of course be executed in a cursor iterating over all the users. It's not beautiful, but easy to understand and maintain.

A third option is to use a smarter query. In the example below I've used a table variable to hold the values that should remain, in order to be able to truncate the table (more efficient than deleting individual rows).

Assuming that crvid is used as an identity. I strongly recommend executing this in a transaction

DECLARE @.remainder TABLE
(
clientid int,
username varchar(50),
lastviewed datetime
)

INSERT INTO @.remainder (clientid,username,lastviewed)
SELECT clientid,username,lastviewed FROM tblclientlastviewed t1
WHERE (SELECT COUNT(*) FROM tblclientlastviewed t2 WHERE t1.clientid=t2.clientid AND t2.crvid>t1.crvid)<10

TRUNCATE TABLE tblclientlastviewed

INSERT INTO tblclientlastviewed (clientid,username,lastviewed)
SELECT clientid,username,lastviewed FROM @.remainder

It would alse be possible to do it in one query, but if there's a lot of data, this may be very inefficient.

DELETE FROM tblclientlastviewed
WHERE crvid NOT IN (
SELECT crvid FROM tblclientlastviewed t1
WHERE (SELECT COUNT(*) FROM tblclientlastviewed t2 WHERE t1.clientid=t2.clientid AND t2.crvid>t1.crvid)<10
)

|||

I think what I will try and do is to:

get the rows into a temp table that I want to keep, per user

remove all rows for that user

insert back into the table what is in the temp table.

as suggested.

I don't know what is the best practise for this.

|||

I agree with gunteman.

You can schedule a job that runs every X hours/minutes and cleans up the table.

|||

Except that TRUNCATE statements aren't transactional, kudos togunteman for giving so many options

|||

doyleits:

Except that TRUNCATE statements aren't transactional

Good catch! Indeed they aren't. Oh well, the strategy should be quite performant even with a delete operation.

|||

So does this mean that a job would be better than a trigger? If yes, how can I create a job using Management Studio Express?

|||

Jackxxx:

So does this mean that a job would be better than a trigger? If yes, how can I create a job using Management Studio Express?

Yes, Triggers are not used for this purpose.In your Object Explorer, go to SQL Server Agent -> Jobs -> Right click -> New Job.