尝试下载'图像'时的日期类型转换错误数据库

时间:2018-03-05 14:20:39

标签: sql sql-server tsql sql-server-2005

我正在尝试下载存储在数据库表中的所有文件/文档。表的结构如下:

CREATE TABLE [dbo].[eAttachment](
    [eKey] [nvarchar](250) NOT NULL,
    [eSize] [int] NULL,
    [eContents] [image] NULL,
 CONSTRAINT [ePKU_eAttachment] PRIMARY KEY CLUSTERED 
(
    [eKey] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

典型内容:

esize: 173586
ekey: 0 0000000000000000000000000005010 Filename.pdf
econtents: 0x7B00350030000037003500460030002D003400310046....etc

我尝试使用的SQL在第30行(标有下面的注释)失败,错误如下。

  

(5行(s)受影响)

     

(5行(s)受影响)

     

Msg 8114,Level 16,State 5,Line 30

     

将数据类型varchar转换为bigint时出错。

这是完整的代码

DECLARE @outPutPath varchar(100)
, @i bigint
, @init int
, @econtents varbinary(max) 
, @fPath varchar(max)  
, @folderPath  varchar(max) 
, @efolderName nvarchar(31)
, @ekey nvarchar(250)

DECLARE @Doctable TABLE (id bigint identity(1,1), ekey  nvarchar(250) , esize int, [econtents] varbinary(max) )

INSERT INTO @Doctable([ekey] , [esize],[econtents])
Select top 5 ekey, esize, econtents from eattachment
select * from @doctable

SELECT @i = COUNT(1) FROM @Doctable

WHILE @i <= 5
BEGIN 

    SET @ekey = (SELECT STUFF(LEFT(ekey,33),1,1,'') from @doctable where id = @i)
    SET @efoldername =  (select top 1 efoldername
                                    from efolder 
                                    where efolderid 
                                    like @ekey
                                    )
    SET @outPutPath = '\\location\to\store\files'

    SELECT --fails here
     @econtents = [econtents],
     @fPath = @outPutPath + '\'+ [id] + '\' + @efolderName + '\' + RIGHT(ekey, LEN(ekey) - 33), 
     @folderPath = @outPutPath + '\'+ [id]
    FROM @Doctable WHERE id = @i

  EXEC  [dbo].[CreateFolder]  @folderPath
  EXEC sp_OACreate 'ADODB.Stream', @init OUTPUT; 
  EXEC sp_OASetProperty @init, 'Type', 1;  
  EXEC sp_OAMethod @init, 'Open'; 
  EXEC sp_OAMethod @init, 'Write', NULL, @econtents; 
  EXEC sp_OAMethod @init, 'SaveToFile', NULL, @fPath, 2; 
  EXEC sp_OAMethod @init, 'Close'; 
  EXEC sp_OADestroy @init; 

  print 'Document saved to: '+  @fPath   

SELECT @econtents = NULL  
, @init = NULL
, @fPath = NULL  
, @folderPath = NULL
SET @i = 1
END

为什么失败了这个错误?我希望它只是将文件复制到我的文件夹。

我无法看到任何varchar-&gt; bigint转换正在发生,特别是在它说失败的行上。

我怀疑它与源表中econtentsimage和临时表中varbinary的列有关。 SQL告诉我,我不允许在程序/声明变量中使用image,所以我认为它会自动转换?

编辑:这与其他帖子无关,因为这是日期时间转换问题。有人已经发布了一个有效的答案,但他们已将其删除

1 个答案:

答案 0 :(得分:1)

这是你的问题:

 @fPath = @outPutPath + '\'+ [id] + '\' + @efolderName + '\' + RIGHT(ekey, LEN(ekey) - 33), 
 @folderPath = @outPutPath + '\'+ [id]

将字符串连接到bigint时,SQL Server将尝试将字符串隐式转换为bigint和sum而不是concat,除非您将bigint显式转换为字符串数据类型。

将其更改为

 @fPath = @outPutPath + '\'+ CAST([id] as varchar(21)) + '\' + @efolderName + '\' + RIGHT(ekey, LEN(ekey) - 33), 
 @folderPath = @outPutPath + '\'+ CAST([id] as varchar(21))

(你需要21个字符,因为bigint的最小值有20位数和一个减号)。