SQL Server SQL语句导入导出大全
2022-11-12 09:51:22
内容摘要
这篇文章主要为大家详细介绍了SQL Server SQL语句导入导出大全,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记!导出到excelEXECmaster..xp
文章正文
这篇文章主要为大家详细介绍了SQL Server SQL语句导入导出大全,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记!
导出到excelEXECmaster..xp_cmdshell'bcpSettleDB.dbo.shanghuoutc:\temp1.xls-c-q-S"GNETDATA/GNETDATA"-U"sa"-P""'导入ExcelSELECT*FROMOpenDataSource('Microsoft.Jet.OLEDB.4.0','DataSource="c:\test.xls";UserID=Admin;Password=;Extendedproperties=Excel5.0')...xactions动态文件名declare@fnvarchar(20),@svarchar(1000)set@fn='c:\test.xls'set@s='''Microsoft.Jet.OLEDB.4.0'',''DataSource="'+@fn+'";UserID=Admin;Password=;Extendedproperties=Excel5.0'''set@s='SELECT*FROMOpenDataSource('+@s+')...sheet1$'exec(@s)SELECTcast(cast(科目编号asnumeric(10,2))asnvarchar(255))+' '转换后的别名FROMOpenDataSource('Microsoft.Jet.OLEDB.4.0','DataSource="c:\test.xls";UserID=Admin;Password=;Extendedproperties=Excel5.0')...xactionsEXCEL导到远程SQL insertOPENDATASOURCE('SQLOLEDB','DataSource=远程ip;UserID=sa;Password=密码').库名.dbo.表名(列名1,列名2)SELECT列名1,列名2FROMOpenDataSource('Microsoft.Jet.OLEDB.4.0','DataSource="c:\test.xls";UserID=Admin;Password=;Extendedproperties=Excel5.0')...xactions导入文本文件EXECmaster..xp_cmdshell'bcpdbname..tablenameinc:\DT.txt-c-Sservername-Usa-Ppassword'导出文本文件EXECmaster..xp_cmdshell'bcpdbname..tablenameoutc:\DT.txt-c-Sservername-Usa-Ppassword'或EXECmaster..xp_cmdshell'bcp"Select*fromdbname..tablename"queryoutc:\DT.txt-c-Sservername-Usa-Ppassword'导出到TXT文本,用逗号分开execmaster..xp_cmdshell'bcp"库名..表名"out"d:\tt.txt"-c-t,-Usa-Ppassword'BULKINSERT库名..表名FROM'c:\test.txt'WITH(FIELDTERMINATOR=';',ROWTERMINATOR='\n')--/*dBaseIV文件select*fromOPENROWSET('MICROSOFT.JET.OLEDB.4.0','dBaseIV;HDR=NO;IMEX=2;DATABASE=C:\','select*from[客户资料4.dbf]')--*/--/*dBaseIII文件select*fromOPENROWSET('MICROSOFT.JET.OLEDB.4.0','dBaseIII;HDR=NO;IMEX=2;DATABASE=C:\','select*from[客户资料3.dbf]')--*/--/*FoxPro数据库select*fromopenrowset('MSDASQL','Driver=MicrosoftVisualFoxProDriver;SourceType=DBF;SourceDB=c:\','select*from[aa.DBF]')--*/导入DBF文件select*fromopenrowset('MSDASQL','Driver=MicrosoftVisualFoxProDriver;SourceDB=e:\VFP98\data;SourceType=DBF','select*fromcustomerwherecountry!="USA"orderbycountry')go导出到DBF如果要导出数据到已经生成结构(即现存的)FOXPRO表中,可以直接用下面的SQL语句insertintoopenrowset('MSDASQL','Driver=MicrosoftVisualFoxProDriver;SourceType=DBF;SourceDB=c:\','select*from[aa.DBF]')select*from表说明:SourceDB=c:\指定foxpro表所在的文件夹aa.DBF指定foxpro表的文件名.导出到Accessinsertintoopenrowset('Microsoft.Jet.OLEDB.4.0','x:\A.mdb';'admin';'',A表)select*from数据库名..B表导入AccessinsertintoB表selet*fromopenrowset('Microsoft.Jet.OLEDB.4.0','x:\A.mdb';'admin';'',A表)文件名为参数 declare@fnamevarchar(20)set@fname='d:\test.mdb'exec('SELECTa.*FROMopendatasource(''Microsoft.Jet.OLEDB.4.0'','''+@fname+''';''admin'';'''',topics)asa')SELECT*FROMOpenDataSource('Microsoft.Jet.OLEDB.4.0','DataSource="f:\northwind.mdb";JetOLEDB:DatabasePassword=123;UserID=Admin;Password=;')导入xml 文件[Page]DECLARE@idocintDECLARE@docvarchar(1000)--sampleXMLdocumentSET@doc='<root><Customercid="C1"name="Janine"city="Issaquah"><Orderoid="O1"date="1/20/1996"amount="3.5"/><Orderoid="O2"date="4/30/1997"amount="13.4">Customerwasverysatisfied</Order></Customer><Customercid="C2"name="Ursula"city="Oelde"><Orderoid="O3"date="7/14/1999"amount="100"note="Wrapitbluewhitered"><Urgency>Important</Urgency>HappyCustomer.</Order><Orderoid="O4"date="1/20/1996"amount="10000"/></Customer></root>'--CreateaninternalrepresentationoftheXMLdocument.EXECsp_xml_preparedocument@idocOUTPUT,@doc--ExecuteaSELECTstatementusingOPENXMLrowsetprovider.SELECT*FROMOPENXML(@idoc,'/root/Customer/Order',1)WITH(oidchar(5),amountfloat,commentntext'text()')EXECsp_xml_removedocument@idocExcel导到Txt 想用 select*intoopendatasource(...)fromopendatasource(...)实现将一个Excel文件内容导入到一个文本文件假设Excel中有两列,第一列为姓名,第二列为很行帐号(16位)且银行帐号导出到文本文件后分两部分,前8位和后8位分开。如果要用你上面的语句插入的话,文本文件必须存在,而且有一行:姓名,银行账号1,银行账号2,然后就可以用下面的语句进行插入,注意文件名和目录根据你的实际情况进行修改.insertintoopendatasource('MICROSOFT.JET.OLEDB.4.0','Text;HDR=Yes;DATABASE=C:\')...[aa#txt]--,aa#txt)--*/select姓名,银行账号1=left(银行账号,8),银行账号2=right(银行账号,8)fromopendatasource('MICROSOFT.JET.OLEDB.4.0','Excel5.0;HDR=YES;IMEX=2;DATABASE=c:\a.xls'--,Sheet1$))...[Sheet1$]如果你想直接插入并生成文本文件,就要用bcpdeclare@sqlvarchar(8000),@tbnamevarchar(50)--首先将excel表内容导入到一个全局临时表select@tbname='[##temp'+cast(newid()asvarchar(40))+']',@sql='select姓名,银行账号1=left(银行账号,8),银行账号2=right(银行账号,8)into'+@tbname+'fromopendatasource(''MICROSOFT.JET.OLEDB.4.0'',''Excel5.0;HDR=YES;IMEX=2;DATABASE=c:\a.xls'')...[Sheet1$]'exec(@sql)--然后用bcp从全局临时表导出到文本文件set@sql='bcp"'+@tbname+'"out"c:\aa.txt"/S"(local)"/P""/c'execmaster..xp_cmdshell@sql--删除临时表exec('droptable'+@tbname)导整个数据库用bcp实现的存储过程/*实现数据导入/导出的存储过程根据不同的参数,可以实现导入/导出整个数据库/单个表调用示例:--导出调用示例----导出单个表execfile2table'zj','','','xzkh_sa..地区资料','c:\zj.txt',1----导出整个数据库execfile2table'zj','','','xzkh_sa','C:\docman',1--导入调用示例----导入单个表execfile2table'zj','','','xzkh_sa..地区资料','c:\zj.txt',0----导入整个数据库execfile2table'zj','','','xzkh_sa','C:\docman',0*/ifexists(select1fromsysobjectswherename='File2Table'andobjectproperty(id,'IsProcedure')=1)dropprocedureFile2TablegocreateprocedureFile2Table@servernamevarchar(200)--服务器名,@usernamevarchar(200)--用户名,如果用NT验证方式,则为空'',@passwordvarchar(200)--密码,@tbnamevarchar(500)--数据库.dbo.表名,如果不指定:.dbo.表名,则导出数据库的所有用户表,@filenamevarchar(1000)--导入/导出路径/文件名,如果@tbname参数指明是导出整个数据库,则这个参数是文件存放路径,文件名自动用表名.txt,@isoutbit--1为导出,0为导入asdeclare@sqlvarchar(8000)if@tbnamelike'%.%.%'--如果指定了表名,则直接导出单个表beginset@sql='bcp'+@tbname+casewhen@isout=1then'out'else'in'end+'"'+@filename+'"/w'+'/S'+@servername+casewhenisnull(@username,'')=''then''else'/U'+@usernameend+'/P'+isnull(@password,'')execmaster..xp_cmdshell@sqlendelsebegin--导出整个数据库,定义游标,取出所有的用户表declare@m_tbnamevarchar(250)ifright(@filename,1)<>'\'set@filename=@filename+'\'set@m_tbname='declare#tbcursorforselectnamefrom'+@tbname+'..sysobjectswherextype=''U'''exec(@m_tbname)open#tbfetchnextfrom#tbinto@m_tbnamewhile@@fetch_status=0beginset@sql='bcp'+@tbname+'..'+@m_tbname+casewhen@isout=1then'out'else'in'end+'"'+@filename+@m_tbname+'.txt"/w'+'/S'+@servername+casewhenisnull(@username,'')=''then''else'/U'+@usernameend+'/P'+isnull(@password,'')execmaster..xp_cmdshell@sqlfetchnextfrom#tbinto@m_tbnameendclose#tbdeallocate#tbendgoOracle EXECsp_addlinkedserver'OracleSvr','Oracle7.3','MSDAORA','ORCLDB'GOdeletefromopenquery(mailser,'select*fromyulin')select*fromopenquery(mailser,'select*fromyulin')updateopenquery(mailser,'select*fromyulinwhereid=15')setdisorder=555,catago=888insertintoopenquery(mailser,'selectdisorder,catagofromyulin')values(333,777)补充:对于用bcp导出,是没有字段名的.用openrowset导出,需要事先建好表.用openrowset导入,除ACCESS及EXCEL外,均不支持非本机数据导入注:关于SQL Server SQL语句导入导出大全的内容就先介绍到这里,更多相关文章的可以留意
代码注释