如何在SQL Server中将包含UniqueIdentifier列表的varchar转换为UniqueIdentifier列表

如何在SQL Server中将包含UniqueIdentifier列表的varchar转换为UniqueIdentifier列表,sql,sql-server,sql-server-2017,Sql,Sql Server,Sql Server 2017,在查询中,employeeId列的数据类型为uniqueidentifier。我在执行时遇到以下错误 从字符串转换为uniqueidentifier时,转换失败 如何将UniqueIdentifier GUID字符串转换为UniqueIdentifier列表 我已尝试执行如中所示的转换 但是它不起作用。如果您这样声明变量: declare @tt nvarchar 您将得到一个长度正好为一个字符的字符串 你应该永远没有例外!为varchar和nvarchar变量定义显式长度: declare

在查询中,employeeId列的数据类型为uniqueidentifier。我在执行时遇到以下错误

从字符串转换为uniqueidentifier时,转换失败

如何将UniqueIdentifier GUID字符串转换为UniqueIdentifier列表

我已尝试执行如中所示的转换
但是它不起作用。

如果您这样声明变量:

declare @tt nvarchar
您将得到一个长度正好为一个字符的字符串

你应该永远没有例外!为varchar和nvarchar变量定义显式长度:

declare @tt nvarchar(2000) = .....
此外-如果将nvarchar定义为Unicode字符串,则必须在分配给它的字符串文字前面加上N,使其成为Unicode字符串文字:

declare @tt nvarchar(2000) = N'.......'
但最终,即使这样也行不通——因为in操作符需要一个列表,而不是一个字符串。。。。。您应该声明一个表变量,将值插入该表变量,然后执行以下操作:

DECLARE @tt TABLE (ID UNIQUEIDENTIFIER)

INSERT INTO @tt(ID)
VALUES ('e347d817-3121-e811-9c0b-58fb847a6047'), 
       ('e447d817-3121-e811-9c0b-58fb847a6047'),
       etc. 

SELECT * 
FROM [dbo].[employee] 
WHERE [employeeId] IN (SELECT ID FROM @tt)

您需要动态SQL来实现这一点:

--declare string, that will hold your query
declare @query varchar(4000) = 'select * 
                                from [dbo].[employee] 
                                where [employeeId] in '
--transform your list of IDs to list of values suitable for a where clause
set @tt = '(''' + replace(@tt, ',', ''',''') + ''')'
set @query = @query + @tt
--execute the query
exec(@query)

嗨,marc_s,即使将其转换为declare@tt nvarchar2000,它也只返回1个唯一标识符。其他值得到truncated@AbhishekPandey:阅读我的全部答案-您不能在in运算符中使用单个字符串-我确实展示了解决方案-试试看!我得到一个guid字符串作为参数,所以我不会将其转换为表。我想把它从一个字符串转换成表格,但这对我来说不起作用。在使用dbo.StrParse函数运行select语句时,仍然会出现相同的转换错误。
--declare string, that will hold your query
declare @query varchar(4000) = 'select * 
                                from [dbo].[employee] 
                                where [employeeId] in '
--transform your list of IDs to list of values suitable for a where clause
set @tt = '(''' + replace(@tt, ',', ''',''') + ''')'
set @query = @query + @tt
--execute the query
exec(@query)
--Create this function.  This function will convert string to Id as row
CREATE FUNCTION [dbo].[StrParse]
               (@delimiter CHAR(1),  
                @csv       NTEXT)  
RETURNS @tbl TABLE(Keys  NVARCHAR(255))  
AS  

  BEGIN  
    DECLARE  @len INT  
    SET @len = Datalength(@csv)  
    IF NOT @len > 0  
      RETURN  

    DECLARE  @l INT  
    DECLARE  @m INT  

    SET @l = 0  
    SET @m = 0  

    DECLARE  @s VARCHAR(255)  
    DECLARE  @slen INT  

    WHILE @l <= @len  
      BEGIN  

        SET @l = @m + 1--current position  
        SET @m = Charindex(@delimiter,Substring(@csv,@l + 1,255))--next delimiter or 0  

        IF @m <> 0  
          SET @m = @m + @l  
        --insert @tbl(keys) values(@m)  
        SELECT @slen = CASE   
                         WHEN @m = 0 THEN 255 --returns the remainder of the string  
                         ELSE @m - @l  
                       END --returns number of characters up to next delimiter  

        IF @slen > 0  
          BEGIN  
            SET @s = Substring(@csv,@l,@slen)  
            INSERT INTO @tbl  
                       (Keys)  
            SELECT @s  
          END  

        SELECT @l = CASE   
                      WHEN @m = 0 THEN @len + 1 --breaks the loop  
                      ELSE @m + 1  
                    END --sets current position to 1 after next delimiter  
      END  

    RETURN  
  END
go

--Below code will return the value you are expecting
declare @tt nvarchar (MAX) = 'e347d817-3121-e811-9c0b-58fb847a6047,e447d817-3121-e811-9c0b-58fb847a6047'
select * 
from [dbo].[employee]
where [employeeId] in (select keys from dbo.StrParse (',', @tt))