简体   繁体   English

XQuery [value()]:期望字符串文字

[英]XQuery [value()]: A string literal was expected

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 XQuery [value()]:期望字符串文字

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? 您正在尝试将base64转移到varbinary ,对吗?
Well, the XML-approach is the recommended way to achieve this. 嗯,XML方法是实现这一目标的推荐方法。 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. 这里的问题是: sql:variable()期望声明的变量的名称为字符串文字。 Furthermore, value() expects two arguments: The Xpath and the data type. 此外, value()需要两个参数: Xpath和数据类型。 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. 你可能需要sql:column("SomeColumn") ,它允许使用列的值。

One example to demonstrate this 一个例子来证明这一点

--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)');

Check this for a tabular approach 检查这是一个表格方法

The table @base64 is a declared table variable, just use your original table instead: @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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM