ql 서버 일괄 가져오기 Excel 테이블 데이터

3559 단어 sqlserver
첨부: Openrowset OpenDataSoure 및 xpcmdshell에서 연 코드:SPCONFIGURE ‘show advanced options’,1 GO RECONFIGURE GO SP_CONFIGURE ‘Ad Hoc Distributed Queries’,1 GO RECONFIGURE GO SP_CONFIGURE ‘xp_cmdshell’,1 GO RECONFIGURE GO SP_CONFIGURE ‘show advanced options’,0 GO RECONFIGURE GO
USE [testdemo]
GO
/****** Object:  StoredProcedure [dbo].[Sp_InputExceltoSQL]    Script Date: 12/19/2016 09:53:39 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROC [dbo].[Sp_InputExceltoSQL]
@dir NVARCHAR(100),--EXCEL      :
@tabname NVARCHAR(50) --            
AS
SET NOCOUNT ON
CREATE TABLE #t([filename] NVARCHAR(1000))
IF RIGHT(@dir,1)<>'\' 
    SET @dir=@dir+'\'

DECLARE @cmd NVARCHAR(1000)    
SET @cmd = N'dir "' + @dir + '*.xls*" /B'
INSERT #t EXEC master..xp_cmdshell @cmd
DELETE #t WHERE [filename] IS NULL
-- SQL    :
DECLARE @S nvarchar(MAX)
IF OBJECT_ID(@tabname) IS NULL
BEGIN
    SELECT TOP 1 @S='SELECT TOP 0 * INTO '+ @tabname+' FROM OPENROWSET(
            ''MICROSOFT.ACE.OLEDB.12.0'', ''EXCEL 12.0;HDR=YES;IMEX=2;DATABASE='+@DIR+[filename]+'''
            ,''SELECT * FROM [Sheet1$]'')'
    FROM #t  
    EXEC(@S)
END
--          
SET @S = ''
SELECT @S = @S + 'INSERT '+@tabname+' SELECT * FROM OPENROWSET(
    ''MICROSOFT.ACE.OLEDB.12.0'', ''EXCEL 12.0;HDR=YES;IMEX=2;DATABASE='+@DIR+FILENAME+'''
    ,''SELECT * FROM [Sheet1$]'')'  
FROM #t      
EXEC(@S)
SET NOCOUNT OFF

호출 방식(경로, 데이터베이스 테이블 이름)execdbo.Sp_InputExceltoSQL ‘E:\shiseidodatabase’,’OrderDetail’

좋은 웹페이지 즐겨찾기