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 : 08:39:28
|
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...AmoskiAmoski |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-05-23 : 03:04:58
|
If you instead of EXEC (@SQL) do a PRINT @SQL what is the result?Can you spot the error then? If not, post the printed result here.I can imagine some thing gonw wrong.1) Table name or column name included a space2) Columns are surrounded with single quote instead of bracket E 12°55'05.25"N 56°04'39.16" |
 |
|
amoski
Starting Member
12 Posts |
Posted - 2008-05-23 : 03:16:45
|
quote: Originally posted by Peso If you instead of EXEC (@SQL) do a PRINT @SQL what is the result?Can you spot the error then? If not, post the printed result here.I can imagine some thing gonw wrong.1) Table name or column name included a space2) Columns are surrounded with single quote instead of bracket E 12°55'05.25"N 56°04'39.16"
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 ascAmoski |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-05-23 : 03:22:29
|
Isn't it obvious where the errors are?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_xorder by capmonth, phlo_name_x asc The line prodcuing this error is thisSELECT @sql='', @sumfunc=stuff(@sumfunc, len(@sumfunc), 1, ' END)' ) E 12°55'05.25"N 56°04'39.16" |
 |
|
amoski
Starting Member
12 Posts |
Posted - 2008-05-23 : 03:25:48
|
quote: Originally posted by Peso Isn't it obvious where the errors are?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_xorder by capmonth, phlo_name_x asc The line prodcuing this error is thisSELECT @sql='', @sumfunc=stuff(@sumfunc, len(@sumfunc), 1, ' END)' ) E 12°55'05.25"N 56°04'39.16"
Hi Peso,I tried all possibilities to fix this line but i fail, please if you can help me in fixing it....!!Amoski |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-05-23 : 03:44:05
|
SET @sql = '' E 12°55'05.25"N 56°04'39.16" |
 |
|
amoski
Starting Member
12 Posts |
Posted - 2008-05-23 : 03:57:50
|
quote: Originally posted by Peso SET @sql = '' E 12°55'05.25"N 56°04'39.16"
Hi Peso,where should i put SET @sql = ''??Amoski |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-23 : 04:57:38
|
quote: Originally posted by amoski
quote: Originally posted by Peso SET @sql = '' E 12°55'05.25"N 56°04'39.16"
Hi Peso,where should i put SET @sql = ''??Amoski
I think this line is causing it. Can you tell why you've included this?SELECT @sql='', @sumfunc=stuff(@sumfunc, len(@sumfunc), 1, ' END)' ) |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-05-23 : 05:05:16
|
05/23/2008 : 03:22:29Replace all this code SELECT @sql='', @sumfunc=stuff(@sumfunc, len(@sumfunc), 1, ' END)' )with this onlySET @sql='' E 12°55'05.25"N 56°04'39.16" |
 |
|
amoski
Starting Member
12 Posts |
Posted - 2008-05-23 : 05:19:49
|
quote: Originally posted by Peso 05/23/2008 : 03:22:29Replace all this code SELECT @sql='', @sumfunc=stuff(@sumfunc, len(@sumfunc), 1, ' END)' )with this onlySET @sql='' E 12°55'05.25"N 56°04'39.16"
It runs fine but it doesnt give correct results:These are the results:200706 HOME LOANS OFFICE,BLOEMFONTEIN 2280 2280 2280 2280200706 HOME LOANS OFFICE,CAPE TOWN 13113 13113 13113 13113200706 HOME LOANS OFFICE,DURBAN 9141 9141 9141 9141200706 HOME LOANS OFFICE,JOHANNESBURG 23612 23612 23612 23612200706 HOME LOANS OFFICE,P/ELIZABETH 3794 3794 3794 3794and i was expecting these results:200706 HOME LOANS OFFICE,BLOEMFONTEIN 603 88 1216 373200706 HOME LOANS OFFICE,CAPE TOWN 3668 541 7186 1718200706 HOME LOANS OFFICE,DURBAN 2484 374 5005 1278200706 HOME LOANS OFFICE,JOHANNESBURG 5969 853 13720 3070200706 HOME LOANS OFFICE,P/ELIZABETH 1033 189 2068 504if you can see it adds all numbers and put one number thru out...Amoski |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-05-23 : 05:27:13
|
Post you calling code to the sp and we might be able to debug it. E 12°55'05.25"N 56°04'39.16" |
 |
|
amoski
Starting Member
12 Posts |
Posted - 2008-05-23 : 05:33:55
|
quote: Originally posted by Peso Post you calling code to the sp and we might be able to debug it. E 12°55'05.25"N 56°04'39.16"
This is the code im running: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' the sample of my data is:capmonth aplcn_n phlo_name_x aplcn_stat_c200706 2482085 HOME LOANS OFFICE,CAPE TOWN RG200707 2526490 HOME LOANS OFFICE,PRETORIA NW200708 2582581 HOME LOANS OFFICE,DURBAN NW200708 2576633 HOME LOANS OFFICE,CAPE TOWN NW200706 2505348 HOME LOANS OFFICE,JOHANNESBURG NW200706 2473731 HOME LOANS OFFICE,DURBAN RG200706 2485644 HOME LOANS OFFICE,DURBAN GT200706 2510016 HOME LOANS OFFICE,DURBAN NW200707 2542767 HOME LOANS OFFICE,DURBAN NW200709 2598192 HOME LOANS OFFICE,PRETORIA NW200709 2624519 HOME LOANS OFFICE,JOHANNESBURG NW200707 2544214 HOME LOANS OFFICE,JOHANNESBURG GT200707 2512039 HOME LOANS OFFICE,DURBAN NW200706 2490290 HOME LOANS OFFICE,CAPE TOWN NT200707 2530797 HOME LOANS OFFICE,PRETORIA RGAmoski |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-05-23 : 05:49:08
|
This part is never executed either!SELECT @sql=@sql + '''' + convert(varchar(100), pivot) + ''' = ' + stuff(@sumfunc,charindex( '(', @sumfunc )+1, 0, ' CASE ' + @pivot + ' WHEN ' + @delim + convert(varchar(100), pivot) + @delim + ' THEN ' ) + ', ' FROM ##pivotThe CASE ... WHEN ... is never output to statement! E 12°55'05.25"N 56°04'39.16" |
 |
|
amoski
Starting Member
12 Posts |
Posted - 2008-05-23 : 05:51:37
|
quote: Originally posted by Peso This part is never executed either!SELECT @sql=@sql + '''' + convert(varchar(100), pivot) + ''' = ' + stuff(@sumfunc,charindex( '(', @sumfunc )+1, 0, ' CASE ' + @pivot + ' WHEN ' + @delim + convert(varchar(100), pivot) + @delim + ' THEN ' ) + ', ' FROM ##pivotThe CASE ... WHEN ... is never output to statement! E 12°55'05.25"N 56°04'39.16"
Please assist my friend...im really desparate..!!Amoski |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
amoski
Starting Member
12 Posts |
Posted - 2008-05-23 : 06:33:07
|
quote: Originally posted by Peso Use a tested and timeproven CROSSTAB like thishttp://weblogs.sqlteam.com/jeffs/articles/5120.aspx E 12°55'05.25"N 56°04'39.16"
Hi Peso,I hope this one will work my friend can you tip on how put my excecute statment .i.e how do i call sp...!!Amoski |
 |
|
|
|
|
|
|