|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hi
I'm getting myself in a tiz and was wondering if someone could help me. I need to write a crosstab report in SQL Server (stored procedure) that gets its column headings from a table. I've played around with case statements but i just cant get my head round this! Basically i have 2 tables. Table1: a list of tasks (one of each) eg graphic design, deployment etc. Table2: a permissions table, has the usersid and task that they can access. (one row per permission - numerous userid entries) Report: Have the task names taken from table 1 as the column headings. have a different row for each user and have a 1 if permission exists and a 0 if it doesnt. I cant hard code the tasks as they have to be configurable to the sysadmin user of the system i'm developing. This has got me running in circles...any help would be greatly appreciated! Thanks |
|
#2
|
|||
|
|||
|
--Table1(ID_Task int, strTask Varchar(100))
--Table2(ID_User int, ID_Task int) If Exists(Select * From tempdb.dbo.sysobjects Where name = '##Return') BEGIN DROP TABLE ##Return END Declare @tmpTable Table(Row varchar(100), Col varchar(100), Value int) Insert @tmpTable Select T2.ID_User, T1.strTask, 1 From Table2 T2, Table1 T1 Where T2.ID_Task = T1.ID_Task Declare @Field varchar(100), @Return nvarchar(4000), @Cols nvarchar(4000), @NameCols nvarchar(4000) Set @Return = '' Set @Cols = '' Set @NameCols = '' DECLARE Cols_Cursor CURSOR FOR Select strTask From Table1 Order By strTask OPEN Cols_Cursor FETCH NEXT FROM Cols_Cursor INTO @Field WHILE @@FETCH_STATUS = 0 BEGIN Set @Cols = @Cols + Case When @Cols = '' Then '' Else ',' End + '[' + @Field + '] Int' Set @NameCols = @NameCols + Case When @NameCols = '' Then '' Else ',' End + '[' + @Field + ']' FETCH NEXT FROM Cols_Cursor INTO @Field END CLOSE Cols_Cursor DEALLOCATE Cols_Cursor Set @Return = N'create Table ##Return ([Row] varchar(100), ' + @Cols + ')' Exec sp_executesql @Return Insert Into ##Return ([Row]) Select ID_User From Table2 Group By ID_User Order By ID_User Declare @Row Varchar(100), @Col Varchar(100), @Value float, @Updated nvarchar(4000) DECLARE Update_Cursor_1 CURSOR FOR Select Row, Col, Value From @tmpTable Order By Row, Col OPEN Update_Cursor_1 FETCH NEXT FROM Update_Cursor_1 INTO @Row, @Col, @Value WHILE @@FETCH_STATUS = 0 BEGIN Set @Updated = '' Set @Updated = N'Update ##Return Set [' + @Col + '] = ' + convert(nvarchar, @Value) + ' Where [Row] = ''' + @Row + '''' Exec sp_executesql @Updated FETCH NEXT FROM Update_Cursor_1 INTO @Row, @Col, @Value END CLOSE Update_Cursor_1 DEALLOCATE Update_Cursor_1 DECLARE Update_Cursor_0 CURSOR FOR Select strTask From Table1 Order By strTask OPEN Update_Cursor_0 FETCH NEXT FROM Update_Cursor_0 INTO @Field WHILE @@FETCH_STATUS = 0 BEGIN Set @Updated = '' Set @Updated = N'Update ##Return Set [' + @Field + '] = 0 ' + ' Where [' + @Field + '] Is Null ' Exec sp_executesql @Updated FETCH NEXT FROM Update_Cursor_0 INTO @Field END CLOSE Update_Cursor_0 DEALLOCATE Update_Cursor_0 Select * From ##Return |
|
#3
|
|||
|
|||
|
Sql Server Crosstab
I am a newbie to this and I have got the code partially work for what I am trying to do. However, I would like to see the actual value of my row. How do I change that? If someone could walk me through the logic of the code I would be very grateful!!!!!!! |
![]() |
| Viewing: Dev Articles Community Forums > Databases > Microsoft SQL Server > SQL Server Crosstab |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|