SQL:使用 IN 子句将字符串转换为 IDS

发布于 2024-08-31 13:07:33 字数 272 浏览 7 评论 0原文

DECLARE @STR_IDS VARCHAR(15)
SET @STR_IDS='7,15,18'
UPDATE TBL_USERS WHERE ID IN  @STR_IDS

我知道更新语句不起作用,因为 ID 是 INT 类型,并且我正在替换那里的 varachar 值。我如何更改查询,以便它实际上像这样执行?

 UPDATE TBL_USERS WHERE ID IN (7,15,18)

提前致谢

DECLARE @STR_IDS VARCHAR(15)
SET @STR_IDS='7,15,18'
UPDATE TBL_USERS WHERE ID IN  @STR_IDS

I know the update statement would not work as the ID is of type INT and i am replacing a varachar value there .How can i change the query so that it will be executed like this in effect ?

 UPDATE TBL_USERS WHERE ID IN (7,15,18)

Thanks in advace

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一梦浮鱼 2024-09-07 13:07:33

Op 没有提到数据库,所以我只使用 SQL Server,因为问题中的示例 SQL 看起来像 TSQL。 SQL Server 中有多种分割字符串的方法。本文涵盖了几乎每种方法的优点和缺点:

"数组和列表SQL Server 2005 及更高版本,当表值参数无法分割时”作者:Erland Sommarskog

您需要创建一个拆分函数。这是分割函数的使用方式:

SELECT
    *
    FROM YourTable                               y
    INNER JOIN dbo.yourSplitFunction(@Parameter) s ON y.ID=s.Value

我更喜欢数字表方法在 TSQL 中分割字符串,但在 SQL Server 中分割字符串的方法有很多种,请参阅前面的链接,其中解释了每种方法的优点和缺点。

要使 Numbers Table 方法发挥作用,您需要执行此一次时间表设置,这将创建一个包含 1 到 10,000 行的 Numbers 表:

SELECT TOP 10000 IDENTITY(int,1,1) AS Number
    INTO Numbers
    FROM sys.objects s1
    CROSS JOIN sys.objects s2
ALTER TABLE Numbers ADD CONSTRAINT PK_Numbers PRIMARY KEY CLUSTERED (Number)

设置 Numbers 表后,创建此拆分函数:

CREATE FUNCTION [dbo].[FN_ListToTable]
(
     @SplitOn  char(1)      --REQUIRED, the character to split the @List string on
    ,@List     varchar(8000)--REQUIRED, the list to split apart
)
RETURNS TABLE
AS
RETURN 
(

    ----------------
    --SINGLE QUERY-- --this will not return empty rows
    ----------------
    SELECT
        ListValue
        FROM (SELECT
                  LTRIM(RTRIM(SUBSTRING(List2, number+1, CHARINDEX(@SplitOn, List2, number+1)-number - 1))) AS ListValue
                  FROM (
                           SELECT @SplitOn + @List + @SplitOn AS List2
                       ) AS dt
                      INNER JOIN Numbers n ON n.Number < LEN(dt.List2)
                  WHERE SUBSTRING(List2, number, 1) = @SplitOn
             ) dt2
        WHERE ListValue IS NOT NULL AND ListValue!=''

);
GO 

您现在可以轻松地将 CSV 字符串拆分为表并连接它或根据需要使用它,甚至可以在动态 sql 中。以下是如何根据您的问题使用它:

UPDATE t
    SET Col1=...
    FROM dbo.FN_ListToTable(',','7,15,18') dt
        INNER JOIN TBL_USERS                t ON  CAST(dt.value AS INT)=t.id

Op doesn't mention database, so I'll just use SQL Server, because the example SQL in the question looks like TSQL. There are many ways to split string in SQL Server. This article covers the PROs and CONs of just about every method:

"Arrays and Lists in SQL Server 2005 and Beyond, When Table Value Parameters Do Not Cut it" by Erland Sommarskog

You need to create a split function. This is how a split function can be used:

SELECT
    *
    FROM YourTable                               y
    INNER JOIN dbo.yourSplitFunction(@Parameter) s ON y.ID=s.Value

I prefer the number table approach to split a string in TSQL but there are numerous ways to split strings in SQL Server, see the previous link, which explains the PROs and CONs of each.

For the Numbers Table method to work, you need to do this one time table setup, which will create a table Numbers that contains rows from 1 to 10,000:

SELECT TOP 10000 IDENTITY(int,1,1) AS Number
    INTO Numbers
    FROM sys.objects s1
    CROSS JOIN sys.objects s2
ALTER TABLE Numbers ADD CONSTRAINT PK_Numbers PRIMARY KEY CLUSTERED (Number)

Once the Numbers table is set up, create this split function:

CREATE FUNCTION [dbo].[FN_ListToTable]
(
     @SplitOn  char(1)      --REQUIRED, the character to split the @List string on
    ,@List     varchar(8000)--REQUIRED, the list to split apart
)
RETURNS TABLE
AS
RETURN 
(

    ----------------
    --SINGLE QUERY-- --this will not return empty rows
    ----------------
    SELECT
        ListValue
        FROM (SELECT
                  LTRIM(RTRIM(SUBSTRING(List2, number+1, CHARINDEX(@SplitOn, List2, number+1)-number - 1))) AS ListValue
                  FROM (
                           SELECT @SplitOn + @List + @SplitOn AS List2
                       ) AS dt
                      INNER JOIN Numbers n ON n.Number < LEN(dt.List2)
                  WHERE SUBSTRING(List2, number, 1) = @SplitOn
             ) dt2
        WHERE ListValue IS NOT NULL AND ListValue!=''

);
GO 

