Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 SSIS and Import/Export (2005)
 Delete Rows from a Table

Author  Topic 

loudbliss
Starting Member

37 Posts

Posted - 2008-06-20 : 12:16:42
Hi,

This is an easy one.

How do delete rows from a table?

I want to delete all the rows from the last month of a table so i have to be able to pass the month parameter to the delete statement.

Help.. plz.. i think this is an easy one

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-20 : 13:00:27
DELETE FROM YourTable WHERE MONTH(DateColumn)=@Month-1

Where @Month is the month parameter
Go to Top of Page

loudbliss
Starting Member

37 Posts

Posted - 2008-06-20 : 15:13:06
Visak, thanks but where do i put this in SSIS? Script Component, Ole DB Destination..?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-06-20 : 15:15:44
Execute SQL task

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-20 : 15:15:52
quote:
Originally posted by loudbliss

Visak, thanks but where do i put this in SSIS? Script Component, Ole DB Destination..?


In SSIS Create a OLEDB Command Task and put this statement. Create a variable in package to hold the value of month and use ? as place holder for parameter for query statement. Then map the parameter value to the created variable.
Go to Top of Page

loudbliss
Starting Member

37 Posts

Posted - 2008-06-20 : 16:33:32
tkizer, i could do the work with the Execute SQL Task, very simple, only had to write the statement:
"delete from mytable where month(datecolumn) <> month(getdate())"

Visakh im trying it now with the ? as a place holder but i really dont know how to make this work.

I've had trouble writing statements with parameters so i've been forced to use scripts to send custom parametres to a query.

Could you please explain how do I use the ? part and how do i send a non fixed value to a variable,that it, lets say i want to delete everything from last year but only the actual month, i will need so somehow calculate the value of last year and the value of this month and then send it to a variable.

I know it's kind of a noobie thing but for some of us it would be very helpful.

Thax



Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-06-20 : 19:23:34
quote:
Originally posted by visakh16

DELETE FROM YourTable WHERE MONTH(DateColumn)=@Month-1

Where @Month is the month parameter


You should also use year value otherwise all years data for that month would be deleted

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-21 : 01:54:27
quote:
Originally posted by loudbliss

tkizer, i could do the work with the Execute SQL Task, very simple, only had to write the statement:
"delete from mytable where month(datecolumn) <> month(getdate())"

Visakh im trying it now with the ? as a place holder but i really dont know how to make this work.

I've had trouble writing statements with parameters so i've been forced to use scripts to send custom parametres to a query.

Could you please explain how do I use the ? part and how do i send a non fixed value to a variable,that it, lets say i want to delete everything from last year but only the actual month, i will need so somehow calculate the value of last year and the value of this month and then send it to a variable.

I know it's kind of a noobie thing but for some of us it would be very helpful.

Thax







http://technet.microsoft.com/en-us/library/ms141138.aspx

http://www.developerdotstar.com/community/node/432
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-21 : 01:55:42
quote:
Originally posted by madhivanan

quote:
Originally posted by visakh16

DELETE FROM YourTable WHERE MONTH(DateColumn)=@Month-1

Where @Month is the month parameter


You should also use year value otherwise all years data for that month would be deleted

Madhivanan

Failing to plan is Planning to fail


Sure Madhi. I did overlook this condition. It should have been

DELETE FROM YourTable WHERE MONTH(DateColumn)=@Month-1 AND YEAR(DateColumn)=YEAR(GETDATE())


thanks for spotting this out
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-06-21 : 14:02:42
Another method though complicated but accurate

DELETE FROM YourTable
WHERE DateColumn>=DATEADD(month,@month-2,dateadd(year,datediff(year,0,getdate()),0))and
DagteColumn<DATEADD(month,@month-1,dateadd(year,datediff(year,0,getdate()),0))


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-21 : 14:08:16
quote:
Originally posted by madhivanan

Another method though complicated but accurate

DELETE FROM YourTable
WHERE DateColumn>=DATEADD(month,@month-2,dateadd(year,datediff(year,0,getdate()),0))and
DagteColumn<DATEADD(month,@month-1,dateadd(year,datediff(year,0,getdate()),0))


Madhivanan

Failing to plan is Planning to fail


looks good too
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-06-21 : 22:23:50
quote:
Originally posted by madhivanan

Another method though complicated but accurate

DELETE FROM YourTable
WHERE DateColumn>=DATEADD(month,@month-2,dateadd(year,datediff(year,0,getdate()),0))and
DagteColumn<DATEADD(month,@month-1,dateadd(year,datediff(year,0,getdate()),0))


Madhivanan

Failing to plan is Planning to fail


Accurate and more efficient.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

loudbliss
Starting Member

37 Posts

Posted - 2008-06-23 : 10:12:15
Guys,

Thanks for the help with the delete statements but what i'm more interested in knowing is the setting of parameters (how to set them and how to add them in a query) in SSIS.

Deleting a previus month can be done with those queries you guys sent w/o the need of parameters but for example i need to make an statement in ssis where i can delete all the data from the previous week (that is the last 7 days) For that i need to calculate what was is that day, determine to what month and year it belongs. Therefore i need to do that in the script component i think.

That's where i really need help. How to set a calculated value to a variable and how to put that in a statement.

Visakh mentioned this
"Create a variable in package to hold the value of month and use ? as place holder for parameter for query statement. Then map the parameter value to the created variable." but i really didnt how to do this specially the use of the ?

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-23 : 10:29:36
quote:
Originally posted by loudbliss

Guys,

Thanks for the help with the delete statements but what i'm more interested in knowing is the setting of parameters (how to set them and how to add them in a query) in SSIS.

Deleting a previus month can be done with those queries you guys sent w/o the need of parameters but for example i need to make an statement in ssis where i can delete all the data from the previous week (that is the last 7 days) For that i need to calculate what was is that day, determine to what month and year it belongs. Therefore i need to do that in the script component i think.

That's where i really need help. How to set a calculated value to a variable and how to put that in a statement.

Visakh mentioned this
"Create a variable in package to hold the value of month and use ? as place holder for parameter for query statement. Then map the parameter value to the created variable." but i really didnt how to do this specially the use of the ?




Did you check the link i posted earlier? Also refer this too

http://www.learnintegrationservices.com/VideoTutorials/SSIS-Video/1338/Part6-Variables-and-Execute-SQL-Task.aspx
Go to Top of Page

loudbliss
Starting Member

37 Posts

Posted - 2008-06-23 : 16:38:10
visakh i missed your links before.

http://technet.microsoft.com/en-us/library/ms141138.aspx
aswered my question.

Really appreciate your help
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-24 : 00:07:09
quote:
Originally posted by loudbliss

visakh i missed your links before.

http://technet.microsoft.com/en-us/library/ms141138.aspx
aswered my question.

Really appreciate your help



You're welcome. Glad that i could help you.
Go to Top of Page

loudbliss
Starting Member

37 Posts

Posted - 2008-06-25 : 11:37:48
Just wanted to share the same solution but in a video
http://midnightdba.itbookworm.com/SSISExecSQLwParams/SSISExecSQLwParams.html
Go to Top of Page

khask99
Starting Member

1 Post

Posted - 2009-06-22 : 11:16:02
loudbliss....Thanks a bunch for the video!!

K
Go to Top of Page
   

- Advertisement -