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 2000 Forums
 SQL Server Development (2000)
 Get rid of Stored Procedure

Author  Topic 

Vassago
Starting Member

33 Posts

Posted - 2007-09-05 : 22:03:55
Hello all,

I have implimented and used the dynamic crosstab stored procedure located here:

http://weblogs.sqlteam.com/jeffs/archive/2005/05/02/4842.aspx

and it works flawlessly in Query Analyzer as a Temp Stored Procedure as I do not have permissions to create Stored Procedures. I now have a problem. I need to be able to run this stored procedure as a query without any stored procedure intervention. I have no idea how to go about doing this. Does anyone have any ideas that can help me? The query I usually run with the stored procedure is this:

exec #CrossTab 'SELECT buildexcludereason, Buildprojectcs FROM (select buildprojectcs, buildexcludereason from dialermasterrawfilehist where datadate = ''9/05/07'' and owner in (''p1'',''p2'') and buildexcludereason is not null) mytest',
'buildexcludereason',
'Count(buildexcludereason)[]',
'buildprojectcs',
'',
'buildprojectcs'

Thanks in advance,

Vassago

Kristen
Test

22859 Posts

Posted - 2007-09-06 : 04:18:22
Well, taking Jeff's code, you could just make that into dynamic SQL:

create procedure CrossTab (@Select varchar(1000),
@PivotCol varchar(100),
@Summaries varchar(100),
@GroupBy varchar(100),
@OtherCols varchar(100) = Null)
AS


set nocount on
set ansi_warnings off

DECLARE @Select varchar(1000),
@PivotCol varchar(100),
@Summaries varchar(100),
@GroupBy varchar(100),
@OtherCols varchar(100)
SELECT
@Select = 'SELECT buildexcludereason, Buildprojectcs FROM (select buildprojectcs, buildexcludereason
from dialermasterrawfilehist where datadate = ''9/05/07'' and owner in (''p1'',''p2'')
and buildexcludereason is not null) mytest',
@PivotCol = 'buildexcludereason',
@Summaries = 'Count(buildexcludereason)[]',
@GroupBy = 'buildprojectcs',
@OtherCols = ''

-- 'buildprojectcs' -- This one seems to be an extra parameter, so you'll need to sort this out!

declare @Vals varchar(8000);
set @Vals = '';

set @OtherCols= isNull(', ' + @OtherCols,'')

create table #temp (Pivot varchar(100))

insert into #temp
exec ('select distinct convert(varchar(100),' + @PivotCol + ') as Pivot FROM (' + @Select + ') A')

select @Vals = @Vals + ', ' +
replace(replace(@Summaries,'(','(CASE WHEN ' + @PivotCol + '=''' +
Pivot + ''' THEN '),')[', ' END) as [' + Pivot )
from #Temp
order by Pivot

drop table #Temp

exec ( 'select ' + @GroupBy + @OtherCols + @Vals +
' from (' + @Select + ') A GROUP BY ' + @GroupBy)

set nocount off
set ansi_warnings on

Kristen
Go to Top of Page

Vassago
Starting Member

33 Posts

Posted - 2007-09-06 : 17:18:50
Thanks for the suggestion. When I run the code, I get the following:

Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near ','.
Server: Msg 170, Level 15, State 1, Line 3
Line 3: Incorrect syntax near 'A'.

Do you know where the problem might lie?

Vassago

Also, is there a way of running this code without the use of temp tables or variables?
Go to Top of Page

Vassago
Starting Member

33 Posts

Posted - 2007-09-06 : 17:31:54
Nevermind on that, I got it working by removing the OtherCols variable, as I don't really need that. My problem now is I need to have this query working in Excel as a Database Query. That's the problem I am currently having. Do you have any suggestions?
Go to Top of Page

Jeff Moden
Aged Yak Warrior

652 Posts

Posted - 2007-09-06 : 20:17:20
Yes... forget doing the pivot in SQL... import the data into Excel and use the Excel pivot to warp the data.

--Jeff Moden
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-09-07 : 02:53:55
Might depend on how many rows there are in the query/ (I think Excel has a limit of 32,000 rows??)

Can't you set up a Database Query in Excel as raw SQL? In which case you can just paste in your working SQL. But it would be easier if you could create an Sproc in the database ...

Kristen
Go to Top of Page

Vassago
Starting Member

33 Posts

Posted - 2007-09-10 : 05:42:43
quote:
Yes... forget doing the pivot in SQL... import the data into Excel and use the Excel pivot to warp the data.

--Jeff Moden


Yes, that was what I was doing at first, but the problems I am having with that are that I require more rows than Excel will allow and when I try to do a straight external data pivot, it makes the file massive. I don't need all of the information stored in the spreadsheet, just the results from this.


quote:
Might depend on how many rows there are in the query/ (I think Excel has a limit of 32,000 rows??)

Can't you set up a Database Query in Excel as raw SQL? In which case you can just paste in your working SQL. But it would be easier if you could create an Sproc in the database


That's what I am wanting to do, but it is producing an error when creating the temp table and not running. If I can somehow make this work with subqueries instead of temp tables, I think it'll work, but all of my attempts so far have been in vain. If you think you can help or make any suggestions, I am open to them. Right now, I'm still studying the code to make sure I understand everything it does as I try and convert it.
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-09-10 : 09:08:46
Don't return ALL of the data back to Excel; summarize it as best you can, but don't pivot it. Then, let excel pivot the summarized results. That's my overall recommendaton for cross tabs (despite the fact that I wrote that stored procedure) -- return unpivoted summarized data to the client. focus on writing a valid, efficient SELECT that returns the results you need, test it and optimize it, and then worry about pivoting at the very last step.

That is, work on writing something like this:

SELECT buildexcludereason, Buildprojects, count(*) as Num
FROM dialermasterrawfilehist
where datadate = '9/05/07' and owner in ('p1','p2') and buildexcludereason is not null
GROUP BY buildexcludereason, Buildprojectcs

Test it, optimize it, make sure it returns data you need, check how many rows are returned overall. If it is less than 32,000, you are good to go and can pivot that easily in Excel.

Notice how much easier it is to write and troubleshoot this way as well.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

Vassago
Starting Member

33 Posts

Posted - 2007-09-10 : 15:23:52
Thanks for the reply Jeff.

I never thought about doing the pivot by counting the loans before I pull the data in. DOH! That just might work without causing the file size to become too large. I'll test it out and post my results, thanks for the suggestion.

BTW, your code above is fantastic.
Go to Top of Page

Vassago
Starting Member

33 Posts

Posted - 2007-09-10 : 19:31:48
Jeff, you are a gentleman and a scholar. Your idea works perfectly and keeps the size of the spreadsheet less than 50K. Thanks for your help. I will also be able to use your sql code in other projects.

Thanks also to Kristen for sharing my headache with the sql code, and to Jeff Moden for his input.

This place is wonderful.
Go to Top of Page
   

- Advertisement -