You can now easily split a CSV string into a table and join on it or use it however you need, even from within dynamic sql. Here is how to use it from your question:

UPDATE t
    SET Col1=...
    FROM dbo.FN_ListToTable(',','7,15,18') dt
        INNER JOIN TBL_USERS                t ON  CAST(dt.value AS INT)=t.id
感悟人生的甜 2024-09-07 13:07:33

最近我更喜欢使用用户定义的表类型来传递参数列表,但我曾经使用这个实用函数:

CREATE FUNCTION [dbo].[fn_CommasToIntTable]
(
    @CommaList varchar(8000)
)
RETURNS  @ParsedList TABLE
(
    TokenID int
)
AS
BEGIN
    DECLARE @CurrentToken varchar(10)
    DECLARE @Pos int
    SET @CommaList = LTRIM(RTRIM(@CommaList))+ ','
    SET @Pos = CHARINDEX(',', @CommaList, 1)
    IF REPLACE(@CommaList, ',', '') <> ''
    BEGIN
        WHILE @Pos > 0
        BEGIN
            SET @CurrentToken = LTRIM(RTRIM(LEFT(@CommaList, @Pos - 1)))
            IF @CurrentToken <> ''
            BEGIN
                INSERT @ParsedList (TokenID) 
                VALUES (CAST(@CurrentToken AS int))
            END
            SET @CommaList = RIGHT(@CommaList, LEN(@CommaList) - @Pos)
            SET @Pos = CHARINDEX(',', @CommaList, 1)
        END
    END

    RETURN
END

你可以像这样使用它:

SELECT (Columns)
FROM (Table)
WHERE ID IN (SELECT TokenID FROM dbo.fn_CommasToIntTable(@idList))

Lately I prefer to use User-Defined Table Types to pass lists of parameters, but I used to use this utility function:

CREATE FUNCTION [dbo].[fn_CommasToIntTable]
(
    @CommaList varchar(8000)
)
RETURNS  @ParsedList TABLE
(
    TokenID int
)
AS
BEGIN
    DECLARE @CurrentToken varchar(10)
    DECLARE @Pos int
    SET @CommaList = LTRIM(RTRIM(@CommaList))+ ','
    SET @Pos = CHARINDEX(',', @CommaList, 1)
    IF REPLACE(@CommaList, ',', '') <> ''
    BEGIN
        WHILE @Pos > 0
        BEGIN
            SET @CurrentToken = LTRIM(RTRIM(LEFT(@CommaList, @Pos - 1)))
            IF @CurrentToken <> ''
            BEGIN
                INSERT @ParsedList (TokenID) 
                VALUES (CAST(@CurrentToken AS int))
            END
            SET @CommaList = RIGHT(@CommaList, LEN(@CommaList) - @Pos)
            SET @Pos = CHARINDEX(',', @CommaList, 1)
        END
    END

    RETURN
END

You'd use it like this:

SELECT (Columns)
FROM (Table)
WHERE ID IN (SELECT TokenID FROM dbo.fn_CommasToIntTable(@idList))
赴月观长安 2024-09-07 13:07:33

盗自此处

CREATE  FUNCTION fn_Split(@text varchar(8000), @delimiter varchar(20) = ' ')
RETURNS @Strings TABLE
(   
  position int IDENTITY PRIMARY KEY,
  value varchar(8000)  
)
AS
BEGIN
DECLARE @index int
SET @index = -1
WHILE (LEN(@text) > 0)
  BEGIN 
    SET @index = CHARINDEX(@delimiter , @text) 
    IF (@index = 0) AND (LEN(@text) > 0) 
      BEGIN  
        INSERT INTO @Strings VALUES (@text)
          BREAK 
      END 
    IF (@index > 1) 
      BEGIN  
        INSERT INTO @Strings VALUES (LEFT(@text, @index - 1))  
        SET @text = RIGHT(@text, (LEN(@text) - @index)) 
      END 
    ELSE
      SET @text = RIGHT(@text, (LEN(@text) - @index))
    END
  RETURN
END

正确做法:

UPDATE  tbl_users
SET     ...
FROM    fn_split('7,15,18', ',') q
JOIN    tbl_users
ON      id = CAST(q.value AS INT)

Stolen from here:

CREATE  FUNCTION fn_Split(@text varchar(8000), @delimiter varchar(20) = ' ')
RETURNS @Strings TABLE
(   
  position int IDENTITY PRIMARY KEY,
  value varchar(8000)  
)
AS
BEGIN
DECLARE @index int
SET @index = -1
WHILE (LEN(@text) > 0)
  BEGIN 
    SET @index = CHARINDEX(@delimiter , @text) 
    IF (@index = 0) AND (LEN(@text) > 0) 
      BEGIN  
        INSERT INTO @Strings VALUES (@text)
          BREAK 
      END 
    IF (@index > 1) 
      BEGIN  
        INSERT INTO @Strings VALUES (LEFT(@text, @index - 1))  
        SET @text = RIGHT(@text, (LEN(@text) - @index)) 
      END 
    ELSE
      SET @text = RIGHT(@text, (LEN(@text) - @index))
    END
  RETURN
END

The just do:

UPDATE  tbl_users
SET     ...
FROM    fn_split('7,15,18', ',') q
JOIN    tbl_users
ON      id = CAST(q.value AS INT)
梦太阳 2024-09-07 13:07:33

对于 mysql 来说真的很简单
只需使用FIND_IN_SET函数

UPDATE TBL_USERS WHERE FIND_IN_SET ( id , '7,15,18')

for mysql its really easy
just use FIND_IN_SET function

UPDATE TBL_USERS WHERE FIND_IN_SET ( id , '7,15,18')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文