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 2005 Forums
 Transact-SQL (2005)
 Can't use TEXT data type for local variable?

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 ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[dynamic_pivot]
(
@select nvarchar(2000),
@PivotCol nvarchar(100),
@Summaries nvarchar(100)
) as

set 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_columns
exec(@sql)

select @pivot=coalesce(@pivot+',','')+'['+pivot_column+']'from #pivot_columns


select @sql=
'
select * from
(
'+@select+'
) as t
pivot
(
'+@Summaries+' for pivot_col in ('+@pivot+')
) as p
'

exec(@sql)

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-11-24 : 19:06:11
You can use nvarchar(max) with sp_executesql or you could concatenate multiple varchar(8000)s together with EXEC @SQL.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-11-24 : 19:06:53
For you knowledge, read this about dynamic SQL: http://www.sommarskog.se/dynamic_sql.html

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-11-25 : 01:48:35
Why did you remove varchar(max)?
See, I used it but you made it varchar(8000) but kept rest of the code as such
http://sqlblogcasts.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx



Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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 such
http://sqlblogcasts.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx



Madhivanan

Failing 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_columns

As #pivot_columns is very large for this particular client. It works fine for clients with fewer items.
Go to Top of Page

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...

Go to Top of Page

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 ON
GO
SET QUOTED_IDENTIFIER ON
GO
alter procedure dynamic_pivot
(
@select varchar(2000),
@PivotCol varchar(100),
@Summaries varchar(100)
) as

set 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_columns
exec(@sql)

select @pivot=coalesce(@pivot+',','')+'['+pivot_column+']'from #pivot_columns

select @sql=
'
select * from
(
'+@select+'
) as t
pivot
(
'+@Summaries+' for pivot_col in ('+@pivot+')
) as p
'
--exec(@sql)
EXECUTE sp_executesql @sql



I printed out this line...

select @pivot=coalesce(@pivot+',','')+'['+pivot_column+']'from #pivot_columns

And it is getting truncated which I believe is causing the error.
Go to Top of Page

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.
Go to Top of Page

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"
Go to Top of Page

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!
Go to Top of Page

mfemenel
Professor Frink

1421 Posts

Posted - 2009-11-25 : 12:43:45
wow, way to overreact

Mike
"oh, that monkey is going to pay"
Go to Top of Page

ferrethouse
Constraint Violating Yak Guru

352 Posts

Posted - 2009-11-25 : 13:29:46
quote:
Originally posted by mfemenel

wow, way to overreact

Mike
"oh, that monkey is going to pay"



Thanks for your contribution to this thread.
Go to Top of Page

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?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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_columns

And it is getting truncated which I believe is causing the error.

That may not be true. You should see the actual length by

print len(@pivot)

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -