子字符串和字符索引的 SQL 语法错误 [英] SQL syntax error with substring and charindex

查看:41
本文介绍了子字符串和字符索引的 SQL 语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个脚本:

update edi_file_steps
  set
    user_id= table_B.id,
    message= SUBSTRING( message, 1, CHARINDEX('[',message)-1)
from edi_file_steps table_A INNER JOIN GU_User table_B
where message LIKE '%Downloaded%' 
  AND table_B.login = SUBSTRING( 
    message, 
    CHARINDEX('[', message) + 1, 
    len(message) - CHARINDEX('[',message)-1
  );

我在第 4 行收到 sql 语法错误:意外令牌,我的脚本有什么问题吗?sql

I am getting sql syntax error at line 4: unexpected token , Is there any thing wrong with my script ?sql

推荐答案

如果您使用的是 SQL Server,则您的 JOIN 格式不正确.和非常糟糕的表别名.也许这可以满足您的要求:

If you are using SQL Server, you have a malformed JOIN. And very poor table aliases. Perhaps this does what you want:

update fs
    set user_id = u.id,
        message = SUBSTRING(fs.message, 1, CHARINDEX('[', fs.message)-1)
    from edi_file_steps fs INNER JOIN
         GU_User u
         on u.login = SUBSTRING(fs.message, CHARINDEX('[', fs.message)+1, len(fs.message)- CHARINDEX('[',fs.message)-1 )
    where message LIKE '%Downloaded%' ;

在 MySQL 中,这将是:

In MySQL, this would be:

update edi_file_steps fs INNER JOIN
       GU_User u
       on fs.message like concat('%[', u.login, ']%')
    set user_id = table_B.id,
        message = substring_index(fs.message, '[', 1)
    where message LIKE '%Downloaded%' ;

这篇关于子字符串和字符索引的 SQL 语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