尝试从数据库下载 'image' 列内容时出现日期类型转换错误
Datetype conversion error when trying to download 'image' column contents from database
我正在尝试下载存储在数据库 table 中的所有 files/documents。 table的结构如下:
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 row(s) affected)
(5 row(s) affected)
Msg 8114, Level 16, State 5, Line 30
Error converting data type varchar to 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->bigint 转换,尤其是在它表示失败的行上。
我怀疑这与源 table 中的 econtents
列类型 table 和临时 table 中的 varbinary
类型有关。 SQL 告诉我不允许在 procedures/declaring 变量中使用 image
,所以我认为它会自动转换?
编辑:这与另一个 post 无关,因为那是日期时间转换问题。有人已经 post 给出了有效的答案,但他们已将其删除
这是你的问题:
@fPath = @outPutPath + '\'+ [id] + '\' + @efolderName + '\' + RIGHT(ekey, LEN(ekey) - 33),
@folderPath = @outPutPath + '\'+ [id]
将字符串连接到 bigint 时,SQL 服务器将尝试将字符串隐式转换为 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 位数字和一个负号)。
我正在尝试下载存储在数据库 table 中的所有 files/documents。 table的结构如下:
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 row(s) affected)
(5 row(s) affected)
Msg 8114, Level 16, State 5, Line 30
Error converting data type varchar to 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->bigint 转换,尤其是在它表示失败的行上。
我怀疑这与源 table 中的 econtents
列类型 table 和临时 table 中的 varbinary
类型有关。 SQL 告诉我不允许在 procedures/declaring 变量中使用 image
,所以我认为它会自动转换?
编辑:这与另一个 post 无关,因为那是日期时间转换问题。有人已经 post 给出了有效的答案,但他们已将其删除
这是你的问题:
@fPath = @outPutPath + '\'+ [id] + '\' + @efolderName + '\' + RIGHT(ekey, LEN(ekey) - 33),
@folderPath = @outPutPath + '\'+ [id]
将字符串连接到 bigint 时,SQL 服务器将尝试将字符串隐式转换为 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 位数字和一个负号)。