I want to execute this code:
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
But I get this error:
XQuery [value()]: A string literal was expected
On this line:
cast(N'''' as xml).value(''xs:base64Binary(sql:variable(Files))''
On the first sight this looks a bit weird. I'm pretty sure, that this can be solved differently.
You are trying to transfer base64
to varbinary
, correct?
Well, the XML-approach is the recommended way to achieve this. However, your problem seems to be situated here:
cast(N'''' as xml).value(''xs:base64Binary(sql:variable(Files))''
This is your attempt to create the needed statement dynamically, hence the doubled single quotes. This will translate to
cast(N'' as xml).value('xs:base64Binary(sql:variable(Files))'
The problem here is: sql:variable()
expects the name of a declared variable as a string literal. Furthermore, value()
expects two arguments: The Xpath
and the data type. You'd need something like this
cast(N'' as xml).value('sql:variable("@Files")','varbinary(max)')
Might be you need sql:column("SomeColumn")
, which allows to use the value of a column.
--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)');
And this would return the same and looks a bit easier:
SELECT CAST(@base64 AS XML).value('.','varbinary(max)');
The table @base64
is a declared table variable, just use your original table instead:
--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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.