| Author |
Topic |
|
ferrethouse
Constraint Violating Yak Guru
352 Posts |
Posted - 2009-11-24 : 18:59:07
|
My stored procedure is failing because I'm doing an exec on @SQL variable which is a varchar(8000) but the actual SQL is getting chopped off because it is longer than 8000 characters. It complains when I try to use a TEXT data type instead. Here is the code...SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER procedure [dbo].[dynamic_pivot](@select nvarchar(2000),@PivotCol nvarchar(100),@Summaries nvarchar(100)) asset nocount on;SET ANSI_WARNINGS OFF;declare @pivot varchar(8000)declare @sql varchar(8000)select @select =replace(@select,'select ','select '+@PivotCol+' as pivot_col,') create table #pivot_columns (pivot_column nvarchar(1000))Select @sql='select distinct pivot_col from ('+@select+') as t'insert into #pivot_columnsexec(@sql)select @pivot=coalesce(@pivot+',','')+'['+pivot_column+']'from #pivot_columnsselect @sql=' select * from ( '+@select+' ) as tpivot ( '+@Summaries+' for pivot_col in ('+@pivot+') ) as p'exec(@sql) |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
ferrethouse
Constraint Violating Yak Guru
352 Posts |
Posted - 2009-11-25 : 11:17:53
|
quote: Originally posted by madhivanan Why did you remove varchar(max)?See, I used it but you made it varchar(8000) but kept rest of the code as suchhttp://sqlblogcasts.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspxMadhivananFailing to plan is Planning to fail
I reverted it back as you suggest but now I get...String or binary data would be truncated.I think the problem is specifically on this line...select @pivot=coalesce(@pivot+',','')+'['+pivot_column+']'from #pivot_columnsAs #pivot_columns is very large for this particular client. It works fine for clients with fewer items. |
 |
|
|
ferrethouse
Constraint Violating Yak Guru
352 Posts |
Posted - 2009-11-25 : 11:31:51
|
| No, the "will be truncated" error was caused by the temp table definition being too small...create table #pivot_columns (pivot_column varchar(100))Some of the column names are in excess of 100 characters. So I changed it to varchar(1000) and now I'm back to the original error... |
 |
|
|
ferrethouse
Constraint Violating Yak Guru
352 Posts |
Posted - 2009-11-25 : 11:38:29
|
So this is what I'm running now.../****** Object: StoredProcedure [dbo].[dynamic_pivot] Script Date: 11/24/2009 14:06:40 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOalter procedure dynamic_pivot(@select varchar(2000),@PivotCol varchar(100),@Summaries varchar(100)) asset nocount on;SET ANSI_WARNINGS OFF;declare @pivot varchar(max), @sql nvarchar(max)select @select =replace(@select,'select ','select '+@PivotCol+' as pivot_col,') create table #pivot_columns (pivot_column varchar(1000))Select @sql='select distinct pivot_col from ('+@select+') as t'insert into #pivot_columnsexec(@sql)select @pivot=coalesce(@pivot+',','')+'['+pivot_column+']'from #pivot_columnsselect @sql=' select * from ( '+@select+' ) as t pivot ( '+@Summaries+' for pivot_col in ('+@pivot+') ) as p'--exec(@sql)EXECUTE sp_executesql @sqlI printed out this line...select @pivot=coalesce(@pivot+',','')+'['+pivot_column+']'from #pivot_columnsAnd it is getting truncated which I believe is causing the error. |
 |
|
|
ferrethouse
Constraint Violating Yak Guru
352 Posts |
Posted - 2009-11-25 : 11:53:12
|
| I got it to work by only selecting the first 20 characters of the pivot column...select @select =replace(@select,'select ','select substring('+@PivotCol+',1,20) + ''...'' as pivot_col,')That is obviously only a temporary solution because the number of items will continue to grow so if anyone has any other suggestions I would appreciate it. |
 |
|
|
mfemenel
Professor Frink
1421 Posts |
Posted - 2009-11-25 : 12:10:44
|
| because I live in this world and your post frightens me...please reconsider using a dynamic sql statemet that is over 8000 characters. Consider the poor bastard who has to change this years from now when you're not around. Scary.Mike"oh, that monkey is going to pay" |
 |
|
|
ferrethouse
Constraint Violating Yak Guru
352 Posts |
Posted - 2009-11-25 : 12:33:43
|
quote: Originally posted by mfemenel because I live in this world and your post frightens me...please reconsider using a dynamic sql statemet that is over 8000 characters. Consider the poor bastard who has to change this years from now when you're not around. Scary.Mike"oh, that monkey is going to pay"
If you bothered to actually look at the code it is a dynamically built sql statement that would be easy to maintain. The stored proc was not written by me and is widely used by people doing dynamic pivots.If you know of a better way to do dynamic pivots I would love to hear it! |
 |
|
|
mfemenel
Professor Frink
1421 Posts |
Posted - 2009-11-25 : 12:43:45
|
| wow, way to overreactMike"oh, that monkey is going to pay" |
 |
|
|
ferrethouse
Constraint Violating Yak Guru
352 Posts |
Posted - 2009-11-25 : 13:29:46
|
quote: Originally posted by mfemenel wow, way to overreactMike"oh, that monkey is going to pay"
Thanks for your contribution to this thread. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-11-26 : 01:18:59
|
quote: Originally posted by ferrethouse I got it to work by only selecting the first 20 characters of the pivot column...select @select =replace(@select,'select ','select substring('+@PivotCol+',1,20) + ''...'' as pivot_col,')That is obviously only a temporary solution because the number of items will continue to grow so if anyone has any other suggestions I would appreciate it.
Can you show parameter values passed to the procedure?MadhivananFailing to plan is Planning to fail |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-11-26 : 01:27:36
|
quote: I printed out this line...select @pivot=coalesce(@pivot+',','')+'['+pivot_column+']'from #pivot_columnsAnd it is getting truncated which I believe is causing the error.
That may not be true. You should see the actual length byprint len(@pivot)MadhivananFailing to plan is Planning to fail |
 |
|
|
|