原帖地址:
问题描述:
我准备在MS-SQL服务器端建立一个存储过程,需要用纯SQL语句存取文件。
求老师指导一下!!
解决方案:
有些方法用于导入和导出图片、照片、视频、文档等BLOB类型数据。
SQL Server 2000:
在binn目录下的未公开工具可用,提示你提供相应的参数:
textcopy
使用textcopy对sqlserver的Image字段进行读取和写入
用存储过程方法:
1、建立过程
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 GO2、建表和初始化数据
create table 表名 (编号 int,p_w_picpath列名 p_w_picpath) go insert 表名 values(1,0x) -- 必须的,且不是null insert 表名 values(2,0x) -- 必须的,且不是null go3、读入
sp_textcopy '你的服务器名','sa','你的密码','库名','表名','p_w_picpath列名','c:\图片.bmp','where 编号=1','I' --注意条件是 编号=1 sp_textcopy '你的服务器名','sa','你的密码','库名','表名','p_w_picpath列名','c:\bb.doc','where 编号=2','I' --注意条件是 编号=2 go4、读出成文件
sp_textcopy '你的服务器名','sa','你的密码','库名','表名','p_w_picpath列名','c:\图片.bmp','where 编号=1','O' --注意条件是 编号=1 sp_textcopy '你的服务器名','sa','你的密码','库名','表名','p_w_picpath列名','c:\bb.doc','where 编号=2','O' --注意条件是 编号=2 go
注意:
如果报textcopy不是可执行文件的话,你就到
C:\Program Files\Microsoft SQL Server\MSSQL\Binn 目录下拷备 textcopy.exe到: C:\Program Files\Microsoft SQL Server\80\Tools\Binn
开启xp_cmdshell方法:
--启用xp_cmdshell EXEC sp_configure 'show advanced options', 1;RECONFIGURE;EXEC sp_configure 'xp_cmdshell', 1;RECONFIGURE; --关闭xp_cmdshell EXEC sp_configure 'show advanced options', 1;RECONFIGURE;EXEC sp_configure 'xp_cmdshell', 0;RECONFIGURE; --如果提示拒绝访问,一般是目录权限的问题,可新建一个目录,加入Everyone完全控制权限即可。
SQL Server 2005:
通过OPENROWSET和bcp:
示例1:
-- SQL Server import p_w_picpath - SQL Server export p_w_picpath-- Extract p_w_picpath SQL Server - SQL Server export binary dataUSE AdventureWorks;GO -- Create p_w_picpath warehouse for importing p_w_picpath into sql databaseCREATE TABLE dbo.PhotoLibrary ( PhotoLibraryID INT IDENTITY ( 1 , 1 ) PRIMARY KEY, ImageName VARCHAR(100), Photo VARBINARY(MAX))GO -- SQL Server import p_w_picpath - sql storing p_w_picpaths database sql serverINSERT INTO dbo.PhotoLibrary([ImageName])VALUES ('MadisonAVE.JPG')UPDATE dbo.PhotoLibrarySET Photo = (SELECT *FROM OPENROWSET(BULK 'e:\p_w_picpath\photo\MadisonAVE.JPG',SINGLE_BLOB) AS x)WHERE [ImageName] = 'MadisonAVE.JPG'GO -- Check table populationSELECT *FROM dbo.PhotoLibraryGO -- SQL Server export p_w_picpathDECLARE @Command NVARCHAR(4000) -- Keep the command on ONE LINE - SINGLE LINE!!! - broken here for presentationSET @Command = 'bcp "SELECT Photo FROM AdventureWorks.dbo.PhotoLibrary" queryout "e:\p_w_picpath\photo\expMadisonAVE.jpg" -T -n -SPROD\SQL2005'PRINT @Command -- debuggingEXEC xp_cmdshell @CommandGO
示例2:
-------------- T-SQL Export all p_w_picpaths in table to file system folder-- Source table: Production.ProductPhoto - Destination: K:\data\p_w_picpaths\productphoto\------------ USE AdventureWorks2008;GO DECLARE @Command VARCHAR(4000), @PhotoID INT, @ImageFileName VARCHAR(128)DECLARE curPhotoImage CURSOR FOR -- Cursor for each p_w_picpath in tableSELECT ProductPhotoID, LargePhotoFileNameFROM Production.ProductPhotoWHERE LargePhotoFileName != 'no_p_w_picpath_available_large.gif'OPEN curPhotoImageFETCH NEXT FROM curPhotoImageINTO @PhotoID, @ImageFileNameWHILE (@@FETCH_STATUS = 0) -- Cursor loop BEGIN -- Keep the bcp command on ONE LINE - SINGLE LINE!!! - broken up for presentationSET @Command = 'bcp "SELECT LargePhoto FROM AdventureWorks2008.Production.ProductPhoto WHERE ProductPhotoID = ' +convert(VARCHAR,@PhotoID) + '" queryout "K:\data\p_w_picpaths\productphoto\' + @ImageFileName + '" -T -n -SHPESTAR'PRINT @Command – debugging /* bcp "SELECT LargePhoto FROM AdventureWorks2008.Production.ProductPhotoWHERE ProductPhotoID = 69" queryout"K:\data\p_w_picpaths\productphoto\racer02_black_f_large.gif" -T -n -SHPESTAR*/ EXEC xp_cmdshell @Command -- Carry out p_w_picpath export to file from db tableFETCH NEXT FROM curPhotoImageINTO @PhotoID, @ImageFileNameEND -- cursor loopCLOSE curPhotoImageDEALLOCATE curPhotoImage /*outputNULLStarting copy...NULL1 rows copied.Network packet size (bytes): 4096Clock Time (ms.) Total : 16 Average : (62.50 rows per sec.)NULL.....*/ ------------