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

Wednesday, March 28, 2012

How to determine what event has caused Trigger to fire

Hello all.
I wish to use a single trigger for all Update/Insert/Delete events.
In order to do so I need a way of determining which event has caused the combined trigger to fire.
Based upon the event I can then determine which tables (Inserted or Deleted) I need to reference.
So I will have something like ;

CREATETRIGGER [Test_Trigger]

ON [dbo].[Table]

AFTER INSERT,UPDATE,DELETE

and will need to do something like ;

AS

BEGIN

IF <Insert>

Do something with INSERTED

ELSE

IF <Delete>

Do something with DELETED

I dont really want to create 3 triggers for each event
Many thanks

If INSERTED is populated and DELETED is not, then it's an insert

If INSERTED is not populated and DELETED is, then it's a delete

If both are populated, it's an UPDATE

|||

Normally, there are very distinct and different actions to be taken determined by the type of TRIGGER action. And normally, it is more efficient to have separate TRIGGERs. If you have additional code, even with good IF conditions and good flow control, you are having to execute unnecessary code EVERY time there is a table action.

Consider that you want a taxi to travel from point A to point D. Every time you travel there, the taxi goes by Point B and Point C (not a straight line) -it doesn't stop, but it goes by those places also. Was that efficient? Are you wasting time? Would you have prefered to travel straight from Point A to Point D?

The exception is for creating a history or 'audit' trail. In that case, it is usually enough to handle the 'deleted' table to store the previous data state (the current data state is in the table).

|||

You can use Inserted & Deleted logical (conceptual) tables to identify the current operation.

Yes, as Arine says you can have seperate triggers for each operation. It will reduce the additional overload of your query.

The sample as follow as,

Code Snippet

Create Trigger ..

..

..

Begin

Declare @.Flag as Int;

Select Top 1 @.Flag = 1 From Inserted;

Select Top 1 @.Flag = Isnull(@.Flag, 0) + 2 From Deleted;

If @.Flag = 1

Begin

--'Insert Operation Performed'

Return;

End

If @.Flag = 2

Begin

-- 'Delete Operation Performed'

Return;

End

If @.Flag = 3

Begin

-- 'Update Operation Performed'

Return;

End

End

|||

Thank you all for your comments.
I have done it, it is essentially what DaleJ also suggested.

This is what I did ;

DECLARE @.InsCount_T int

DECLARE @.DelCount_T int

SELECT @.InsCount_T =Count(*)FROM INSERTED

SELECT @.DelCount_T =Count(*)FROM DELETED

IF @.InsCount_T > @.DelCount_T --Insert

INSERTINTO dbo.Audit

SELECT*FROM INSERTED

ELSE

IF @.InsCount_T = @.DelCount_T --Update

INSERTINTO dbo.Audit

SELECT*FROM DELETED

ELSE

IF @.InsCount_T < @.DelCount_T --Delete

INSERTINTO dbo.Audit

SELECT*FROM DELETED
I understand what Arnie says about performance, however as with most things there is a trade-off between performance and maintainability. One trigger is easier to maintain and I dont imagine that the performance hit would be that great given the code above and the fact that a trigger will fire in any case. The only extra bit are the Counts on the tables. In my case I think a minor performance hit is acceptable given the number of users, potential additions/updates/deletes and the number of databases currently on server. But thanks for the word of warning, I'll certainly bear it in mind for more high usage db's. Manivannan's code is probably a more efficient way of doing it as it does not count all of the rows.

|||

I would add the following at the top:

IF @.@.ROWCOUNT = 0

RETURN

That will prevent the TRIGGER having to check the inserted and deleted tables needlessly when the TRIGGER is fired but there is no data available. (That can happen in a number of situations, such as CONSTRAINT failure.

Personally, I consider every little piece of code to require 'clock ticks', and I want my code to execute with the minimum 'clock ticks'. That is the attitude required in order to maximize performance efficency. In checking the count of inserted/deleted, you are wasting clock ticks.

It appears that all you are doing is making copies of inserted and deleted ino the Audit table. Why bother with the checks? Just INSERT into Audit from deleted, and then INSERT into Audit from inserted. If the virtual tables are empty, nothing happens -the results are the same with a lot less code (and wasted clock ticks.)

|||

Hi Preet,

You do not need to count rows in order to prove existence. how long it will take if you insert a mass of rows, better to use operator EXISTS.

if exists(select * from inserted) and exists(select * from deleted)

...

Also, use the tip given by Arnie, because most of your comparisons will fail if no row was affected by the statement that caused the trigger to be fired.

delete dbo.t1

where 0 = 1;

The trigger will catch:

...

ELSE

IF @.InsCount_T = @.DelCount_T --Update

INSERTINTO dbo.Audit

SELECT*FROM DELETED

and as you can see, the action was a "delete" and not an "update".

AMB

|||

Thanks to Arnie & Hunchback, its always good to 'hear' another viewpoint.
Sorry Arnie, I omitted a field that I am populating in Audit. I am using 0,1,2 to indicate the type of update. So In the case of an update I am executing ;

INSERTINTO dbo.Audit

SELECT*,1FROM DELETED (not SELECT*FROM DELETED as posted previously)

In the case of an Insert ;

INSERTINTO dbo.Audit

SELECT*,0FROM DELETED

and a delete ;

INSERTINTO dbo.Audit

SELECT*,2FROM DELETED

Yes you are right, there wouldn't be much point in the checks.

Hunchback, I didn't realise that triggers fired if no rows were affected. I'll try it out. However if no rows were affected then DELETED would be empty and it then does not matter which line in the trigger is caught because nothing will go into Audit, except for the needless execution. Tell me, does ; exists(select * from inserted) stop as soon as it finds one occurrence or does it actually evaluate the entire content of bracket ? i.e. does it actually select all of the records or does it stop as soon as it can satisfy the IF statement. If the latter is true then yes your statement is more efficient. Otherwise I will be doing Select * several times such as ;

if exists(select * from inserted) and exists(select * from deleted) --Update

if exists(select * from inserted) and not exists(select * from deleted) --Insert

if not exists(select * from inserted) and exists(select * from deleted) --Delete

I think we've kicked this one to death but I will include the use of ROWCOUNT and yes you both are right that extra work is undesirable no matter how small an impact we think it is going to have.

|||

Hi Preet,

It will stop as soon as it finds something. You can assign a value to a variable in case of existece so you do not need to inquiry again.

if @.@.rowcount = 0

return

declare @.i int

declare @.d int

if exists (select * from inserted)

set @.i = 1

else

set @.i = 0

if exists (select * from deleted)

set @.d = 1

else

set @.d = 0

if @.i = 1 and @.d = 1 then -- update

...

if @.i = 1 and @.d = 0 then -- insert

...

if @.i = 0 and @.d = 1 then -- delete

...

AMB

How to determine the last time a table was accessed ?

I'm trying to do some housekeeping. I want to delete user tables from database(s) that have not had any activity...

I cannot seem to find a mechanism for accomplishing this. sysobjects only shows the createdate, not the last time a user table had a SELECT, INSERT, UPDATE or DELETE operation performed on it.

Anyone know how to do this ?

Thanks

(P.S. this is the second posting of this question today, as I went to my threads and I do not see the original post - sorry for the duplicate, but as I say, I do not see the original so am re-posting).

randyvol

SQL server doesn't store such information and all those process is logged in transction log, and you might need third party tools in this case to audit the events or run server side trace if you want to schedule such information for time being.|||

Satya -

First and foremost thank you for your reply.

Next - what?!!? WOW! I just naturally assumed that SQL Server would do this. I cannot imagine a product that is being touted as 'ready for prime time' does not provide such basic necessities. Don't get me wrong, I really like the product, especially the 2k5 instantiation, which is why I'm even more perplexed.

How is one supposed to know over time what tables one can delete with absolute safety? I understand that 3rd party tools provide this ability, but surely they leverage something (perhaps undocumented) in the basic system? Teradata, for instance provides this information - I know, I'm a certified Teradata Master and have used that system's entries on many occasions to ascertain whether or not a table was really 'stale' and could be dropped to free up disk. I would not think it is that big a deal (or that much overhead) to have one extra column, say in sysobjects, for instance, 'last updated'.

I just cannot believe MSFT overlooked this, or expects me to cough up dollars for a 3rd party tool to do this routine maintenance chore. This is something I'd expect to find in the sys tables for sure. Doesn't have to be elegant and exposed into Studio - just basic data I can fetch with a query would suffice.

As for the tranlog.. it is transient. I'm sure that there is data there to mine, but it doesn't help me on the 100's of tables already existent on our legacy system, that have been around for years.

I sure hope MSFT decides to provide this ability soon.

(It does explain why I cannot find any documentation on how to do this though ;-)

Oh well, I guess I'll have to go build my own stuff and let it cook for a couple of quarters to see if tables are stale or not.

Regards

randyvol

Monday, March 19, 2012

How to detect a dead database

I have a database of SqlServer call myData, and it's physicial is
c:\myData.mdf.
Some one stop the SQLServer Service, then delete c:\myData.mdf, then
start the SQLService, and then the database myData is dead.
How can I detect if myData is in this state?Ad
The sysdatabases table has a column status. Read the BOL about it
http://www.karaszi.com/SQLServer/in..._suspect_db.asp
"ad" <flying@.wfes.tcc.edu.tw> wrote in message
news:OqBggcyyGHA.4408@.TK2MSFTNGP05.phx.gbl...
>I have a database of SqlServer call myData, and it's physicial is
>c:\myData.mdf.
> Some one stop the SQLServer Service, then delete c:\myData.mdf, then
> start the SQLService, and then the database myData is dead.
> How can I detect if myData is in this state?
>|||If the physical file containing the database has been deleted, then the
database is truly gone.
Do you have a backup?
Arnie Rowland, Ph.D.
Westwood Consulting, Inc
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous
"ad" <flying@.wfes.tcc.edu.tw> wrote in message
news:OqBggcyyGHA.4408@.TK2MSFTNGP05.phx.gbl...
>I have a database of SqlServer call myData, and it's physicial is
>c:\myData.mdf.
> Some one stop the SQLServer Service, then delete c:\myData.mdf, then
> start the SQLService, and then the database myData is dead.
> How can I detect if myData is in this state?
>|||> Some one stop the SQLServer Service, then delete c:\myData.mdf, then
> start the SQLService, and then the database myData is dead.
Did you hire a terrorist?
Just like the case of DBF in Foxpro, if you delete the table.dbf, there
is no way to recover it. You may wanna try Easy Data Recovery Pro to
undelete the file. Did you check the recycle bin?
Man-wai Chang
Softmedia Technology Co., Ltd.
Tel: (852)3583 2780|||I did not wnat to recover the database.
I want to confirm if the database has no physical file before delete it.
How can I confirm the database has no physical file?
"Man-wai Chang" <info@.softmedia.hk>
'?:OIcIYg0yGHA.4844@.TK2MSFTNGP04.phx.gbl...
> Did you hire a terrorist?
> Just like the case of DBF in Foxpro, if you delete the table.dbf, there is
> no way to recover it. You may wanna try Easy Data Recovery Pro to undelete
> the file. Did you check the recycle bin?
>
> --
> Man-wai Chang
> Softmedia Technology Co., Ltd.
> Tel: (852)3583 2780|||Hi,
As a first step ensure that no one have rights to SQL Server box apart from
authorised people. If you have backup you could
restore the database from Backup.
Thanks
Hari
SQL Server MVP
"ad" <flying@.wfes.tcc.edu.tw> wrote in message
news:OqBggcyyGHA.4408@.TK2MSFTNGP05.phx.gbl...
>I have a database of SqlServer call myData, and it's physicial is
>c:\myData.mdf.
> Some one stop the SQLServer Service, then delete c:\myData.mdf, then
> start the SQLService, and then the database myData is dead.
> How can I detect if myData is in this state?
>|||Hi,
Execute the command from Master database:-
DROP DATABASE <DBNAME>
This command will drop the database and close all physical MDF and LDF
Files.
Thanks
hari
SQL Server MVP
"ad" <flying@.wfes.tcc.edu.tw> wrote in message
news:ObiV0Q2yGHA.4116@.TK2MSFTNGP02.phx.gbl...
>I did not wnat to recover the database.
> I want to confirm if the database has no physical file before delete it.
> How can I confirm the database has no physical file?
>
> "Man-wai Chang" <info@.softmedia.hk>
> '?:OIcIYg0yGHA.4844@.TK2MSFTNGP04.phx.gbl...
>|||Thanks,
But how can I dertiminate if a database lost it's physicial file?
"Hari Prasad" <hari_prasad_k@.hotmail.com> glsD:ey1nz22yGHA.4204@.TK2MSFTNGP04.phx.g
bl...
> Hi,
> Execute the command from Master database:-
> DROP DATABASE <DBNAME>
> This command will drop the database and close all physical MDF and LDF
> Files.
> Thanks
> hari
> SQL Server MVP
>
> "ad" <flying@.wfes.tcc.edu.tw> wrote in message
> news:ObiV0Q2yGHA.4116@.TK2MSFTNGP02.phx.gbl...
>|||ad wrote:
> Thanks,
> But how can I dertiminate if a database lost it's physicial file?
>
If you're on SQL 2000, query the sysdatabases table to get the data file
name, then use xp_cmdshell or the undocumented xp_fileexists sproc to
see if the file exists.
Tracy McKibben
MCDBA
http://www.realsqlguy.com|||Hi,
Database will move to suspect status.
Thanks
Hari
SQL Server MVP
"ad" <flying@.wfes.tcc.edu.tw> wrote in message
news:%23uh5rS6yGHA.996@.TK2MSFTNGP03.phx.gbl...
> Thanks,
> But how can I dertiminate if a database lost it's physicial file?
>
> "Hari Prasad" <hari_prasad_k@.hotmail.com>
> glsD:ey1nz22yGHA.4204@.TK2MSFTNGP04.phx.gbl...
>

How to detect a dead database

I have a database of SqlServer call myData, and it's physicial is
c:\myData.mdf.
Some one stop the SQLServer Service, then delete c:\myData.mdf, then
start the SQLService, and then the database myData is dead.
How can I detect if myData is in this state?Ad
The sysdatabases table has a column status. Read the BOL about it
http://www.karaszi.com/SQLServer/info_corrupt_suspect_db.asp
"ad" <flying@.wfes.tcc.edu.tw> wrote in message
news:OqBggcyyGHA.4408@.TK2MSFTNGP05.phx.gbl...
>I have a database of SqlServer call myData, and it's physicial is
>c:\myData.mdf.
> Some one stop the SQLServer Service, then delete c:\myData.mdf, then
> start the SQLService, and then the database myData is dead.
> How can I detect if myData is in this state?
>|||If the physical file containing the database has been deleted, then the
database is truly gone.
Do you have a backup?
--
Arnie Rowland, Ph.D.
Westwood Consulting, Inc
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous
"ad" <flying@.wfes.tcc.edu.tw> wrote in message
news:OqBggcyyGHA.4408@.TK2MSFTNGP05.phx.gbl...
>I have a database of SqlServer call myData, and it's physicial is
>c:\myData.mdf.
> Some one stop the SQLServer Service, then delete c:\myData.mdf, then
> start the SQLService, and then the database myData is dead.
> How can I detect if myData is in this state?
>|||> Some one stop the SQLServer Service, then delete c:\myData.mdf, then
> start the SQLService, and then the database myData is dead.
Did you hire a terrorist? :)
Just like the case of DBF in Foxpro, if you delete the table.dbf, there
is no way to recover it. You may wanna try Easy Data Recovery Pro to
undelete the file. Did you check the recycle bin?
Man-wai Chang
Softmedia Technology Co., Ltd.
Tel: (852)3583 2780|||I did not wnat to recover the database.
I want to confirm if the database has no physical file before delete it.
How can I confirm the database has no physical file?
"Man-wai Chang" <info@.softmedia.hk>
'?:OIcIYg0yGHA.4844@.TK2MSFTNGP04.phx.gbl...
>> Some one stop the SQLServer Service, then delete c:\myData.mdf, then
>> start the SQLService, and then the database myData is dead.
> Did you hire a terrorist? :)
> Just like the case of DBF in Foxpro, if you delete the table.dbf, there is
> no way to recover it. You may wanna try Easy Data Recovery Pro to undelete
> the file. Did you check the recycle bin?
>
> --
> Man-wai Chang
> Softmedia Technology Co., Ltd.
> Tel: (852)3583 2780|||Hi,
As a first step ensure that no one have rights to SQL Server box apart from
authorised people. If you have backup you could
restore the database from Backup.
Thanks
Hari
SQL Server MVP
"ad" <flying@.wfes.tcc.edu.tw> wrote in message
news:OqBggcyyGHA.4408@.TK2MSFTNGP05.phx.gbl...
>I have a database of SqlServer call myData, and it's physicial is
>c:\myData.mdf.
> Some one stop the SQLServer Service, then delete c:\myData.mdf, then
> start the SQLService, and then the database myData is dead.
> How can I detect if myData is in this state?
>|||Hi,
Execute the command from Master database:-
DROP DATABASE <DBNAME>
This command will drop the database and close all physical MDF and LDF
Files.
Thanks
hari
SQL Server MVP
"ad" <flying@.wfes.tcc.edu.tw> wrote in message
news:ObiV0Q2yGHA.4116@.TK2MSFTNGP02.phx.gbl...
>I did not wnat to recover the database.
> I want to confirm if the database has no physical file before delete it.
> How can I confirm the database has no physical file?
>
> "Man-wai Chang" <info@.softmedia.hk>
> '?:OIcIYg0yGHA.4844@.TK2MSFTNGP04.phx.gbl...
>> Some one stop the SQLServer Service, then delete c:\myData.mdf, then
>> start the SQLService, and then the database myData is dead.
>> Did you hire a terrorist? :)
>> Just like the case of DBF in Foxpro, if you delete the table.dbf, there
>> is no way to recover it. You may wanna try Easy Data Recovery Pro to
>> undelete the file. Did you check the recycle bin?
>>
>> --
>> Man-wai Chang
>> Softmedia Technology Co., Ltd.
>> Tel: (852)3583 2780
>|||Thanks,
But how can I dertiminate if a database lost it's physicial file?
"Hari Prasad" <hari_prasad_k@.hotmail.com> ¼¶¼g©ó¶l¥ó·s»D:ey1nz22yGHA.4204@.TK2MSFTNGP04.phx.gbl...
> Hi,
> Execute the command from Master database:-
> DROP DATABASE <DBNAME>
> This command will drop the database and close all physical MDF and LDF
> Files.
> Thanks
> hari
> SQL Server MVP
>
> "ad" <flying@.wfes.tcc.edu.tw> wrote in message
> news:ObiV0Q2yGHA.4116@.TK2MSFTNGP02.phx.gbl...
>>I did not wnat to recover the database.
>> I want to confirm if the database has no physical file before delete it.
>> How can I confirm the database has no physical file?
>>
>> "Man-wai Chang" <info@.softmedia.hk>
>> '?:OIcIYg0yGHA.4844@.TK2MSFTNGP04.phx.gbl...
>> Some one stop the SQLServer Service, then delete c:\myData.mdf, then
>> start the SQLService, and then the database myData is dead.
>> Did you hire a terrorist? :)
>> Just like the case of DBF in Foxpro, if you delete the table.dbf, there
>> is no way to recover it. You may wanna try Easy Data Recovery Pro to
>> undelete the file. Did you check the recycle bin?
>>
>> --
>> Man-wai Chang
>> Softmedia Technology Co., Ltd.
>> Tel: (852)3583 2780
>>
>|||ad wrote:
> Thanks,
> But how can I dertiminate if a database lost it's physicial file?
>
If you're on SQL 2000, query the sysdatabases table to get the data file
name, then use xp_cmdshell or the undocumented xp_fileexists sproc to
see if the file exists.
Tracy McKibben
MCDBA
http://www.realsqlguy.com|||Hi,
Database will move to suspect status.
Thanks
Hari
SQL Server MVP
"ad" <flying@.wfes.tcc.edu.tw> wrote in message
news:%23uh5rS6yGHA.996@.TK2MSFTNGP03.phx.gbl...
> Thanks,
> But how can I dertiminate if a database lost it's physicial file?
>
> "Hari Prasad" <hari_prasad_k@.hotmail.com>
> ¼¶¼g©ó¶l¥ó·s»D:ey1nz22yGHA.4204@.TK2MSFTNGP04.phx.gbl...
>> Hi,
>> Execute the command from Master database:-
>> DROP DATABASE <DBNAME>
>> This command will drop the database and close all physical MDF and LDF
>> Files.
>> Thanks
>> hari
>> SQL Server MVP
>>
>> "ad" <flying@.wfes.tcc.edu.tw> wrote in message
>> news:ObiV0Q2yGHA.4116@.TK2MSFTNGP02.phx.gbl...
>>I did not wnat to recover the database.
>> I want to confirm if the database has no physical file before delete it.
>> How can I confirm the database has no physical file?
>>
>> "Man-wai Chang" <info@.softmedia.hk>
>> '?:OIcIYg0yGHA.4844@.TK2MSFTNGP04.phx.gbl...
>> Some one stop the SQLServer Service, then delete c:\myData.mdf, then
>> start the SQLService, and then the database myData is dead.
>> Did you hire a terrorist? :)
>> Just like the case of DBF in Foxpro, if you delete the table.dbf, there
>> is no way to recover it. You may wanna try Easy Data Recovery Pro to
>> undelete the file. Did you check the recycle bin?
>>
>> --
>> Man-wai Chang
>> Softmedia Technology Co., Ltd.
>> Tel: (852)3583 2780
>>
>>
>

