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-1Where @Month is the month parameter |
 |
|
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..? |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
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. |
 |
|
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 |
 |
|
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-1Where @Month is the month parameter
You should also use year value otherwise all years data for that month would be deletedMadhivananFailing to plan is Planning to fail |
 |
|
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.aspxhttp://www.developerdotstar.com/community/node/432 |
 |
|
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-1Where @Month is the month parameter
You should also use year value otherwise all years data for that month would be deletedMadhivananFailing to plan is Planning to fail
Sure Madhi. I did overlook this condition. It should have beenDELETE FROM YourTable WHERE MONTH(DateColumn)=@Month-1 AND YEAR(DateColumn)=YEAR(GETDATE()) thanks for spotting this out |
 |
|
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)) MadhivananFailing to plan is Planning to fail |
 |
|
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)) MadhivananFailing to plan is Planning to fail
looks good too |
 |
|
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)) MadhivananFailing to plan is Planning to fail
Accurate and more efficient. Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
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 ? |
 |
|
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 toohttp://www.learnintegrationservices.com/VideoTutorials/SSIS-Video/1338/Part6-Variables-and-Execute-SQL-Task.aspx |
 |
|
loudbliss
Starting Member
37 Posts |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
loudbliss
Starting Member
37 Posts |
Posted - 2008-06-25 : 11:37:48
|
Just wanted to share the same solution but in a videohttp://midnightdba.itbookworm.com/SSISExecSQLwParams/SSISExecSQLwParams.html |
 |
|
khask99
Starting Member
1 Post |
Posted - 2009-06-22 : 11:16:02
|
loudbliss....Thanks a bunch for the video!!K |
 |
|
|