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.
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) ASDECLARE @sql varchar(8000), @delim varchar(1)SET NOCOUNT ONSET ANSI_WARNINGS OFFEXEC ('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 ##pivotDROP TABLE ##pivotSELECT @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 1Incorrect 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. |
 |
|
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_RGGROUP BY capmonth,phlo_name_x order by capmonth,phlo_name_x ascIt puts END in every count...Amoski |
 |
|
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) ASDECLARE @sql varchar(8000), @delim varchar(1)SET NOCOUNT ONSET ANSI_WARNINGS OFFEXEC ('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 ##pivotDROP TABLE ##pivotSELECT @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 1Incorrect 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 AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
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_RGGROUP BY capmonth,phlo_name_x order by capmonth,phlo_name_x ascIt puts END in every count...Amoski
thats the probelm. The problem seems to be in thisstatementSELECT @sql='', @sumfunc=stuff(@sumfunc, len(@sumfunc), 1, ' END)' ) Can you post how you will be calling this sp? |
 |
|
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 AthalyeHi Athalye,Pls help with correcting the Case statement i tried all posibilities my friend and i failed...!!Amoski |
 |
|
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 AthalyeHi 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? |
 |
|
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 AthalyeHi 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_RGGROUP 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 |
 |
|
|
|
|
|
|