Friday, March 9, 2012

how to delete?..

what codes should i use if i want to delete a record in sql using vb6?.,

because this doesnt work..

Confirm = MsgBox ("Are you sure you want to delete this record?", vbYesNo, "Deletion Confirmation")
If Confirm = vbYes Then
adodc1.Recordset.Delete
MsgBox "Record Deleted!", , "Message"
Else
MsgBox "Record Not Deleted!", , "Message"
End If

-grrr.,they are teaching us about sql now??.,
gggrrrrrr!!!!.,our skuL is useless!.,connectionObj.execute ("Delete from tablename where keyColumn=" & keyVal)|||thankyou.,:)

How to delete/drop all the tables from SQL Server Database without using Enterprise Manager?


How to delete/drop all the tables from SQL Server Database without using Enterprise Manager?

I tried using DROP Tables, Truncate Database, Delete and many more but it is not working. I want to delete all tables using Query Analyzer, i.e. through SQL Query.

Please help me out in this concern.

Nishith Shah

hi Nishith Shah

try this

EXEC sp_MSforeachtable @.command1 = "DROP TABLE ?"

this is a hidden SP in sql server, this will be executed for each table in the database you connected (you cant rollback this)

if u want to delete it from the command prompt try this

EXEC xp_cmdshell 'SQLCMD -U <user> -P <password> -Q 'EXEC sp_MSforeachtable @.command1 = "DROP TABLE ?" ' ,no_output

