declare @dirPath varchar(200)='C:\Users\Administrator\Desktop\待處理數據\順江學校4\' --------------------------------------------------------------------------------------------獲取本地文件夾下多個文件---------------------------------------------------------- if(OBJECT_ID('ff')>0) drop table ff create table ff ( id int identity(1,1), fName varchar(300), depth int, isFile int ) insert into ff exec xp_dirtree @dirPath, 0, 1 --select * from ff if(OBJECT_ID('RawScore')>0) drop table RawScore CREATE TABLE [dbo].[RawScore]( [F] [varchar](50) NULL, [F0] [varchar](50) NULL, [F1] [nvarchar](255) NULL, [F2] [nvarchar](255) NULL, [F3] [float] NULL, [F4] [float] NULL, [F5] [float] NULL, [F6] [nvarchar](255) NULL, [F7] [float] NULL, [F8] [float] NULL, [F9] [float] NULL ) declare @studentId varchar(50)='',@studentName varchar(50)='',@studentInfo varchar(100)=''; declare @pos int=0,@len int =0; declare @fileName varchar(100); declare @sql varchar(max) = ''; ------------------------------------------------------------------遊標操做----------------------------------------------------------------------------- declare cur cursor for select fName from ff open cur fetch next from cur into @fileName while @@FETCH_STATUS=0 begin set @studentInfo=SUBSTRING(@fileName,1,patindex('%.xls',@fileName)-1) set @pos = PATINDEX('%[_]%',@studentInfo); set @len = LEN(@studentInfo); set @studentName = SUBSTRING(@studentInfo,1,@pos-1); set @studentId = SUBSTRING(@studentInfo,@pos+1,@len); --select @studentName,@studentId --------------------------------------------------------------------------------------導入本地Excel文件數據--------------------------------------------------------------------------- set @sql = 'insert into RawScore select '''+@studentId+''','''+@studentName+''',* from OPENROWSET(''Microsoft.Jet.OLEDB.4.0'',''Excel 8.0;Database='+@dirPath+@fileName+''', [Sheet1$])'; exec(@sql) fetch next from cur into @fileName end close cur deallocate cur if(OBJECT_ID('StudentScore')>0) drop table StudentScore CREATE TABLE [dbo].[StudentScore]( [Id] [uniqueidentifier] NOT NULL, [StudentID] [varchar](50) NULL, [Name] [varchar](50) NULL, [Kind] [int] NULL, [ItemName] [varchar](50) NULL, [Score] [float] NULL ) insert into studentScore select NEWID(), f,F0,1,F2,f5 from RawScore where f2!='內容板塊(滿分)' and PATINDEX('%[(]%',F2)>0 union all select NEWID(), f,F0,2,F6,f9 from RawScore where f6!='能力層次(滿分)' and PATINDEX('%[(]%',F6)>0 select * from StudentScore order by name -------------------------------------------------------跨服務器連接數據庫進行數據操做------------------------------------------------- --declare @count int=0 --select @count=COUNT(*) from sys.servers where name='syncDBLink' --if(@count > 0) --begin -- exec sp_dropserver 'syncDBLink','droplogins' --end ----打開指定服務器上的數據庫 --exec sp_addlinkedserver 'syncDBLink','','SQLOLEDB','192.168.0.102','','','wangyue0428'; --exec sp_addlinkedsrvlogin 'syncDBLink',false,null,'sa','HX1q2w3e4r'; --exec sp_serveroption 'syncDBLink','rpc out','true'; --delete from syncDBLink.wangyue0428.dbo.StudentScore --insert into syncDBLink.wangyue0428.dbo.StudentScore --select * from StudentScore --exec sp_dropserver 'syncDBLink','droplogins' --select StudentID,Name,Kind,SUM(Score) from StudentScore --group by StudentID,Name,Kind --select distinct studentId,name from StudentScore -----------------------------------------------------------------------------------行轉列實現------------------------------------------------------------------- ;with cte1 as ( select StudentID,Name, [拼音練習(8.00)],[字詞練習(16.00)],[句子練習(21.00)],[課內文段閱讀(12.00)],[課外文段閱讀(18.00)],[習做(25.00)] ,[拼音練習(8.00)]+[字詞練習(16.00)]+[句子練習(21.00)]+[課內文段閱讀(12.00)]+[課外文段閱讀(18.00)]+[習做(25.00)] as 小計 from ( select StudentID,Name,ItemName,Score from syncDBLink.wangyue0428.dbo.StudentScore where Kind=1 ) as s pivot ( sum(Score) for ItemName in([拼音練習(8.00)],[字詞練習(16.00)],[句子練習(21.00)],[課內文段閱讀(12.00)],[課外文段閱讀(18.00)],[習做(25.00)]) ) as pv ) ,cte2 as ( select StudentID,Name, [識記(18.00)],[表達應用(51.00)],[理解(16.00)],[分析綜合(15.00)] ,[識記(18.00)]+[表達應用(51.00)]+[理解(16.00)]+[分析綜合(15.00)] as 小計 from ( select StudentID,Name,ItemName,Score from syncDBLink.wangyue0428.dbo.StudentScore where Kind=2 ) as s pivot ( sum(Score) for ItemName in([識記(18.00)],[表達應用(51.00)],[理解(16.00)],[分析綜合(15.00)]) ) as pv ) select ROW_NUMBER() over(order by cte1.小計 desc) as 序號, '順江中學' as 學校名稱,cte1.Name as 姓名,'' as 性別,[拼音練習(8.00)],[字詞練習(16.00)],[句子練習(21.00)],[課內文段閱讀(12.00)],[課外文段閱讀(18.00)],[習做(25.00)],cte1.小計 ,[識記(18.00)],[表達應用(51.00)],[理解(16.00)],[分析綜合(15.00)],cte2.小計 from cte1 inner join cte2 on cte1.StudentID=cte2.StudentID