|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
一个语句分成两个event(实际上不止,其他可以忽略),一个table_mapevent和一个Rows_log_event。Table_mapevent是一样的,主要看Rows_log_event。excel|导出数据
CreateExcelXLSfromT-SQL
---------------------------------------------------------------------------
--CreateXLSscriptDAL-04/24/2003
--
--DesignedforAgentscheduling,turnon"Appendoutputforstephistory"
--
--Searchfor%%%tofindadjustableconstantsandotheroptions
--
--UsesOLEforADOandOLEDBtocreatetheXLSfileifitdoesnotexist
--LinkedserverrequirestheXLStoexistbeforecreation
--UsesOLEADOtoCreatetheXLSWorksheetforuseasatablebyT-SQL
--UsesLinkedServertoallowT-SQLaccesstoXLStable
--UsesT-SQLtopopulateteXLSworksheet,veryfast
--
PRINTBeginCreateXLSscriptat+RTRIM(CONVERT(varchar(24),GETDATE(),121))+
PRINT
GO
SETNOCOUNTON
DECLARE@Connint--ADOConnectionobjecttocreateXLS
,@hrint--OLEreturnvalue
,@srcvarchar(255)--OLEErrorSource
,@descvarchar(255)--OLEErrorDescription
,@Pathvarchar(255)--DriveorUNCpathforXLS
,@Connectvarchar(255)--OLEDBConnectionstringforJet4ExcelISAM
,@WKS_Createdbit--WhethertheXLSWorksheetexists
,@WKS_Namevarchar(128)--NameoftheXLSWorksheet(table)
,@ServerNamenvarchar(128)--LinkedServernameforXLS
,@DDLvarchar(8000)--Jet4DDLfortheXLSWKStablecreation
,@SQLvarchar(8000)--INSERTINTOXLST-SQL
,@Recsint--NumberofrecordsaddedtoXLS
,@Logbit--Whethertologprocessdetail
--Initvariables
SELECT@Recs=0
--%%%1=Verboseoutputdetail,helpsfindproblems,0=minimaloutputdetail
,@Log=1
--%%%assigntheUNCorpathandnamefortheXLSfile,requiresRead/Writeaccess
--mustbeaccessablefromserverviaSQLServerserviceaccount
--&SQLServerAgentserviceaccount,ifscheduled
SET@Path=C:TEMPTest_+CONVERT(varchar(10),GETDATE(),112)+.xls
--assigntheADOconnectionstringfortheXLScreation
SET@Connect=Provider=Microsoft.Jet.OLEDB.4.0;DataSource=+@Path+;ExtendedProperties=Excel8.0
--%%%assigntheLinkedServernamefortheXLSpopulation
SET@ServerName=EXCEL_TEST
--%%%RenameTableasrequired,thiswillalsobetheXLSWorksheetname
SET@WKS_Name=People
--%%%TablecreationDDL,usesJet4syntax,
--Textdatatype=varchar(255)whenaccessedfromT-SQL
SET@DDL=CREATETABLE+@WKS_Name+(SSNText,NameText,PhoneText)
--%%%T-SQLfortablepopulation,notethe4partnamingrequiredbyJet4OLEDB
--INSERTINTOSELECT,INSERTINTOVALUES,andEXECsptypesaresupported
--LinkedServerdoesnotsupportSELECTINTOtypes
SET@SQL=INSERTINTO+@ServerName+...+@WKS_Name+(SSN,Name,Phone)
SET@SQL=@SQL+SELECTau_idASSSN
SET@SQL=@SQL+,LTRIM(RTRIM(ISNULL(au_fname,)++ISNULL(au_lname,)))ASName
SET@SQL=@SQL+,phoneASPhone
SET@SQL=@SQL+FROMpubs.dbo.authors
IF@Log=1PRINTCreatedOLEADODB.Connectionobject
--CreatetheConnobject
EXEC@hr=sp_OACreateADODB.Connection,@ConnOUT
IF@hr0--havetouseasOLE/ADOcanreturnnegativeerrornumbers
BEGIN
--ReturnOLEerror
EXECsp_OAGetErrorInfo@Conn,@srcOUT,@descOUT
SELECTError=convert(varbinary(4),@hr),Source=@src,Description=@desc
RETURN
END
IF@Log=1PRINTchar(9)+AssignedConnectionStringproperty
--SetatheConnobjectsConnectionStringproperty
--Work-aroundforerrorusingavariableparameterontheOpenmethod
EXEC@hr=sp_OASetProperty@Conn,ConnectionString,@Connect
IF@hr0
BEGIN
--ReturnOLEerror
EXECsp_OAGetErrorInfo@Conn,@srcOUT,@descOUT
SELECTError=convert(varbinary(4),@hr),Source=@src,Description=@desc
RETURN
END
IF@Log=1PRINTchar(9)+OpenConnectiontoXLS,forfileCreateorAppend
--CalltheOpenmethodtocreatetheXLSifitdoesnotexist,cantuseparameters
EXEC@hr=sp_OAMethod@Conn,Open
IF@hr0
BEGIN
--ReturnOLEerror
EXECsp_OAGetErrorInfo@Conn,@srcOUT,@descOUT
SELECTError=convert(varbinary(4),@hr),Source=@src,Description=@desc
RETURN
END
--%%%ThissectioncouldberepeatedformultipleWorksheets(Tables)
IF@Log=1PRINTchar(9)+ExecuteDDLtocreate+@WKS_Name+worksheet
--CalltheExecutemethodtoCreatetheworksheetwiththe@WKS_Namecaption,
--whichisalsousedasaTablereferenceinT-SQL
--NeatwaytodefinecolumndatatypesinExcelworksheet
--Sometimesconvertingtotextistheonlywork-aroundforExcelsGeneral
--Cellformatting,eventhoughtheCellcontainsText,Exceltriestoformat
--itina"Smart"way,Ihaveevenhadtousethesinglequoteappendedasthe
--1stcharacterinT-SQLtoforceExceltoleaveitalone
EXEC@hr=sp_OAMethod@Conn,Execute,NULL,@DDL,NULL,129--adCmdText+adExecuteNoRecords
--0x80040E14fortableexistsinADO
IF@hr=0x80040E14
--kludge,skip0x80042732forADOOptionalparameters(NULL)inSQL7
OR@hr=0x80042732
BEGIN
--TraptheseOLEErrors
IF@hr=0x80040E14
BEGIN
PRINTchar(9)++@WKS_Name+Worksheetexistsforappend
SET@WKS_Created=0
END
SET@hr=0--ignoretheseerrors
END
IF@hr0
BEGIN
--ReturnOLEerror
EXECsp_OAGetErrorInfo@Conn,@srcOUT,@descOUT
SELECTError=convert(varbinary(4),@hr),Source=@src,Description=@desc
RETURN
END
IF@Log=1PRINTDestroyedOLEADODB.Connectionobject
--DestroytheConnobject,+++importanttonotleakmemory+++
EXEC@hr=sp_OADestroy@Conn
IF@hr0
BEGIN
--ReturnOLEerror
EXECsp_OAGetErrorInfo@Conn,@srcOUT,@descOUT
SELECTError=convert(varbinary(4),@hr),Source=@src,Description=@desc
RETURN
END
--LinkedServerallowsT-SQLtoaccesstheXLSworksheet(Table)
--ThismustbeperformedaftertheADOstuffastheXLSmustexist
--andcontaintheschemaforthetable,orworksheet
IFNOTEXISTS(SELECTsrvnamefrommaster.dbo.sysserverswheresrvname=@ServerName)
BEGIN
IF@Log=1PRINTCreatedLinkedServer+@ServerName+andLogin
EXECsp_addlinkedserver@server=@ServerName
,@srvproduct=MicrosoftExcelWorkbook
,@provider=Microsoft.Jet.OLEDB.4.0
,@datasrc=@Path
,@provstr=Excel8.0
--nologinnameorpasswordarerequiredtoconnecttotheJet4ISAMlinkedserver
EXECsp_addlinkedsrvlogin@ServerName,false
END
--HavetoEXECtheSQL,otherwisetheSQLisevaluated
--forthelinkedserverbeforeitexists
EXEC(@SQL)
PRINTchar(9)+Populated+@WKS_Name+tablewith+CONVERT(varchar,@@ROWCOUNT)+Rows
--%%%OptionalyoumayleavetheLinkedServerforotherXLSoperations
--RememberthattheLinkedServerwillnotcreatetheXLS,soremoveit
--Whenyouaredonewithit,especiallyifyoudeleteormovethefile
IFEXISTS(SELECTsrvnamefrommaster.dbo.sysserverswheresrvname=@ServerName)
BEGIN
IF@Log=1PRINTDeletedLinkedServer+@ServerName+andLogin
EXECsp_dropserver@ServerName,droplogins
END
GO
SETNOCOUNTOFF
PRINT
PRINTFinishedCreateXLSscriptat+RTRIM(CONVERT(varchar(24),GETDATE(),121))+
GO
目标
SET@Path=C:TEMPTest_+CONVERT(varchar(10),GETDATE(),112)+.xls
源
SET@SQL=INSERTINTO+@ServerName+...+@WKS_Name+(SSN,Name,Phone)
SET@SQL=@SQL+SELECTau_idASSSN
SET@SQL=@SQL+,LTRIM(RTRIM(ISNULL(au_fname,)++ISNULL(au_lname,)))ASName
SET@SQL=@SQL+,phoneASPhone
SET@SQL=@SQL+FROMpubs.dbo.authors
对于insert和delete,event中包含了插入/删除的记录的所有字段的值(太爽了。。) |
|