学生信息建一个表:学号,姓名,班级。。。。。<br>成绩建一个表:学号,考试日期,学科,成绩<br><br><br>SQL语句导入导出大全 <br><br>/******* 导出到excel<br>EXEC master..xp_cmdshell 'bcp SettleDB.dbo.shanghu out c:/temp1.xls -c -q -S"GNETDATA/GNETDATA" -U"sa" -P""'<br><br>/*********** 导入Excel<br>SELECT * <br>FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',<br> 'Data Source="c:/test.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...xactions<br><br>/*动态文件名<br>declare @fn varchar(20),@s varchar(1000)<br>set @fn = 'c:/test.xls'<br>set @s ='''Microsoft.Jet.OLEDB.4.0'',<br>''Data Source="'+@fn+'";User ID=Admin;Password=;Extended properties=Excel 5.0'''<br>set @s = 'SELECT * FROM OpenDataSource ('+@s+')...sheet1$'<br>exec(@s)<br>*/<br><br>SELECT cast(cast(科目编号 as numeric(10,2)) as nvarchar(255))+' ' 转换后的别名<br>FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',<br> 'Data Source="c:/test.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...xactions<br><br>/********************** EXCEL导到远程SQL<br>insert OPENDATASOURCE(<br> 'SQLOLEDB',<br> 'Data Source=远程ip;User ID=sa;Password=密码'<br>  
.库名.dbo.表名 (列名1,列名2)<br>SELECT 列名1,列名2<br>FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',<br> 'Data Source="c:/test.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...xactions<br><br><br>/** 导入文本文件<br>EXEC master..xp_cmdshell 'bcp dbname..tablename in c:/DT.txt -c -Sservername -Usa -Ppassword'<br><br>/** 导出文本文件<br>EXEC master..xp_cmdshell 'bcp dbname..tablename out c:/DT.txt -c -Sservername -Usa -Ppassword'<br>或<br>EXEC master..xp_cmdshell 'bcp "Select * from dbname..tablename" queryout c:/DT.txt -c -Sservername -Usa -Ppassword'<br><br>导出到TXT文本,用逗号分开<br>exec master..xp_cmdshell 'bcp "库名..表名" out "d:/tt.txt" -c -t ,-U sa -P password'<br><br>BULK INSERT 库名..表名<br>FROM 'c:/test.txt'<br>WITH (<br> FIELDTERMINATOR = ';',<br> ROWTERMINATOR = '/n'<br>)<br>--/* dBase IV文件<br>select * from <br>OPENROWSET('MICROSOFT.JET.OLEDB.4.0'<br>,'dBase IV;HDR=NO;IMEX=2;DATABASE=C:/','select * from [客户资料4.dbf]')<br>--*/<br><br>--/* dBase III文件<br>select * from <br>OPENROWSET('MICROSOFT.JET.OLEDB.4.0'<br>,'dBase III;HDR=NO;IMEX=2;DATABASE=C:/','select * from [客户资料3.dbf]')<br>--*/<br><br>--/* FoxPro 数据库<br>select * from openrowset('MSDASQL',<br>'Driver=Microsoft Visual FoxPro Driver;SourceType=DBF;SourceDB=c:/',<br>'select * from [aa.DBF]')<br>--*/<br><br>/**************导入DBF文件****************/<br>select * from openrowset('MSDASQL',<br>'Driver=Microsoft Visual FoxPro Driver;<br>SourceDB=e:/VFP98/data;<br>SourceType=DBF',<br>'select * from customer where country != "USA" order by country')<br>go<br>/***************** 导出到DBF ***************/<br>如果要导出数据到已经生成结构(即现存的)FOXPRO表中,可以直接用下面的SQL语句<br><br>insert into openrowset('MSDASQL',<br>'Driver=Microsoft Visual FoxPro Driver;SourceType=DBF;SourceDB=c:/',<br>'select * from [aa.DBF]')<br>select * from 表<br><br>说明:<br>SourceDB=c:/ 指定foxpro表所在的文件夹<br>aa.DBF 指定foxpro表的文件名.<br><br>/*************导出到Access********************/<br>insert into openrowset('Microsoft.Jet.OLEDB.4.0', <br> 'x:/A.mdb';'admin';'',A表) select * from 数据库名..B表<br><br>/*************导入Access********************/<br>insert into B表 selet * from openrowset('Microsoft.Jet.OLEDB.4.0', <br> 'x:/A.mdb';'admin';'',A表)<br><br>文件名为参数<br>declare @fname varchar(20)<br>set @fname = 'd:/test.mdb'<br>exec('SELECT a.* FROM opendatasource(''Microsoft.Jet.OLEDB.4.0'',<br> '''+@fname+''';''admin'';'''', topics) as a ')<br><br>SELECT * <br>FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',<br> 'Data Source="f:/northwind.mdb";Jet OLEDB
atabase Password=123;User ID=Admin;Password=;')...产品<br><br>********************* 导入 xml 文件<br><br>DECLARE @idoc int<br>DECLARE @doc varchar(1000)<br>--sample XML document.BR> SET @doc ='<br><root><br> <Customer cid= "C1" name="Janine" city="Issaquah"><br> <Order oid="O1" date="1/20/1996" amount="3.5" /><br> <Order oid="O2" date="4/30/1997" amount="13.4">Customer was very satisfied<br> </Order><br> </Customer><br> <Customer cid="C2" name="Ursula" city="Oelde" ><br> <Order oid="O3" date="7/14/1999" amount="100" note="Wrap it blue <br> white red"><br> <Urgency>Important</Urgency><br> Happy Customer.<br> </Order><br> <Order oid="O4" date="1/20/1996" amount="10000"/><br> </Customer><br></root><br>'<br>-- Create an internal representation of the XML document.<br>EXEC sp_xml_preparedocument.nbsp@idoc OUTPUT, @doc<br><br>-- Execute a SELECT statement using OPENXML rowset provider.<br>SELECT *<br>FROM OPENXML (@idoc, '/root/Customer/Order', 1)<br> WITH (oid char(5), <br> amount float, <br> comment ntext 'text()')<br>EXEC sp_xml_removedocument.nbsp@idoc<br><br> <br><br>???????<br><br>/**********************Excel导到Txt****************************************/<br>想用<br>select * into opendatasource(...) from opendatasource(...)<br>实现将一个Excel文件内容导入到一个文本文件<br><br>假设Excel中有两列,第一列为姓名,第二列为很行帐号(16位)<br>且银行帐号导出到文本文件后分两部分,前8位和后8位分开。<br><br><br>邹健:<br>如果要用你上面的语句插入的话,文本文件必须存在,而且有一行:姓名,银行账号1,银行账号2<br>然后就可以用下面的语句进行插入<br>注意文件名和目录根据你的实际情况进行修改.<br><br>insert into<br>opendatasource('MICROSOFT.JET.OLEDB.4.0'<br>,'Text;HDR=Yes;DATABASE=C:/'<br>)...[aa#txt]<br>--,aa#txt)<br>--*/<br>select 姓名,银行账号1=left(银行账号,8),银行账号2=right(银行账号,8) <br>from <br>opendatasource('MICROSOFT.JET.OLEDB.4.0'<br>,'Excel 5.0;HDR=YES;IMEX=2;DATABASE=c:/a.xls'<br>--,Sheet1$)<br>)...[Sheet1$]<br>如果你想直接插入并生成文本文件,就要用bcp<br>declare @sql varchar(8000),@tbname varchar(50)<br><br>--首先将excel表内容导入到一个全局临时表<br>select @tbname='[##temp'+cast(newid() as varchar(40))+']'<br> ,@sql='select 姓名,银行账号1=left(银行账号,8),银行账号2=right(银行账号,8) <br> into '+@tbname+' from <br>opendatasource(''MICROSOFT.JET.OLEDB.4.0''<br>,''Excel 5.0;HDR=YES;IMEX=2;DATABASE=c:/a.xls''<br>)...[Sheet1$]'<br>exec(@sql)<br><br>--然后用bcp从全局临时表导出到文本文件<br>set @sql='bcp "'+@tbname+'" out "c:/aa.txt" /S"(local)" /P"" /c'<br>exec master..xp_cmdshell @sql<br><br>--删除临时表<br>exec('drop table '+@tbname)<br>/********************导整个数据库*********************************************/<br>用bcp实现的存储过程<br>/*<br> 实现数据导入/导出的存储过程<br> 根据不同的参数,可以实现导入/导出整个数据库/单个表<br> 调用示例:<br>--导出调用示例<br>----导出单个表<br>exec file2table 'zj','','','xzkh_sa..地区资料','c:/zj.txt',1<br>----导出整个数据库<br>exec file2table 'zj','','','xzkh_sa','C:/docman',1<br><br>--导入调用示例<br>----导入单个表<br>exec file2table 'zj','','','xzkh_sa..地区资料','c:/zj.txt',0<br>----导入整个数据库<br>exec file2table 'zj','','','xzkh_sa','C:/docman',0<br><br>*/<br>if exists(select 1 from sysobjects where name='File2Table' and objectproperty(id,'IsProcedure')=1)<br> drop procedure File2Table<br>go<br>create procedure File2Table<br>@servername varchar(200) --服务器名<br>,@username varchar(200) --用户名,如果用NT验证方式,则为空''<br>,@password varchar(200) --密码<br>,@tbname varchar(500) --数据库.dbo.表名,如果不指定:.dbo.表名,则导出数据库的所有用户表<br>,@filename varchar(1000) --导入/导出路径/文件名,如果@tbname参数指明是导出整个数据库,则这个参数是文件存放路径,文件名自动用表名.txt<br>,@isout bit --1为导出,0为导入<br>as<br>declare @sql varchar(8000)<br><br>if @tbname like '%.%.%' --如果指定了表名,则直接导出单个表<br>begin<br> set @sql='bcp '+@tbname<br> +case when @isout=1 then ' out ' else ' in ' end<br> +' "'+@filename+'" /w'<br> +' /S '+@servername<br> +case when isnull(@username,'')='' then '' else ' /U '+@username end<br> +' /P '+isnull(@password,'')<br> exec master..xp_cmdshell @sql<br>end<br>else<br>begin --导出整个数据库,定义游标,取出所有的用户表<br> declare @m_tbname varchar(250)<br> if right(@filename,1)<>'/' set @filename=@filename+'/'<br><br> set @m_tbname='declare #tb cursor for select name from '+@tbname+'..sysobjects where xtype=''U'''<br> exec(@m_tbname)<br> open #tb<br> fetch next from #tb into @m_tbname<br> while @@fetch_status=0<br> begin<br> set @sql='bcp '+@tbname+'..'+@m_tbname<br> +case when @isout=1 then ' out ' else ' in ' end<br> +' "'+@filename+@m_tbname+'.txt " /w'<br> +' /S '+@servername<br> +case when isnull(@username,'')='' then '' else ' /U '+@username end<br> +' /P '+isnull(@password,'')<br> exec master..xp_cmdshell @sql<br> fetch next from #tb into @m_tbname<br> end<br> close #tb<br> deallocate #tb <br>end<br>go<br>/************* Oracle **************/<br>EXEC sp_addlinkedserver 'OracleSvr', <br> 'Oracle 7.3', <br> 'MSDAORA', <br> 'ORCLDB'<br>GO<br>delete from openquery(mailser,'select * from yulin')<br>select * from openquery(mailser,'select * from yulin')<br>update openquery(mailser,'select * from yulin where id=15')set disorder=555,catago=888<br>insert into openquery(mailser,'select disorder,catago from yulin')values(333,777)<br>补充:<br>对于用bcp导出,是没有字段名的.<br>用openrowset导出,需要事先建好表.<br>用openrowset导入,除ACCESS及EXCEL外,均不支持非本机数据导入