Importing text files with Bulk Insert on MS SQL Server 2000

本文介绍了一个使用SQL Server存储过程批量导入目录中特定格式文本文件的方法,并将文件存档到另一个目录。该过程可以配合FTP下载脚本从FTP服务器自动获取并导入文件。
Importing text files
Author Nigel Rivett

This process will import text files that arrive in a directory into a table.
It will process every file in the directory with the correct filemask and move the file to an archive directory on completion.
It can be used in conjunction with an ftpget SP to import files from a ftp server (see ftp).


/*
Create the directories c:/Transfer/Archive/
Create these text files in c:/transfer/
bcp1.txt
aaammm0120030101
bbbnnn0220030102
cccooo0320030103

bcp2.txt
abcxyz5320030104
defhhh1020030105
cdezzz1120030106
fsajku9920030107

Create the table
create table BCPData
(
fld1 varchar(20) ,
fld2 varchar(20) ,
fld3 int ,
fld4 datetime
)
Now run the import
exec ImportFiles 'c:/Transfer/' , 'c:/Transfer/Archive/' , 'bcp*.txt', 'MergeBCPData'

You can now move the files back from the archive directory to the transfer directory and import again.
If this SP call is scheduled then it will import and archive any files that arrive in the transfer directory with the corect file mask.

Enhancements
The import should be logged to a table in ImportFiles
The filename should have the datetime appended to it when archived if you wish to be able to import files with the same name
The MergeData SP should log the number of records imported

*/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ImportFiles]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[ImportFiles]
GO

create procedure ImportFiles
@FilePath varchar(1000) = 'c:/Transfer/' ,
@ArchivePath varchar(1000) = 'c:/Transfer/Archive/' ,
@FileNameMask varchar(1000) = 'bcp*.txt' ,
@MergeProc varchar(128) = 'MergeBCPData'

AS

set nocount on

declare @ImportDate datetime
select @ImportDate = getdate()

declare @FileName varchar(1000) ,
@File varchar(1000)

declare @cmd varchar(2000)

create table ##Import (s varchar(8000))
create table #Dir (s varchar(8000))

/*****************************************************************/
-- Import file
/*****************************************************************/
select @cmd = 'dir /B ' + @FilePath + @FileNameMask
delete #Dir
insert #Dir exec master..xp_cmdshell @cmd

delete #Dir where s is null or s like '%not found%'
while exists (select * from #Dir)
begin
select @FileName = min(s) from #Dir
select @File = @FilePath + @FileName

select @cmd = 'bulk insert'
select @cmd = @cmd + ' ##Import'
select @cmd = @cmd + ' from'
select @cmd = @cmd + ' ''' + replace(@File,'"','') + ''''
select @cmd = @cmd + ' with (FIELDTERMINATOR=''|'''
select @cmd = @cmd + ',ROWTERMINATOR = ''' + char(10) + ''')'

truncate table ##Import

-- import the data
exec (@cmd)

-- remove filename just imported
delete #Dir where s = @FileName

exec @MergeProc


-- Archive the file
select @cmd = 'move ' + @FilePath + @FileName + ' ' + @ArchivePath + @FileName
exec master..xp_cmdshell @cmd
end

drop table ##Import
drop table #Dir
go

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[MergeBCPData]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[MergeBCPData]
GO

create procedure MergeBCPData

AS
set nocount on

-- insert data to production table
insert BCPData
(
fld1 ,
fld2 ,
fld3 ,
fld4
)
select
fld1 = substring(s,1,3) ,
fld2 = substring(s,4,3) ,
fld3 = convert(int,substring(s,7,2)) ,
fld4 = convert(datetime,substring(s,9,8))
from ##Import

go


考虑柔性负荷的综合能源系统低碳经济优化调度【考虑碳交易机制】(Matlab代码实现)内容概要:本文围绕“考虑柔性负荷的综合能源系统低碳经济优化调度”展开,重点研究在碳交易机制下如何实现综合能源系统的低碳化与经济性协同优化。通过构建包含风电、光伏、储能、柔性负荷等多种能源形式的系统模型,结合碳交易成本与能源调度成本,提出优化调度策略,以降低碳排放并提升系统运行经济性。文中采用Matlab进行仿真代码实现,验证了所提模型在平衡能源供需、平抑可再生能源波动、引导柔性负荷参与调度等方面的有效性,为低碳能源系统的设计与运行提供了技术支撑。; 适合人群:具备一定电力系统、能源系统背景,熟悉Matlab编程,从事能源优化、低碳调度、综合能源系统等相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①研究碳交易机制对综合能源系统调度决策的影响;②实现柔性负荷在削峰填谷、促进可再生能源消纳中的作用;③掌握基于Matlab的能源系统建模与优化求解方法;④为实际综合能源项目提供低碳经济调度方案参考。; 阅读建议:建议读者结合Matlab代码深入理解模型构建与求解过程,重点关注目标函数设计、约束条件设置及碳交易成本的量化方式,可进一步扩展至多能互补、需求响应等场景进行二次开发与仿真验证。
### SQL Server 2000 安装无反应的可能原因及解决方案 SQL Server 2000 是一款较老版本的数据库管理系统,其安装过程可能会受到多种因素的影响而出现“无反应”的情况。以下是可能导致该问题的原因及其对应的解决方法: #### 1. **硬件兼容性** 如果目标计算机的硬件配置不符合 SQL Server 2000 的最低要求,则可能出现安装无响应的情况。例如,某些现代操作系统或高规格 CPU 可能不被支持。 - 确保运行环境满足官方文档中的最低硬件需求[^3]。 - 对于多核处理器的支持问题,可以参考 SQL Server 2005 中类似的案例。如果发现因多核导致的服务无法启动,建议更换到单核虚拟机环境中测试安装。 #### 2. **操作系统的兼容性** Windows XP 和 Windows Server 2003 是 SQL Server 2000 支持的主要平台。在更高版本的操作系统(如 Win7 或更新版)上直接安装 SQL Server 2000 很容易引发兼容性问题。 - 尝试通过“以兼容模式运行”设置来执行安装程序。具体步骤为:右键点击 `setup.exe` 文件 -> 属性 -> 兼容性 -> 勾选“以兼容模式运行”,并选择合适的旧版操作系统版本[^4]。 - 使用虚拟化技术创建一个受控环境(如 VirtualBox),模拟支持 SQL Server 2000 的经典操作系统。 #### 3. **权限不足** 若当前用户账户缺乏管理员权限,也可能造成安装过程中断或者界面冻结的现象。 - 确认正在使用的账号具有本地管理员权限。 - 在命令行中使用提升权限的方式重新启动安装向导: ```cmd runas /user:Administrator "path\to\setup.exe" ``` #### 4. **依赖组件缺失** SQL Server 2000 需要一些特定的 Microsoft 库文件作为前置条件才能正常工作。缺少这些库会阻止安装继续进行下去。 - 下载并手动安装 MDAC (Microsoft Data Access Components) 版本 2.8 SP1 或以上[^5]。 - 更新 .NET Framework 至最新可用补丁级别(尽管 SQL Server 2000 不完全依赖它,但有助于减少潜在冲突)。 #### 5. **防火墙/杀毒软件干扰** 实际部署期间,安全防护措施有时会被误认为威胁从而阻拦必要的网络通信活动。 - 暂时关闭实时保护功能后再重试整个流程;完成之后记得恢复原有设定以免影响日常网络安全状况。 --- ```python # 示例代码片段用于演示如何检查Python脚本是否具备所需模块 import os try: import pyodbc except ImportError as e: print(f"Error importing module {e}") finally: pass ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值