Best of luck.

Gurpreet S. Gill

|||Hi Gurpreet,
it worked man.......... thanx a lot for your reply!

Nishith Shah|||

Thanks man

|||Hi Gurpreet! once again.

you have shown me the perfect way to delete/drop all table using single SQL statement.

tell me if i just want to truncate/delete all the tables then how can i?

pls reply

Nishith|||

Hay man what you are asking for,if you just check my reply, the answere is there

ok, try this, this will delete/truncate all the Data from each table for in the database you connected

EXEC sp_MSforeachtable @.command1 = "DELETE FROM ?"

EXEC sp_MSforeachtable @.command1 = "TRUNCATE TABLE ?"

I too explain it now, as sp_MSforeachtable is Stored Procedure, that will execute for all the tables for database & @.command1 is variable which will run against each table for connected database, now whatever you will write in the double quotes, that will be act as a command for each table, where '?' is the name of the table.

try this, it will clear your comcepts

EXEC sp_MSforeachtable @.command1 = "SELECT * FROM ?" -- Selects all the rows form all the table

EXEC sp_MSforeachtable @.command1 = "PRINT '?'" --Just print the tables names with owner(dbo)

For more understanding, go for the MSDN or google, this is the right way.

If still you are confused do call me any time(I am an Indian, 24x7) at +91-99495-60051

Regards,

Thanks.

Gurpreet S. Gill

|||Hello Gurpreet,

thanks a lot for helping man and giving your cell # also. It worked again...
So, where r u working? as a?

do contact me anyhow on me.poison@.gmail.com or nishith82@.hotmail.com
atleast send me a blank email, i will understand its u.

thanks,
Nishith|||

You won't be able to run TRUNCATE against all tables if you have foreign keys references

Here is one way to circumvent that

-- First disable referential integrity
EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
GO

EXEC sp_MSForEachTable '
IF OBJECTPROPERTY(object_id(''?''), ''TableHasForeignRef'') = 1
DELETE FROM ?
else
TRUNCATE TABLE ?
'
GO

-- Now enable referential integrity again
EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'
GO


Denis the SQL Menace
http://sqlservercode.blogspot.com/

|||

Thanks Denis, ya these things need to consider, before applying delete/truncate command.

Regards,

Thanks.

Gurpreet S. Gill

|||

HI people

I want to do this in MS Access database . Delete all tables . Is there a hidden SP here also ? or some other way . .

Plz help

|||

hi

i cant say anything about this, better to go for the MS-Access forum.

or

if you know the visual basic you can write the macro for that.

just check this link

http://www.codecomments.com/message725983.html

Regards,

thanks.

Gurpreet S. Gill

How to delete user from a SQL server 2000 database in SQL server 2005?

Hi,

I have a database created in server 2000, and now I have moved it to server 2005.

All works do fine, but there is a user which cannot be removed.

In the user properties window, the assigned schema is empty. The user is a db_owner of the database. When I was trying to update the user, it asked me for the login. The login is empty, but the field is disabled.

So my question is, how to remove this user?

Thank you.

Jensan

Hi,

then it is probably an orphanded database user. You should use the

sp_dropuser [ sp_dropuser ] 'user'
ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/e28f18f9-7ecf-4568-89f4-fe5c520df386.htm

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de
_--

how to delete unused connection in DTS package

hi guys,
I have a dts package which has 2 unused connections, but I don't know
how to delete them using dts package designer... is that possible to do on U
I
level or
do I have to write a script to drop unused connections?
-kevI got it.. thanks anyway..
http://www.sqldts.com/default.aspx?253
"Kevin" wrote:

> hi guys,
> I have a dts package which has 2 unused connections, but I don't know
> how to delete them using dts package designer... is that possible to do on
UI
> level or
> do I have to write a script to drop unused connections?
> -kev

How to delete tmp file which created by Crystal Report automatically

Hi,
Each time I run the vb application, the crystal report will create tmp file in the C:\ and VB*.tmp in the current working dirctory. How can I delete it automatically? Now, I need to delete it manually, otherwise, the huge tmp file will remind in both directories.
ThanksKill FileName

How to delete the whole sub tree?

