Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 1st, 2003, 08:32 AM
Jacks Jacks is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 12 Jacks User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unhappy SQL Server Crosstab

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

Reply With Quote
  #2  
Old June 4th, 2004, 01:36 PM
Eloy Cantu Eloy Cantu is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 1 Eloy Cantu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Smile Cross Tab SQL Server

--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

Reply With Quote
  #3  
Old August 8th, 2007, 05:47 PM
PURDURANGO PURDURANGO is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2007
Posts: 1 PURDURANGO User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 43 sec
Reputation Power: 0
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!!!!!!!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMicrosoft SQL Server > SQL Server Crosstab


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT