给你一份参考资籵:
Copy text or image into or out of SQL Server 
Alexander Chigrik
[email protected]
In this article, I want to show, how you can copy a single text or
image value into or out of SQL Server by using the textcopy.exe utility.
You can find this utility in the directory containing the standard
SQL Server EXE files (C:\Mssql\Binn for SQL Server 6.5, by default
and C:\Mssql7\Binn for SQL Server 7.0, by default).Textcopy utility is not described in SQL Server Books Online, but you
can get its description by typing textcopy /? from the command prompt.
This is the description:Copies a single text or image value into or out of SQL Server. The value
is a specified text or image 'column' of a single row (specified by the
"where clause") of the specified 'table'.If the direction is IN (/I) then the data from the specified 'file' is
copied into SQL Server, replacing the existing text or image value. If the
direction is OUT (/O) then the text or image value is copied from
SQL Server into the specified 'file', replacing any existing file.TEXTCOPY [/S [sqlserver]] [/U [login]] [/P [password]]
  [/D [database]] [/T table] [/C column] [/W"where clause"]
  [/F file] [{/I | /O}] [/K chunksize] [/Z] [/?]  /S sqlserver       The SQL Server to connect to. If 'sqlserver' is not
                     specified, the local SQL Server is used.
  /U login           The login to connect with. If 'login' is not specified,
                     a trusted connection will be used.
  /P password        The password for 'login'. If 'password' is not
                     specified, a NULL password will be used.
  /D database        The database that contains the table with the text or
                     image data. If 'database' is not specified, the default
                     database of 'login' is used.
  /T table           The table that contains the text or image value.
  /C column          The text or image column of 'table'.
  /W "where clause"  A complete where clause (including the WHERE keyword)
                     that specifies a single row of 'table'.
  /F file            The file name.
  /I                 Copy text or image value into SQL Server from 'file'.
  /O                 Copy text or image value out of SQL Server into 'file'.
  /K chunksize       Size of the data transfer buffer in bytes. Minimum
                     value is 1024 bytes, default value is 4096 bytes.
  /Z                 Display debug information while running.
  /?                 Display this usage information and exit.You will be prompted for any required options you did not specify.
 You can use the following stored procedure to simplify the using of
textcopy utility:CREATE PROCEDURE sp_textcopy ( 
  @srvname     varchar (30),
  @login       varchar (30),
  @password    varchar (30),
  @dbname      varchar (30),
  @tbname      varchar (30),
  @colname     varchar (30),
  @filename    varchar (30),
  @whereclause varchar (40),
  @direction   char(1))
AS
DECLARE @exec_str varchar (255)
SELECT @exec_str = 
         'textcopy /S ' + @srvname +
         ' /U ' + @login + 
         ' /P ' + @password + 
         ' /D ' + @dbname +
         ' /T ' + @tbname + 
         ' /C ' + @colname + 
         ' /W "' + @whereclause +
         '" /F ' + @filename + 
         ' /' + @direction
EXEC master..xp_cmdshell @exec_str
 This is the example to copy image into SQL Server database pubs,
table pub_info, column name logo from picture.bmp file where pub_id='0736':
sp_textcopy @srvname = 'ServerName', 
            @login = 'Login', 
            @password = 'Password', 
            @dbname = 'pubs', 
            @tbname = 'pub_info',
            @colname = 'logo', 
            @filename = 'c:\picture.bmp', 
            @whereclause = " WHERE pub_id='0736' ", 
            @direction = 'I'
 By the way, you should insert something into text/image column before
copy text/image into it. I mean, this value should not be null.
You should write (for example):INSERT INTO [dbo].[images] VALUES ('1', 0x0, null)
 instead of:INSERT INTO [dbo].[images] VALUES ('1', null, null)
 Otherwise, you will get the following error message:ERROR: Text or image pointer and timestamp retrieval failed.

