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
 Transact-SQL (2000)
 Need Help with Cross Tab Query....

Author  Topic 

amoski
Starting Member

12 Posts

Posted - 2008-05-22 : 04:11:46
Hi Guys pls assist...

im using the script below:
CREATE PROCEDURE crosstab
@select varchar(8000),
@sumfunc varchar(100),
@pivot varchar(100),
@table varchar(100)
AS

DECLARE @sql varchar(8000), @delim varchar(1)
SET NOCOUNT ON
SET ANSI_WARNINGS OFF

EXEC ('SELECT ' + @pivot + ' AS pivot INTO ##pivot FROM ' + @table + ' WHERE 1=2')
EXEC ('INSERT INTO ##pivot SELECT DISTINCT ' + @pivot + ' FROM ' + @table + ' WHERE '
+ @pivot + ' Is Not Null')

SELECT @sql='', @sumfunc=stuff(@sumfunc, len(@sumfunc), 1, ' END)' )

SELECT @delim=CASE Sign( CharIndex('char', data_type)+CharIndex('date', data_type) )
WHEN 0 THEN '' ELSE '''' END
FROM tempdb.information_schema.columns
WHERE table_name='##pivot' AND column_name='pivot'

SELECT @sql=@sql + '''' + convert(varchar(100), pivot) + ''' = ' +
stuff(@sumfunc,charindex( '(', @sumfunc )+1, 0, ' CASE ' + @pivot + ' WHEN '
+ @delim + convert(varchar(100), pivot) + @delim + ' THEN ' ) + ', ' FROM ##pivot

DROP TABLE ##pivot

SELECT @sql=left(@sql, len(@sql)-1)
SELECT @select=stuff(@select, charindex(' FROM ', @select)+1, 0, ', ' + @sql + ' ')

EXEC (@select)
SET ANSI_WARNINGS ON

--------------------------------
And I am getting the following ERROR:

Server: Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'END'.

Pls may someone assist...

Amoski

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-22 : 04:52:48
Replace EXEC(@select) with PRINT(@select) and post the string its printing out.
Go to Top of Page

amoski
Starting Member

12 Posts

Posted - 2008-05-22 : 05:03:35
[quote]Originally posted by visakh16

Replace EXEC(@select) with PRINT(@select) and post the string its printing out.

Hi,

This is the result after replacing Replace EXEC(@select) with PRINT(@select)

SELECT capmonth,phlo_name_x , 'GT' = count(aplcn_n END), 'NT' = count(aplcn_n END), 'NW' = count(aplcn_n END), 'RG' = count(aplcn_n END) FROM ors_hmln.a066353.APPS_NW_GT_NT_RG
GROUP BY capmonth,phlo_name_x order by capmonth,phlo_name_x asc

It puts END in every count...



Amoski
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-05-22 : 05:05:55
quote:
Originally posted by amoski

Hi Guys pls assist...

im using the script below:
CREATE PROCEDURE crosstab
@select varchar(8000),
@sumfunc varchar(100),
@pivot varchar(100),
@table varchar(100)
AS

DECLARE @sql varchar(8000), @delim varchar(1)
SET NOCOUNT ON
SET ANSI_WARNINGS OFF

EXEC ('SELECT ' + @pivot + ' AS pivot INTO ##pivot FROM ' + @table + ' WHERE 1=2')
EXEC ('INSERT INTO ##pivot SELECT DISTINCT ' + @pivot + ' FROM ' + @table + ' WHERE '
+ @pivot + ' Is Not Null')

SELECT @sql='', @sumfunc=stuff(@sumfunc, len(@sumfunc), 1, ' END)' )

SELECT @delim=CASE Sign( CharIndex('char', data_type)+CharIndex('date', data_type) )
WHEN 0 THEN '' ELSE '''' END
FROM tempdb.information_schema.columns
WHERE table_name='##pivot' AND column_name='pivot'

SELECT @sql=@sql + '''' + convert(varchar(100), pivot) + ''' = ' +
stuff(@sumfunc,charindex( '(', @sumfunc )+1, 0, ' CASE ' + @pivot + ' WHEN '
+ @delim + convert(varchar(100), pivot) + @delim + ' THEN
' ) + ', ' FROM ##pivot

DROP TABLE ##pivot

SELECT @sql=left(@sql, len(@sql)-1)
SELECT @select=stuff(@select, charindex(' FROM ', @select)+1, 0, ', ' + @sql + ' ')

EXEC (@select)
SET ANSI_WARNINGS ON

--------------------------------
And I am getting the following ERROR:

Server: Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'END'.

Pls may someone assist...

Amoski



I believe problem is with no proper ending to CASE statement I highlighted above.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-22 : 05:08:53
quote:
Originally posted by amoski

[quote]Originally posted by visakh16

Replace EXEC(@select) with PRINT(@select) and post the string its printing out.

Hi,

This is the result after replacing Replace EXEC(@select) with PRINT(@select)

SELECT capmonth,phlo_name_x , 'GT' = count(aplcn_n END), 'NT' = count(aplcn_n END), 'NW' = count(aplcn_n END), 'RG' = count(aplcn_n END) FROM ors_hmln.a066353.APPS_NW_GT_NT_RG
GROUP BY capmonth,phlo_name_x order by capmonth,phlo_name_x asc

It puts END in every count...



Amoski


thats the probelm. The problem seems to be in this
statement

SELECT @sql='', @sumfunc=stuff(@sumfunc, len(@sumfunc), 1, ' END)' )
Can you post how you will be calling this sp?
Go to Top of Page

amoski
Starting Member

12 Posts

Posted - 2008-05-22 : 05:09:53


I believe problem is with no proper ending to CASE statement I highlighted above.

Harsh Athalye

Hi Athalye,

Pls help with correcting the Case statement i tried all posibilities my friend and i failed...!!

Amoski
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-22 : 05:55:41
quote:
Originally posted by amoski



I believe problem is with no proper ending to CASE statement I highlighted above.

Harsh Athalye

Hi Athalye,

Pls help with correcting the Case statement i tried all posibilities my friend and i failed...!!

Amoski


can you post how you will be calling the sp?
Go to Top of Page

amoski
Starting Member

12 Posts

Posted - 2008-05-22 : 06:03:52
quote:
Originally posted by visakh16

quote:
Originally posted by amoski



I believe problem is with no proper ending to CASE statement I highlighted above.

Harsh Athalye

Hi Athalye,

Pls help with correcting the Case statement i tried all posibilities my friend and i failed...!!

Amoski


can you post how you will be calling the sp?



This is how i Execute my cross tab procedure:

EXECUTE crosstab 'SELECT capmonth,phlo_name_x FROM ors_hmln.a066353.APPS_NW_GT_NT_RG
GROUP BY capmonth,phlo_name_x order by capmonth,phlo_name_x asc',
'count(aplcn_n)','aplcn_stat_c','ors_hmln.a066353.APPS_NW_GT_NT_RG'


Amoski
Go to Top of Page
   

- Advertisement -