--exec sp_getcrossdata
set quoted_identifier on
goset ansi_nulls on
go--exec sp_getcrossdata
--alter proc sp_getcrossdata as
--begin
declare @sqlstring nvarchar(4000)
declare @temp_cybh varchar(50)
declare @temp_gzyd varchar(511)
select 使用者名稱,姓名 into #tmp1 from 使用者表
select 詞語編號,工作分類+'|'+工作要點 as 工作要點 into #tmp2 from 詞語檢視
select 使用者名稱,詞語編號,sum(工作時) as 工作時 into #t*** from 日誌明細表 where 專案編號='0000000' group by 使用者名稱,詞語編號 order by 詞語編號
set @sqlstring = n'select 姓名,'
declare cursor_xkb cursor for
select distinct a.詞語編號,工作要點 from #t*** as a,#tmp2 as b
where a.詞語編號=b.詞語編號
open cursor_xkb
fetch next from cursor_xkb into @temp_cybh,@temp_gzyd
while @@fetch_status=0
begin
set @sqlstring=@sqlstring+n'isnull(sum(case b.詞語編號 when '''
+cast(@temp_cybh as varchar)+n''' then 工作時 end),0)
as "'+@temp_gzyd+'",'
fetch next from cursor_xkb into @temp_cybh,@temp_gzyd
endclose cursor_xkb
deallocate cursor_xkb
set @sqlstring = left(@sqlstring,len(@sqlstring)-1)
set @sqlstring = @sqlstring +n'from #tmp1 as a,#t*** as b
where a.使用者名稱=b.使用者名稱 group by 姓名'
print @sqlstring
exec sp_executesql @sqlstring
drop table #tmp1,#tmp2,#t***
--end
goset quoted_identifier off
goset ansi_nulls on
go
縱橫表轉換學習
動態橫表轉縱表 建表語句 if not object id class1 is null drop table class1 gocreate table class1 student nvarchar 2 數學 int,物理 int,英語 int,語文 int insert class1 sele...
交叉表例項
建表 在查詢分析器裡執行 create table test id int identity 1,1 not null name nvarchar 50 collate chinese prc ci as null subject nvarchar 50 collate chinese prc ci...
交叉表例項
create table test id int identity 1,1 names nvarchar 50 subject nvarchar 50 source decimal 18,0 insert into test names subject source values n 張三 n 語文...