解决方案 »

  1.   

    如果你的文本格式与数据库结构相一致的话,只用一条简单命令就可以了.
    查看一下帮助里的bulk insert 语句的用法,应该满足你的要求
      

  2.   

    用BCP命令轻松搞定bcp xianju..aaa out c:\aaa.txt -c -C936 -Ugdxinxi -P0416 //导出数据
    bcp xianju..aaa in c:\aaa.txt -c -C936 -Ugdxinxi -P0416 //导入数据
    ms sql server 7.0的数据迁移 
     
    ms sql server 7.0的数据迁移
    数据库的主要作用是管理现代信息数据并对信息数据加以处理,因此,维护数据库也是一项艰巨的工作。对系统维护人员和数据库设计者来说,经常会遇到数据库的升级、数据的迁移或是不同数据库之间的数据转换等问题。MS SQL Server 7.0中提供对数据迁移的强大功能,主要体现在提供了通过向导的易操作性和多方法通用实用性。下面将对MS SQL Server 7.0中数据迁移的技术及需注意的问题作介绍。数据转换服务(DTS)的数据迁移功能
    数据转换服务允许你在Microsoft SQL Server, OLE DB、 ODBC或文本文件(*.TXT)等结构的多个数据源之间输入和输出数据,以及在运行SQL Server 7.0的多台计算机之间执行数据库和数据库对象的传输。你还可以用DTS来执行数据转换,以便用它来通过一个在线事务处理系统建立数据仓库和数据商场。DTS也提供批通信程序(BCP)来输出和输入数据。主要提供以下几种功能,但是在操作上差不多,对不同的数据源将会有相应的不同的操作结果:
    数据转换服务导入和导出数据;
    数据转换服务转换数据;
    数据转换服务传输数据库对象。
    数据转换服务可以轻易地以图形化方式建立,并且通过与其他数据源进行导入、导出,以及在不同数据源之间进行转换来维护数据仓库的实现。数据转换服务导入向导和导出向导可以在19种不同的数据源之间转换数据。这19种数据源如表1所示。
    数据转换服务的导入和导出数据是SQL Server中提供通用的数据格式输入或输出数据的数据交流。具体的操作如下:
    1. 在SQL Server企业管理器中的Tools(工具)菜单上,选择Data Transformation Services(数据转换服务),然后选择Import Data(导入数据)。 
    2. 在Choose a Data Source(选择数据源)对话框中选择所需的数据源(即表1所列出的19种不同的数据源)。 
    3. 在Choose a Destination(选择目标)对话框中,选择Microsoft OLE DB Provider for SQL Server,选择数据库服务器,然后单击必要的验证方式(与图1相似)。 
    4. 在第二步中选择不同的数据源,将有三种不同的数据转换方式(见图2)。
    (1) Copy table(s) from the source database(指定表格复制)
    (2) Use a query to specify the data to transfer 通过Query(查询)对话框生成查询语句,查询语句由向导执行生成。
    (3) Transfer objects and data between SQL server 7.0 database只在SQL server 7.0之间传输数据对象,包括数据库中的对象和数据。
    在Specify Table Copy(指定表格复制),单击Copy tables(复制表格)。(见图3〕 。
    4. 在Select Source Tables(选择源表格)对话框中,单击Select All(全部选定)。 
    数据转换服务转换数据提供了相当完善的导入和导出机制,并使您能够在转换的任何一步中使用脚本。您可以用“Visual Basic 脚本编辑 (VBScript)”、“JScript”或“Perl”的技术来扩展 DTS 的能力。(见图4),在默认情况下,“SQL Server”有一个定义的文件夹,可存储任何转换,而且“Enterprise Manager”提供了创建和编辑DTS 程序包的大的图形用户界面 (GUI)。在下面的示例中,DTA程序包将创建该表,加载到Text文件中,然后运行某个脚本,将数据转换到“SQL Server“的表中。
    '********************************************************************
    Visual Basic 转换脚本
    把每个源列复制到目标列
    '********************************************************************
    Function Main()
    DTSDestination("CZYBH") = DTSSource("CZYBH")
    DTSDestination("CZYMM") = DTSSource("CZYMM")
    DTSDestination("CZYXM") = DTSSource("CZYXM")
    DTSDestination("CZYGS") = DTSSource("CZYGS")
    Main = DTSTransformStat_OK
    End Function
    重要的是,要注意脚本可能不是操作数据的最佳方式,尤其是您的数据集很大的话。如果您有大量数据需要转换,而且性能也很重要,则您可能需要考虑使用Visual Basic或C++来创建COM组件,然后从DTS内部调用该组件。也就是说,如果性能并不重要,并且要在数据导入/导出时对它进行转换,则脚本为您提供了实现这一点的灵活机制,并使您能够将所有代码存储到“SQL Server”数据库中,使部署变得相当简单。
    附注:在安装Office 2000或Microsoft SQL Server 7.0 客户端程序之后由SQL Server 6.5所附的MS Query存取SQL Server 6.5的资料得到的会是乱码。这可能是因为您使用SQL Server ODBC 3.70.06.23版的Driver。请在配置ODBC时不要设定“Perform translation for character data”,并且升级SQL Server ODBC 版本至3.70.08.20。SQL server 7.0附的ODBC 版本是3.70.06.23,SQL server 7.0 SP2附带的ODBC 版本是3.70.08.20,SQL server 7.0 SP2版本号为Microsoft SQL Server 7.00 - 7.00.842。可以从美国微软网站 http://www.microsoft.com/sql/下载。批通信程序(BCP)的输入和输出数据
    在一般情况,源SQL Server向目的SQL Server转换时,它们的字符集和排序次序是相同的,都可以通过上述几点进行。但是,源SQL Server和目的SQL Server字符集和排序次序不一样时,采用数据转换服务系统将出错,这时可通过批通信程序(BCP)输入和输出数据。如:在SQLServer 6.5向SQLServer7.0升级的过程中, SQLServer7.0为双字节版本,可安装于Windows NT Server4.0中文版上。但是其默认的安装字符集和排序次序是:1252/ISO Character Set(Default)和Dictionary order,case-insensitive。跟中文版的SQL 6.5是不一样的(具体的比较请查看表2)。在升级的时候,没有改变这个默认的设置,结果中文数据在转化后,变成怪字符。假设数据库为:NEWBJTEL,源SQL Server为英文,目的SQL Server为中文,CZYTAB为数据库NEWBJTEL的数据表。操作步骤如下:
    1. 在源SQL Server机器上,DOS状态,进入c:\mssql\binn,运行如下命令:
    bcp NEWBJTEL..CZYTAB OUT C:\shareJxzdf\NEWBJTELDATA\CZYTAB.TXT -c -C1252 -Usa -Pas
    对于该命令的解释:bcp.exe是一个用来将SQLServer数据拷入或拷出操作系统文件的命令行实用程序。为了提高速度,必须在数据库中设置为:select into/bulk copy方式。该命令可以选择众多的参数,其中-c表示拷贝后的文件将以字符串的形式记录,C后面跟字符代码,英文版的代号为:1252。简体中文的代号是:936。-U后面是帐号名,P后面是密码。如果帐号名是sa,密码为空,那么格式为:-Usa -P。另外可选的一些参数为:“-t ,”意思是在每一个字段之后用“,”来分隔,“-r \n”,意思是在每一行的末尾用“\ n”(即换行符)来分隔;其他的参数可以在在线帮助中找到。CZYTAB为数据库NEWBJTEL中的一个表格名,C:\shareJxzdf\NEWBJTELDATA\CZYTAB.TXT指定导出文件的位置。如果有多个表格,必须用多个bcp语句,建议做成一个批处理,然后一并执行。
    2. 在目的SQL Server机器上,将BCP输出的文本文件拷贝到目的SQL Server机器,在DOS状态,进入c:\mssql\binn,运行如下命令:
    bcp NEWBJTEL..CZYTAB IN C:\shareJxzdf\NEWBJTELDATA\CZYTAB.TXT -c -C936 -Usa -Pas
    或:在Query Analyzer中打开一个新的Query窗口,然后运行下面的命令:
    BULK INSERT CZYTAB FROM ' C:\shareJxzdf\NEWBJTELDATA\CZYTAB.TXT '
    WITH (
    CODEPAGE = 936
    )
    可以同时用多个命令来对多个表格进行数据恢复,只要改变表格名和相应的数据文件名即可。其中CODEPAGE = 936是指数据以“中文简体”的形式被恢复。
    运行成功后的反馈信息类似:“(20 row(s) affected)“,表明20行数据被插入。
    附注:
    如果在对SQL Server的升级时把字符集和排序次序安装成不一样时,应按以下步骤操作:
    1.先从SQL Server 7.0中备份所有数据库的SQL脚本,因为数据库中有indexes、stored procedures、triggers等需进行备份。
    2.从SQL Server 7.0中备份所有数据库数据,即利用BCP.exe。
    3.重建主数据库(利用Rebuildm.exe),改变字符集及排序次序等。
    4.重建所有数据库,利用备份的SQL脚本。
    5.再把数据导回去。Windows 98向NT 4 SQL Server 7.0的数据移植
    数据库在不同电脑的SQLServer 7.0中互相移植将变得十分的轻松。在Windows 98平台上完成转化,然后利用SQL Server中提供的存储过程sp_attach_db来把数据库恢复到Windows NT 4中。
    具体的做法很简单,把Windows 98 SQL Server中对应数据库的两个文件:*.mdf,*.ldf拷贝到Windows NT平台的c:\mssql7\data中,运行Query Analyzer,在命令窗口中执行如下语句:
    EXEC sp_attach_db @dbname = N'COMPANY', 
    @filename1 = N'C:\mssql7\data\ company_Data.MDF', 
    @filename2 = N'C:\mssql7\data\ company_Log.LDF'
    运行完毕后,系统就会把COMPANY数据库加入到SQL Server Group中,顺利完成移植。相关的命令格式请查考有关指南。总结
    数据库数据迁移还有数据的备份和恢复,请参考有关的指南,SQL Server 7.0提供了相应的向导。由于关系型数据库管理系统产品SQL Server 7.0功能和结构的巨大改变使得不能简单地使用以前版本的数据库内容。但是,系统提供了一个版本升级向导,使用该升级向导可以轻易地把以前版本中的数据库平滑地迁移到新的版本中。SQL Server 7.0中对数据的迁移提供了兼顾其他数据库与SQL Server 7.0的平滑迁移,更考虑了多方面的实用性功能的提供,确保数据迁移的正确性。