行转列有关问题中如要转的列非常多 用declare中定义的 变量最多存储8000个字符,但是小弟我的数据非常多 定义变量装不上 怎么处理 还有其他办法吗

行转列问题中如要转的列非常多 用declare中定义的 变量最多存储8000个字符,但是我的数据非常多 定义变量装不下 怎么办 还有其他办法吗?
行转列问题
表1如下  
  A            B
200901   F23,F34,F56,T67
200902   F23,T34,T67,T89
200903   TP25,TP45,F45,F56
表2如下
  A     F23 F34 F56 T67 T89 TP25 TP45 F45   
200901   1   1   1   1   0   0    0    0
200902   1   1   0   1   1   0    0    0
200903   0   0   1   0   0   1    1    1   
意思把表1变为表2. 表1解释如下:字段A里的数据是读者的借书号,B是读者所借的书籍的索引号。
现在要变为表2 意思是:把所借的书籍的索引号改为列名,读者借了哪本书 其列数值为1 否则为0  
就是布尔数值类型 不知道明白了没有?
有懂的加QQ 443714055 谢谢

且我已经用如下代码完成了部分行转列的问题
declare @sql varchar(8000)
set @sql=' select code '
select @sql=@sql+' ,max(case when indexcode='''+indexcode+''' then 1 else 0 end) as ['+indexcode+']' 
from (select distinct indexcode from t1) as aaa
set @sql=@sql+' from t1 group by code'
print @sql
exec (@sql)

这个语句可以完成一些 但是 我要转的列如果是有 1000个 行是1000的话 那么总大小就是 1000000  而前面定义的变量@sql 没存储那么多 在执行时 只有0行受 影响  谁能还有其他语句可以解决问题 非常感谢  如有问题不懂的 可以加 443714055 详问……谢谢
 
------解决方案--------------------

create table t1
(
  code varchar(10),
  indexcode varchar(10)
)
insert into t1
select '200901', 'F23' union all
select '200901', 'F34' union all
select '200901', 'F56' union all
select '200901', 'T67' union all
select '200902', 'F23' union all
select '200902', 'F34' union all
select '200902', 'T67' union all
select '200902', 'T89' union all
select '200903', 'F56' union all
select '200903', 'F45' union all
select '200903', 'TP25' union all
select '200903', 'TP45'
go

select distinct indexcode,rid=identity(int,1,1)
    into #tb
from t1

declare @sql varchar(8000)  --第一个拼接字符串
declare @str varchar(8000)  --第二个拼接字符串
set @sql=' select code '
select @sql=@sql+' ,max(case when indexcode='''+indexcode+''' then 1 else 0 end) as ['+indexcode+']'  
from #tb
where rid between 1 and 4
select @str = isnull(@str,'')+' ,max(case when indexcode='''+indexcode+''' then 1 else 0 end) as ['+indexcode+']'
from #tb
where rid between 5 and 8
exec (@sql+@str+' from t1 group by code')

drop table t1,#tb

/***************

code       F23         F34         F45         F56         T67         T89         TP25        TP45