Please see also the problem at:
http://blog.joycode.com/mvm/articles/63479.aspx"Roger" <Roger@.discussions.microsoft.com> wrote in message
news:95C239A5-96E2-41F3-A680-EADEB362E06B@.microsoft.com...
> Please see also the problem at:
> http://blog.joycode.com/mvm/articles/63479.aspx
You can find a great deal of discussion about SQL hierarchies and subtree
maintenance online. In SQL 2000 your options are either to choose a
different hierarchy model that allows set-based subtree maintenance or to
delete the nodes recursively (using a loop or recursive triggers for
example). In SQL Server 2005 we have recursive CTEs to accomplish the same.
Here are some references:
http://www.intelligententerprise.co.../celko1_1.jhtml
http://www.dbazine.com/tropashko4.html
http://www.windowsitpro.com/SQLServ.../8826/8826.html
http://msdn.microsoft.com/library/d...r />
_5yk3.asp
http://www.amazon.com/exec/obidos/t...0220136-2726321
http://www.solidqualitylearning.com...0-%20Slides.zip
http://www.solidqualitylearning.com...-%20Scripts.zip
http://www.sqlteam.com/item.asp?ItemID=8866
http://vyaskn.tripod.com/hierarchie...r_databases.htm
http://www.yafla.com/papers/sqlhier...hierarchies.htm
David Portas
SQL Server MVP
--|||Get a copy of TREES & HIERARCHIES IN SQL for several better ways to
model this.

How to delete the uploaded files in Report Manager

Hi,

Is there a way to delete uploaded the report projects in http:/Localhost/ReportServer

Thanks,

Zixing Wang

Switch to Detail view|||

Note: the Detail view is only available in report manager (.../reports), not through the /reportserver virtual root.

-- Robert

How to delete the role by using AMO(Analysis Management Object)?

I want to delete the role by using AMO, but what I find is only a way that how to create it.

following sample is deleting the members of the role :

Code Snippet

Dim ServerName As Server 'Connect OLAP Server
Dim db As Database 'Database of OLAP Server

Dim role As Role

role = db.Roles.Item(0)
role.Members.Clear()
role.Update()

So I think that deleting the roles of database is like following :

Code Snippet

db.Roles.Clear()

db.Update()

It's wrong. It can't delete roles of a database after executing.

Please tell me what I shall do or where I can find these infoemation, thanks!!

Here's a code sample from one of our developers that does this. (Thanks for the code, Jason.) Keep in mind that in this code, we were looking for Roles that met a naming standard. It's a long story, but you may want to skip that step. Still, I kept it in here (and only changed the naming pattern) so I can insure the code still works with minimal effort. Also, keep in mind this code was written for an SSIS package so you will see some odd ball references in there.

Code Snippet

Public Sub Main()
' SSAS 2005 Server
Dim server As New Microsoft.AnalysisServices.Server
Try
' CONNECT TO THE SERVER
server.Connect("localhost")

' THE ANALYSIS SERVICES DATABASE TO CONNECT TO
Dim database As New Microsoft.AnalysisServices.Database
database = server.Databases.FindByName(Dts.Variables("AnalysisServicesDatabaseName").Value.ToString)

Dim roleCollection As New ArrayList()

' FIND ALL OF THE ROLES IN THE ANALYSIS SERVICES DB THAT MATCH THE NAMING PATTERN
For Each currentRole As Role In database.Roles
If (currentRole.Name.StartsWith("XYZ")) Then
' BECAUSE YOU CAN'T DELETE AN ITEM IN A COLLECTION, ADD TO THE roleCollection ARRAY LIST
roleCollection.Add(currentRole)
End If
Next

' DELETE ALL ROLES IN THE roleCollection
For Each currentObj As Role In roleCollection

'DROP OPTION OF AlterOrDeleteDependents SHOULD REMOVE ANY ASSOCIATED PERMISSIONS WITH THIS ROLE
currentObj.Drop(DropOptions.AlterOrDeleteDependents)
database.Update()
Next

Dts.TaskResult = Dts.Results.Success

Catch ex As Exception
Dts.Events.FireError(1, ex.TargetSite.ToString, ex.Message, "", 0)
Finally
server.Disconnect()
End Try


End Sub

|||

Thanks for your answer~~

I can delete the role now.But I find that I can skip the step of ArrayList() and delete the role.

I don't know what a risk has in these statement, like following code :

If you know that. Could you tell me,please? Thanks!!

Code Snippet

Public Sub Main()
' SSAS 2005 Server
Dim server As New Microsoft.AnalysisServices.Server
Try
' CONNECT TO THE SERVER
server.Connect("localhost")

' THE ANALYSIS SERVICES DATABASE TO CONNECT TO
Dim database As New Microsoft.AnalysisServices.Database
database = server.Databases.FindByName(Dts.Variables("AnalysisServicesDatabaseName").Value.ToString)

' DELETE A ROLES
Dim currentObj As Role

currentObj.database.Roles.FinByName("XYZ")
currentObj.Drop(DropOptions.AlterOrDeleteDependents)
database.Update()

Dts.TaskResult = Dts.Results.Success

Catch ex As Exception
Dts.Events.FireError(1, ex.TargetSite.ToString, ex.Message, "", 0)
Finally
server.Disconnect()
End Try


End Sub

How to delete the role by using AMO(Analysis Management Object)?

I want to delete the role by using AMO, but what I find is only a way that how to create it.

following sample is deleting the members of the role :

Code Snippet

Dim ServerName As Server 'Connect OLAP Server
Dim db As Database 'Database of OLAP Server

Dim role As Role

role = db.Roles.Item(0)
role.Members.Clear()
role.Update()

So I think that deleting the roles of database is like following :

Code Snippet

db.Roles.Clear()

db.Update()

It's wrong. It can't delete roles of a database after executing.

Please tell me what I shall do or where I can find these infoemation, thanks!!

Here's a code sample from one of our developers that does this. (Thanks for the code, Jason.) Keep in mind that in this code, we were looking for Roles that met a naming standard. It's a long story, but you may want to skip that step. Still, I kept it in here (and only changed the naming pattern) so I can insure the code still works with minimal effort. Also, keep in mind this code was written for an SSIS package so you will see some odd ball references in there.

Code Snippet

Public Sub Main()
' SSAS 2005 Server
Dim server As New Microsoft.AnalysisServices.Server
Try
' CONNECT TO THE SERVER
server.Connect("localhost")

' THE ANALYSIS SERVICES DATABASE TO CONNECT TO
Dim database As New Microsoft.AnalysisServices.Database
database = server.Databases.FindByName(Dts.Variables("AnalysisServicesDatabaseName").Value.ToString)

Dim roleCollection As New ArrayList()

' FIND ALL OF THE ROLES IN THE ANALYSIS SERVICES DB THAT MATCH THE NAMING PATTERN
For Each currentRole As Role In database.Roles
If (currentRole.Name.StartsWith("XYZ")) Then
' BECAUSE YOU CAN'T DELETE AN ITEM IN A COLLECTION, ADD TO THE roleCollection ARRAY LIST
roleCollection.Add(currentRole)
End If
Next

' DELETE ALL ROLES IN THE roleCollection
For Each currentObj As Role In roleCollection

'DROP OPTION OF AlterOrDeleteDependents SHOULD REMOVE ANY ASSOCIATED PERMISSIONS WITH THIS ROLE
currentObj.Drop(DropOptions.AlterOrDeleteDependents)
database.Update()
Next

Dts.TaskResult = Dts.Results.Success

Catch ex As Exception
Dts.Events.FireError(1, ex.TargetSite.ToString, ex.Message, "", 0)
Finally
server.Disconnect()
End Try


End Sub

|||

Thanks for your answer~~

I can delete the role now.But I find that I can skip the step of ArrayList() and delete the role.

I don't know what a risk has in these statement, like following code :

If you know that. Could you tell me,please? Thanks!!

Code Snippet

Public Sub Main()
' SSAS 2005 Server
Dim server As New Microsoft.AnalysisServices.Server
Try
' CONNECT TO THE SERVER
server.Connect("localhost")

' THE ANALYSIS SERVICES DATABASE TO CONNECT TO
Dim database As New Microsoft.AnalysisServices.Database
database = server.Databases.FindByName(Dts.Variables("AnalysisServicesDatabaseName").Value.ToString)

' DELETE A ROLES
Dim currentObj As Role

currentObj.database.Roles.FinByName("XYZ")
currentObj.Drop(DropOptions.AlterOrDeleteDependents)
database.Update()

Dts.TaskResult = Dts.Results.Success

Catch ex As Exception
Dts.Events.FireError(1, ex.TargetSite.ToString, ex.Message, "", 0)
Finally
server.Disconnect()
End Try


End Sub

How to delete the reduplicate row in a table?

There are two columns in the table1,
ID|AccountName | ContactName |
1 | ebay.com | Alex
2 | ebay.com |
Look the second row, it is reduplicate info which is absolutely same with
the first row. So I'd like to delete the second row. How to use SQL to do
that?
Cheers,
Jim
DELETE FROM
SOMETABLE ST
WHERE yourid Column >
(SELECT youridcolumn From sometable ST2 where
ST1.col1 = ST2.col1 AND ST1.col2 = ST2.col2)
--Where
ST1.col1 = ST2.col1 AND ST1.col2 = ST2.col2 is the criteria that matches the
exact data. perhaps put that in a transaction to see if it works
BEGIN TRANSACTION
DELETE ...
Select ... --See the result
--Then Commit or Rollback if not as excpected
HTH, Jens Suessmeyer.
"CEO" wrote:

> There are two columns in the table1,
> ID|AccountName | ContactName |
> 1 | ebay.com | Alex
> 2 | ebay.com |
>
> Look the second row, it is reduplicate info which is absolutely same with
> the first row. So I'd like to delete the second row. How to use SQL to do
> that?
> Cheers,
> Jim
|||That should be:

