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
 General SQL Server Forums
 New to SQL Server Programming
 drop a column

Author  Topic 

junior6202
Starting Member

45 Posts

Posted - 2015-04-06 : 22:36:16
I have a question, Every month I get a request from cutomer service to go into a SQL VIEW and DELETE a column to reflect the past Month. Can anybody advice on how to write a query to create a SQL job to performed this task automatically. For example today I deleted the column [2015/03] which represents the month of March. The columns left are [2015/04] representing the current month, [2015/05], [2015/06],[2015/07]....[2016/08].Next month I will have to delete column [2015/04]and the current month will be [2015/05]....etc.

So I'm thinking I will have to Drop the column and also Maybe using a date funtion to look at the current date and format it to reflect [YYYY/MM] and IF the Column Month is less than current date month then drop the column. Any Ideas will be appreciated.

Thanks in advance.

Junior

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2015-04-07 : 02:50:41
You can't delete columns like that from a view. You will have to open the view with ALTER and then make the necessary changes.


Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2015-04-07 : 08:14:58
YOu could use Dynamic SQL instead of a VIEW ... no idea if that would fit into your application or other usage of this view though.

You could generate the VIEW each month with some sort of application code / scheduled task.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2015-04-10 : 08:11:53
Looks like you need to use Dynamic PIVOT http://beyondrelational.com/modules/2/blogs/70/posts/10840/dynamic-pivot-in-sql-server-2005.aspx

Madhivanan

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

- Advertisement -