我要执行以下代码:
if exists(select * from FileUploadFileTable..sysobjects where xtype='u' and name='##Tmp830963' )
drop table ##Tmp830963
CREATE table ##Tmp830963 (RowId int,Files varchar(MAX) ,Files_Name NVARCHAR(MAX), Files_Size bigint,Job_Id bigint, Files_Type VARCHAR(MAX) , User_id bigint ,User_Name NVARCHAR(MAX)) Insert into ##Tmp830963(RowId,Files,Files_Name,Files_Size , Files_Type) select A.row_num,A.Items,O.Items,B.Items,C.Items
from(
SELECT Items,row_number() over (order by (select 0)) as row_num
FROM dbo.Func_Split('/9j/AqAAAAB4CgAwAEAAABAAAAAAA', '^') ) A
join
(SELECT Items,row_number() over (order by (select 0)) as row_num
FROM dbo.Func_Split('tt^', '^') ) O on O.row_num=A.row_num
join
(SELECT Items,row_number() over (order by (select 0)) as row_num
FROM dbo.Func_Split('12^', '^'))B on A.row_num=B.row_num
join
(SELECT Items,row_number() over (order by (select 0)) as row_num
FROM dbo.Func_Split('png^', '^'))C
on C.row_num=A.row_num
update ##Tmp830963 set User_Name=100update ##Tmp830963 set Job_Id='20' update ##Tmp830963 set User_id='1' select * from ##Tmp830963 DECLARE @OutputTbl TABLE (ID uniqueidentifier) INSERT INTO [Uploads] ([file_stream],[name]) OUTPUT INSERTED.stream_id INTO @OutputTbl(ID) select cast(N'' as xml).value('xs:base64Binary(sql:variable(Files))', 'varbinary(max)') ,Files_Name from ##Tmp830963 while (select count(*) from @OutputTbl) > 0 begin INSERT INTO [dbo].[FileDescriptions] ([User_ID] ,[FileName],Stream_id,[Size],Job_Id) select [User_id] ,cast((select MAX(ID) from @OutputTbl ) as nvarchar(max) ),(select MAX(ID) from @OutputTbl) , Files_Size , Job_Id from ##Tmp830963 where RowId=(select top 1(RowId) from ##Tmp830963) delete @OutputTbl where ID =(select MAX(ID) from @OutputTbl ) end
但是我得到这个错误:
XQuery [value()]:应该是字符串文字
在这一行:
cast(N'''' as xml).value(''xs:base64Binary(sql:variable(Files))''
答案 0 :(得分:0)
乍一看,这看起来有点不可思议。我很确定,这可以用其他方法解决。
您正在尝试将base64
转移到varbinary
,对吗?
好吧,XML方法是实现此目的的推荐方法。但是,您的问题似乎出在这里:
cast(N'''' as xml).value(''xs:base64Binary(sql:variable(Files))''
这是您尝试动态创建所需语句的尝试,因此将单引号加倍。这将转换为
cast(N'' as xml).value('xs:base64Binary(sql:variable(Files))'
这里的问题是:sql:variable()
期望声明的变量的名称为字符串文字。此外,value()
需要两个参数:Xpath
和数据类型。您需要像这样的东西
cast(N'' as xml).value('sql:variable("@Files")','varbinary(max)')
可能需要sql:column("SomeColumn")
,它允许使用列的值。
--We use FOR XML to get the binary 0x1234 as base64
SELECT 0x1234 FOR XML PATH(''); --returns "EjQ="
--Now I place the corresponding base64 string in a string variable
DECLARE @base64 VARCHAR(100)='EjQ=';
--And this is, how XML is returning the binary 0x1234 from the base64-string
SELECT CAST('' AS XML).value('sql:variable("@base64")','varbinary(max)');
这将返回相同的结果,并且看起来更简单:
SELECT CAST(@base64 AS XML).value('.','varbinary(max)');
表@base64
是声明的表变量,只需使用原始表即可:
--Declare a table variable with one string column and insert the base64 of 0x1234
DECLARE @base64 TABLE(Files VARCHAR(100));
INSERT INTO @base64 VALUES('EjQ=');
--use the same trick as above to get the varbinary back
SELECT CAST(Files AS XML).value('.','varbinary(max)')
FROM @base64;