> DELETE FROM
> SOMETABLE ST
> WHERE yourid Column >
> (SELECT MIN(youridcolumn) From sometable ST2 where
> ST1.col1 = ST2.col1 AND ST1.col2 = ST2.col2)
"Jens Sü?meyer" wrote:
[vbcol=seagreen]
> DELETE FROM
> SOMETABLE ST
> WHERE yourid Column >
> (SELECT youridcolumn From sometable ST2 where
> ST1.col1 = ST2.col1 AND ST1.col2 = ST2.col2)
> --Where
> ST1.col1 = ST2.col1 AND ST1.col2 = ST2.col2 is the criteria that matches the
> exact data. perhaps put that in a transaction to see if it works
> BEGIN TRANSACTION
> DELETE ...
> Select ... --See the result
> --Then Commit or Rollback if not as excpected
> HTH, Jens Suessmeyer.
> "CEO" wrote:
|||Thanks Jens,
I noticed this command: (SELECT MIN(youridcolumn) From sometable ST2 where
which is not what I wanted, becasue I want to remove the row which has NO
ContactName included. It maybe not the MIN(youridcolumn).
So what can I do?
|||CEO, This might do what you want:
delete from auction
where id in (select a1.id
from auction a1, auction a2
where a1.account = a2.account
and a1.contact = ' ' and a2.contact != ' ')
"CEO" wrote:

> Thanks Jens,
> I noticed this command: (SELECT MIN(youridcolumn) From sometable ST2 where
> which is not what I wanted, becasue I want to remove the row which has NO
> ContactName included. It maybe not the MIN(youridcolumn).
> So what can I do?
|||You told that the rows are about THE SAME, you didn′t mentioned something of
a missing contact name, I thought this was just a copy & paster error from
you.
"CEO" wrote:

> Thanks Jens,
> I noticed this command: (SELECT MIN(youridcolumn) From sometable ST2 where
> which is not what I wanted, becasue I want to remove the row which has NO
> ContactName included. It maybe not the MIN(youridcolumn).
> So what can I do?
|||CEO, In case you have more than 2 duplicate rows , i slightly modified the
SQL statement:
create table auction(
id int,
account varchar(25),
contact varchar(25)
)
go
insert into auction values(1, 'ebay.com', 'Alex')
insert into auction values(2, 'ebay.com', '')
insert into auction values(3, 'aol.com', 'Alexsey')
insert into auction values(4, 'aol.com', '')
insert into auction values(5, 'aol.com', '')
delete from auction
where id in (select distinct a1.id
from auction a1, auction a2
where a1.account = a2.account
and a1.contact = ' ' and a2.contact != ' ')
"CEO" wrote:

> Thanks Jens,
> I noticed this command: (SELECT MIN(youridcolumn) From sometable ST2 where
> which is not what I wanted, becasue I want to remove the row which has NO
> ContactName included. It maybe not the MIN(youridcolumn).
> So what can I do?

How to delete the reduplicate row in a table?

There are two columns in the table1,
ID|AccountName | ContactName |
1 | ebay.com | Alex
2 | ebay.com |
Look the second row, it is reduplicate info which is absolutely same with
the first row. So I'd like to delete the second row. How to use SQL to do
that?
Cheers,
JimDELETE FROM
SOMETABLE ST
WHERE yourid Column >
(SELECT youridcolumn From sometable ST2 where
ST1.col1 = ST2.col1 AND ST1.col2 = ST2.col2)
--Where
ST1.col1 = ST2.col1 AND ST1.col2 = ST2.col2 is the criteria that matches the
exact data. perhaps put that in a transaction to see if it works
BEGIN TRANSACTION
DELETE ...
Select ... --See the result
--Then Commit or Rollback if not as excpected
HTH, Jens Suessmeyer.
"CEO" wrote:
> There are two columns in the table1,
> ID|AccountName | ContactName |
> 1 | ebay.com | Alex
> 2 | ebay.com |
>
> Look the second row, it is reduplicate info which is absolutely same with
> the first row. So I'd like to delete the second row. How to use SQL to do
> that?
> Cheers,
> Jim|||That should be:
> DELETE FROM
> SOMETABLE ST
> WHERE yourid Column >
> (SELECT MIN(youridcolumn) From sometable ST2 where
> ST1.col1 = ST2.col1 AND ST1.col2 = ST2.col2)
"Jens Sü�meyer" wrote:
> DELETE FROM
> SOMETABLE ST
> WHERE yourid Column >
> (SELECT youridcolumn From sometable ST2 where
> ST1.col1 = ST2.col1 AND ST1.col2 = ST2.col2)
> --Where
> ST1.col1 = ST2.col1 AND ST1.col2 = ST2.col2 is the criteria that matches the
> exact data. perhaps put that in a transaction to see if it works
> BEGIN TRANSACTION
> DELETE ...
> Select ... --See the result
> --Then Commit or Rollback if not as excpected
> HTH, Jens Suessmeyer.
> "CEO" wrote:
> > There are two columns in the table1,
> >
> > ID|AccountName | ContactName |
> > 1 | ebay.com | Alex
> > 2 | ebay.com |
> >
> >
> > Look the second row, it is reduplicate info which is absolutely same with
> > the first row. So I'd like to delete the second row. How to use SQL to do
> > that?
> >
> > Cheers,
> > Jim|||Thanks Jens,
I noticed this command: (SELECT MIN(youridcolumn) From sometable ST2 where
which is not what I wanted, becasue I want to remove the row which has NO
ContactName included. It maybe not the MIN(youridcolumn).
So what can I do?|||CEO, This might do what you want:
delete from auction
where id in (select a1.id
from auction a1, auction a2
where a1.account = a2.account
and a1.contact = ' ' and a2.contact != ' ')
"CEO" wrote:
> Thanks Jens,
> I noticed this command: (SELECT MIN(youridcolumn) From sometable ST2 where
> which is not what I wanted, becasue I want to remove the row which has NO
> ContactName included. It maybe not the MIN(youridcolumn).
> So what can I do?|||You told that the rows are about THE SAME, you didn´t mentioned something of
a missing contact name, I thought this was just a copy & paster error from
you.
"CEO" wrote:
> Thanks Jens,
> I noticed this command: (SELECT MIN(youridcolumn) From sometable ST2 where
> which is not what I wanted, becasue I want to remove the row which has NO
> ContactName included. It maybe not the MIN(youridcolumn).
> So what can I do?|||CEO, In case you have more than 2 duplicate rows , i slightly modified the
SQL statement:
create table auction(
id int,
account varchar(25),
contact varchar(25)
)
go
insert into auction values(1, 'ebay.com', 'Alex')
insert into auction values(2, 'ebay.com', '')
insert into auction values(3, 'aol.com', 'Alexsey')
insert into auction values(4, 'aol.com', '')
insert into auction values(5, 'aol.com', '')
delete from auction
where id in (select distinct a1.id
from auction a1, auction a2
where a1.account = a2.account
and a1.contact = ' ' and a2.contact != ' ')
"CEO" wrote:
> Thanks Jens,
> I noticed this command: (SELECT MIN(youridcolumn) From sometable ST2 where
> which is not what I wanted, becasue I want to remove the row which has NO
> ContactName included. It maybe not the MIN(youridcolumn).
> So what can I do?

Wednesday, March 7, 2012

How to delete the reduplicate row in a table?

There are two columns in the table1,
ID|AccountName | ContactName |
1 | ebay.com | Alex
2 | ebay.com |
Look the second row, it is reduplicate info which is absolutely same with
the first row. So I'd like to delete the second row. How to use SQL to do
that?
Cheers,
JimDELETE FROM
SOMETABLE ST
WHERE yourid Column >
(SELECT youridcolumn From sometable ST2 where
ST1.col1 = ST2.col1 AND ST1.col2 = ST2.col2)
--Where
ST1.col1 = ST2.col1 AND ST1.col2 = ST2.col2 is the criteria that matches the
exact data. perhaps put that in a transaction to see if it works
BEGIN TRANSACTION
DELETE ...
Select ... --See the result
--Then Commit or Rollback if not as excpected
HTH, Jens Suessmeyer.
"CEO" wrote:

> There are two columns in the table1,
> ID|AccountName | ContactName |
> 1 | ebay.com | Alex
> 2 | ebay.com |
>
> Look the second row, it is reduplicate info which is absolutely same with
> the first row. So I'd like to delete the second row. How to use SQL to do
> that?
> Cheers,
> Jim|||That should be:

> DELETE FROM
> SOMETABLE ST
> WHERE yourid Column >
> (SELECT MIN(youridcolumn) From sometable ST2 where
> ST1.col1 = ST2.col1 AND ST1.col2 = ST2.col2)
"Jens Sü?meyer" wrote:
[vbcol=seagreen]
> DELETE FROM
> SOMETABLE ST
> WHERE yourid Column >
> (SELECT youridcolumn From sometable ST2 where
> ST1.col1 = ST2.col1 AND ST1.col2 = ST2.col2)
> --Where
> ST1.col1 = ST2.col1 AND ST1.col2 = ST2.col2 is the criteria that matches t
he
> exact data. perhaps put that in a transaction to see if it works
> BEGIN TRANSACTION
> DELETE ...
> Select ... --See the result
> --Then Commit or Rollback if not as excpected
> HTH, Jens Suessmeyer.
> "CEO" wrote:
>|||Thanks Jens,
I noticed this command: (SELECT MIN(youridcolumn) From sometable ST2 where
which is not what I wanted, becasue I want to remove the row which has NO
ContactName included. It maybe not the MIN(youridcolumn).
So what can I do?|||CEO, This might do what you want:
delete from auction
where id in (select a1.id
from auction a1, auction a2
where a1.account = a2.account
and a1.contact = ' ' and a2.contact != ' ')
"CEO" wrote:

> Thanks Jens,
> I noticed this command: (SELECT MIN(youridcolumn) From sometable ST2 where
> which is not what I wanted, becasue I want to remove the row which has NO
> ContactName included. It maybe not the MIN(youridcolumn).
> So what can I do?|||You told that the rows are about THE SAME, you didn′t mentioned something o
f
a missing contact name, I thought this was just a copy & paster error from
you.
"CEO" wrote:

> Thanks Jens,
> I noticed this command: (SELECT MIN(youridcolumn) From sometable ST2 where
> which is not what I wanted, becasue I want to remove the row which has NO
> ContactName included. It maybe not the MIN(youridcolumn).
> So what can I do?|||CEO, In case you have more than 2 duplicate rows , i slightly modified the
SQL statement:
create table auction(
id int,
account varchar(25),
contact varchar(25)
)
go
insert into auction values(1, 'ebay.com', 'Alex')
insert into auction values(2, 'ebay.com', '')
insert into auction values(3, 'aol.com', 'Alexsey')
insert into auction values(4, 'aol.com', '')
insert into auction values(5, 'aol.com', '')
delete from auction
where id in (select distinct a1.id
from auction a1, auction a2
where a1.account = a2.account
and a1.contact = ' ' and a2.contact != ' ')
"CEO" wrote:

> Thanks Jens,
> I noticed this command: (SELECT MIN(youridcolumn) From sometable ST2 where
> which is not what I wanted, becasue I want to remove the row which has NO
> ContactName included. It maybe not the MIN(youridcolumn).
> So what can I do?

How to delete the log shipping on the sql server2000?

I have two servers installed sql server2000,i setuped log shipping on every
server!but now how to do delete log shipping?
--
Study everyday!Hi
Have you checked out the sp_delete_log_shipping... procedures including
sp_delete_log_shipping_monitor_info?
John
"lansehai-chen@.hotmail.com" wrote:

> I have two servers installed sql server2000,i setuped log shipping on ever
y
> server!but now how to do delete log shipping?
> --
> Study everyday!

How to delete the log shipping on the sql server2000?

I have two servers installed sql server2000,i setuped log shipping on every
server!but now how to do delete log shipping?
--
Study everyday!Hi
Have you checked out the sp_delete_log_shipping... procedures including
sp_delete_log_shipping_monitor_info?
John
"lansehai-chen@.hotmail.com" wrote:
> I have two servers installed sql server2000,i setuped log shipping on every
> server!but now how to do delete log shipping?
> --
> Study everyday!

how to delete the day before bak file created by sql

the below script in sql backup runs fine, however i need it to delete the 1
day prior bak first, before it starts the next day. its like this for
redundicy. but i dont have the space to perform the next day job because the
day before is on the drive. we bakup to tape every night. so we have a copy
of the bak file. i just need to buy some time before i replace the drive
sizes.
EXECUTE master..xp_sqlmaint '-D
PracticeManager -WriteHistory -BkUpOnlyIfClean -CkDB -BkUpMedia
DISK -BkUpDB -UseDefDir -BkExt "BAK" -VrfyBackup -DelBkUps
1DAYS -UpdOptiStats 15'
could i just create a new job to just delbkups 1 days ... and arange it to
go first (1) and on success goto (2)
please help
Am sure this can be done in a round about way. Can use xp_cmdshell to delete
the file before initiating the backup call. this way your old backup is
deleted and the backup process can succeed.
HTH,
Vinod Kumar
MCSE, DBA, MCAD, MCSD
http://www.extremeexperts.com
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
"AA" <jgrace@.digitelusa.net> wrote in message
news:%23haziuX1EHA.2716@.TK2MSFTNGP14.phx.gbl...
> the below script in sql backup runs fine, however i need it to delete the
1
> day prior bak first, before it starts the next day. its like this for
> redundicy. but i dont have the space to perform the next day job because
the
> day before is on the drive. we bakup to tape every night. so we have a
copy
> of the bak file. i just need to buy some time before i replace the drive
> sizes.
> EXECUTE master..xp_sqlmaint '-D
> PracticeManager -WriteHistory -BkUpOnlyIfClean -CkDB -BkUpMedia
> DISK -BkUpDB -UseDefDir -BkExt "BAK" -VrfyBackup -DelBkUps
> 1DAYS -UpdOptiStats 15'
>
> could i just create a new job to just delbkups 1 days ... and arange it to
> go first (1) and on success goto (2)
>
> please help
>
|||In this situation we run a job that uses xp_cmdshell to delete the backup
file before our xp_sqlmaint is run. Might be a nice feature if the backup
job could offer to delete old backups before running a new backup.
Chris Wood
Alberta Department of Energy
CANADA
"AA" <jgrace@.digitelusa.net> wrote in message
news:%23haziuX1EHA.2716@.TK2MSFTNGP14.phx.gbl...
> the below script in sql backup runs fine, however i need it to delete the
1
> day prior bak first, before it starts the next day. its like this for
> redundicy. but i dont have the space to perform the next day job because
the
> day before is on the drive. we bakup to tape every night. so we have a
copy
> of the bak file. i just need to buy some time before i replace the drive
> sizes.
> EXECUTE master..xp_sqlmaint '-D
> PracticeManager -WriteHistory -BkUpOnlyIfClean -CkDB -BkUpMedia
> DISK -BkUpDB -UseDefDir -BkExt "BAK" -VrfyBackup -DelBkUps
> 1DAYS -UpdOptiStats 15'
>
> could i just create a new job to just delbkups 1 days ... and arange it to
> go first (1) and on success goto (2)
>
> please help
>
|||Chris Wood wrote:
> In this situation we run a job that uses xp_cmdshell to delete the backup
> file before our xp_sqlmaint is run. Might be a nice feature if the backup
> job could offer to delete old backups before running a new backup.
Hi,
It is not a good idea to remove the previous backup before you know the
new backup is ok. If the new backup fails you don't have a spare one.
Jo.
|||Very true. But in the case of not enough disk space for more than 1 backup
and the backup file backed up to some other media it could still be a nice
option of the backup plan.
Chris Wood
"Jo Segers" <jo.segers@.alro.be> wrote in message
news:e4fS8hu1EHA.2572@.tk2msftngp13.phx.gbl...[vbcol=seagreen]
>
> Chris Wood wrote:
backup[vbcol=seagreen]
backup
> Hi,
> It is not a good idea to remove the previous backup before you know the
> new backup is ok. If the new backup fails you don't have a spare one.
> Jo.

how to delete text file

hi...
how to delete text files from c drive (ex: c:\sale\300407.txt) using ms sql express edition 2005 ?
plz help me...

enable xp_cmdshell from start-- programs -- SQL Server 2005-- SQL Sserver surface Area Configuration tool -- Surface area confiugration for feature --

enable XP_Cmdshell

then run

exec xp_cmdshell 'del c:\yourfilename.txt'

Madhu

|||Be aware that if you are using Windows Authentication, the User who is accessing th e procedure will need the appropiate rights to delete the file. If you are using SQL Server authentication, it has to be the SQL Server Service account, if you don′t won′t either one, you will have to create a proxy account for the XP_CMDSHELL.

Jens K. Suessmeyer.

http://www.sqlserver2005.de
|||i'm using windows authentication. the command given still error. here the error :

c:\010407_S13103.txt
Access is denied

how about delete multiple text file from folder (exTongue Tiedale) in drive c using ms sql express edition 2005. is using the same command?|||

its a permission problem. this user do not have permission in the folder. Jens's post already mention how to handle it. Basically, what xp_cmdshell used for is to run a shell command from SQL Platform. Delete multiple files use

xp_cmdshell del *.txt

Madhu

|||the user is admin. there should be no permission problem. but i still get the same error after i run the command above for multiple files. please be remind that i'm using ms sql express edition 2005.|||Are you using WIndows authentication for connecting to the database ?

|||yes i'm using windows authentication|||Then you either have a proxy account configured which is not able to access the file, or the user you are currently using for the connection is not allowed, or the usage of the directory (if you using Vista) is required elevation.

|||i try this command in ms sql 2005 express edition but still get the same error. here the command :

declare @.del varchar(1000)
select @.del = 'del c:\Transfer\*.TXT'
exec xp_cmdshell @.del

here the error:
c:\Transfer\010407SALE.131.TXT
Access is denied

i'm using windows authentication and using admin account. so there should be no permission problem. i already enable xp_cmdshell in SQL Server Surface Area Configuration. why this error still occur? plz help me. i'm using ms windows xp.|||Is the folder encrypted ?

Jens K. Suessmeyer.

http://www.sqlserver2005.de

|||no