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 2008 Forums
 Transact-SQL (2008)
 Use Stored Procedure to create table from dynamic

Author  Topic 

scamquist
Starting Member

2 Posts

Posted - 2009-07-08 : 11:20:04
I have a table (WeekTaskStatus) with three column: Task, WeekEnd and TaskStatus.
There are currently three tasks (Scope, Budget and Resources) that are updated weekly with the values Green, Yellow and Red.

Task WeekEnd TaskStatus
--------------------------------------------
Scope 1/1/2009 Red
Scope 1/8/2009 Green
Scope 1/15/2009 Green
Budget 1/1/2009 Yellow
Budget 1/8/2009 Red
Budget 1/15/2009 Green
Resources 1/1/2009 Green
Resources 1/8/2009 Red
Resources 1/15/2009 Yellow



The Stored Procedure uses dynamic SQL below creates a pivot table with the Task as the column headers, the WeekEnd as the rows and the TaskStatus as the data.

Scope Budget Resources
--------------------------------------------
1/1/2009 Red Yellow Green
1/8/2009 Green Red Red
1/15/2009 Green Green Yellow
.
.
.


I need to use dynamic SQL because additional tasks may be created and would need to show the additional columns as needed. For example, ModificationReview may be added 1/22/2009.

Is it possible to add a statement in the Stored Procedure to create a table (not temp table), and overwrite when the data changes?



ALTER PROCEDURE dbo.TaskStatusPivot
as

Declare @strSQL varchar(max)
DECLARE @cols varchar(2000)
SELECT @cols = STUFF(( SELECT DISTINCT TOP 100 PERCENT
'],[' + task
FROM WeekTaskStatus
ORDER BY '],[' + task
FOR XML PATH('')
), 1, 2, '') + ']'


set @strSQL = 'select WeekEnd, ' + @cols +
' from (
select WeekEnd, Task, TaskStatus from WeekTaskStatus) o
pivot (Max(TaskStatus) for Task in (' + @cols + ')) p'

exec(@strSQL)

Any help will be appreciated.

Steve

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-07-08 : 12:54:21
quote:
Originally posted by scamquist

I have a table (WeekTaskStatus) with three column: Task, WeekEnd and TaskStatus.
There are currently three tasks (Scope, Budget and Resources) that are updated weekly with the values Green, Yellow and Red.

Task WeekEnd TaskStatus
--------------------------------------------
Scope 1/1/2009 Red
Scope 1/8/2009 Green
Scope 1/15/2009 Green
Budget 1/1/2009 Yellow
Budget 1/8/2009 Red
Budget 1/15/2009 Green
Resources 1/1/2009 Green
Resources 1/8/2009 Red
Resources 1/15/2009 Yellow



The Stored Procedure uses dynamic SQL below creates a pivot table with the Task as the column headers, the WeekEnd as the rows and the TaskStatus as the data.

Scope Budget Resources
--------------------------------------------
1/1/2009 Red Yellow Green
1/8/2009 Green Red Red
1/15/2009 Green Green Yellow
.
.
.


I need to use dynamic SQL because additional tasks may be created and would need to show the additional columns as needed. For example, ModificationReview may be added 1/22/2009.

Is it possible to add a statement in the Stored Procedure to create a table (not temp table), and overwrite when the data changes?



ALTER PROCEDURE dbo.TaskStatusPivot
as

drop table yourdestinationtable
Declare @strSQL varchar(max)
DECLARE @cols varchar(2000)
SELECT @cols = STUFF(( SELECT DISTINCT TOP 100 PERCENT
'],[' + task
FROM WeekTaskStatus
ORDER BY '],[' + task
FOR XML PATH('')
), 1, 2, '') + ']'


set @strSQL = 'select WeekEnd, ' + @cols +
' into yourdestinationtable from (
select WeekEnd, Task, TaskStatus from WeekTaskStatus) o
pivot (Max(TaskStatus) for Task in (' + @cols + ')) p'

exec(@strSQL)

Any help will be appreciated.

Steve


yup you can just add a into table name on dynamic sql string
Go to Top of Page

scamquist
Starting Member

2 Posts

Posted - 2009-07-08 : 16:23:14
It worked. Thank you for the assist

Steve
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-07-09 : 10:50:44
welcome
Go to Top of Page
   

- Advertisement -