EXCEL 导入SQL SERVER存储过程
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER proc [dbo].[pr_xls_to_tb]
@path varchar(200),--EXCEL路径名
@tbName varchar(30),--表名
@stName varchar(30) --excel中要读的SHEET名
as
declare @sql varchar(500),--最后要执行的SQL
@stName_Real varchar(35),--真正的SHEET名
@drop_sql varchar(300) -- 如果表已存在,先删除
set @stName_Real = '[' + @stName + '$]'
--set @path = 'C:\Inetpub\wwwroot\CarStock_ExcelWeb\Upload\CarStock\国贸汽车库存表20090630.xls'
--set @tbName = 't32'
--set @stName = '[不良资产$]'
set @sql =
'SELECT *
into '+ @tbName +'
from OpenDataSource(' + char(39)+ 'Microsoft.Jet.OLEDB.4.0' + char(39)+', '
+ char(39) +'Data Source=' + @path +';User ID=Admin;Password=;Extended properties=Excel 5.0;' + char(39)+')...'+@stName_Real
set @drop_sql = '
if exists(select * from sysobjects where name = ' + char(39) +@tbName + char(39)+')
begin
drop table '+@tbName+'
end '
--print @drop_sql
exec (@drop_sql)--先删除表
exec (@sql)--再创建表
/*
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure
*/
调用:
exec pr_xls_to_tb 'C:\Tools\预算数据\ys_200908.xls' ,'ys_200908_new' ,'Source'
相关文档:
Differring Constraints:
Constraints can have the following attributes: DEFFERRABLE / NOT DEFFERRABLE, INITIALLY DEFFERRED / INITIALLY IMMEDIATE.
e.g.:
alter table dept2 add constraint dept2_id_pk primary key (department_id) deferrable initially deferred; // deferring constraint on creation. ......
PairWise subquery:
e.g.:
select * from wf_docsort where (ndocsortid,nmoduleinfoid) in (select ndocsortid, nmoduleinfoid from wf_docsort where instr(cname,'文')>0)
the above sql is the same function as:
select * from wf_docsort where ndocsortid = (select ndocsortid from wf_docsort where ......
数据类型
在 Microsoft SQL Server中,每个列、局部变量、表达式和参数都有一个相关的数据类型,这是指定对象可持有的数据类型(整型、字符、money 等等)的特性。SQL Server 提供系统数据类型集,定义了可与 SQL Server 一起使用的所有数据类型。下面列出系统提供的数据类型集。
可以定义用户定义的数据类型,其是系统提 ......
---附加数据库
sp_attach_db '数据库名','数据库全路径','数据库日志全路径'
---查看数据库逻辑文件名
RESTORE FILELISTONLY from disk = '备份文件'
---还原数据库
restore database hzrb from disk = '备份文件'
with move '主逻辑名' to '存放mdf路径' ......