我的目标是将VARBINARY转换为VARCHAR。这是我的代码:
DECLARE @objectSidAsByteArray varbinary(max)
execute sp_executesql N'SELECT * FROM OPENQUERY("MyLinkedServer",
''SELECT objectGUID FROM ''''LDAP://bla1.bla2'''' WHERE name = ''''myName
mySurname'''' '')', N'@ VARBINARY(max) OUTPUT',
@objectSidAsByteArray OUTPUT
SELECT @objectSidAsByteArray
DECLARE @objectSIDAsString VARCHAR(max) = CONVERT (NVARCHAR(max),@objectSidAsByteArray)
SELECT @objectSIDAsString
输出:
0x8585EEB0CBFD34449644B2A44B886FCC
NULL
事件虽然@objectSidAsByteArray输出了正确的值,但它有问题。它的类型似乎没问题(VARBINARY),不是吗?如果我使用下面的代码而不是@objectSidAsByteArray,转换效果很好:
DECLARE @objectSID varbinary(max)
set @objectSID = 0x8585EEB0CBFD34449644B2A44B886FCC
CONVERT (NVARCHAR(max),@objectSID)
那么我对@objectSidAsByteArray的错误是什么?
答案 0 :(得分:1)
您的sp_executesql
语句未在变量@objectSidAsByteArray中返回结果,请尝试执行此查询:
DECLARE @objectSidAsByteArray varbinary(max)
execute sp_executesql N'SELECT TOP 1 @objectSidAsByteArray = objectGUID FROM OPENQUERY("MyLinkedServer",
''SELECT objectGUID FROM ''''LDAP://bla1.bla2'''' WHERE name = ''''myName
mySurname'''' '')', N'@objectSidAsByteArray VARBINARY(max) OUTPUT',
@objectSidAsByteArray OUTPUT
SELECT @objectSidAsByteArray
DECLARE @objectSIDAsString VARCHAR(max) = CONVERT (NVARCHAR(max),@objectSidAsByteArray)
SELECT @objectSIDAsString
答案 1 :(得分:0)
不应该
DECLARE @objectSIDAsString NVARCHAR(max)
答案 2 :(得分:0)
DECLARE @objectSidAsByteArray varbinary(max)
execute sp_executesql N' select @i = 0x8585EEB0CBFD34449644B2A44B886FCC', N'@i VARBINARY(max) OUTPUT', @objectSidAsByteArray OUTPUT
SELECT @objectSidAsByteArray
DECLARE @objectSIDAsString NVARCHAR(max) = CONVERT (NVARCHAR(max),@objectSidAsByteArray)
SELECT @objectSIDAsString
go
DECLARE @objectSidAsByteArray varbinary(max)
execute sp_executesql N' select 0x8585EEB0CBFD34449644B2A44B886FCC', N'@ VARBINARY(max) OUTPUT', @objectSidAsByteArray OUTPUT
SELECT @objectSidAsByteArray
答案 3 :(得分:0)
您需要声明未在代码中声明的输出变量。如下例所示: -
DECLARE @objectSidAsByteArray varbinary(max)
execute sp_executesql N'SELECT @objectSidAsByteArray1 =0x21232F297A57A5A743894A0E4A801FC3', N'@objectSidAsByteArray1 VARBINARY(max) OUTPUT', @objectSidAsByteArray OUTPUT
SELECT @objectSidAsByteArray
DECLARE @objectSIDAsString VARCHAR(max) = CONVERT(NVARCHAR(max),@objectSidAsByteArray,1)
SELECT @objectSIDAsString