0

我需要从系统中删除用户,搜索基于他们的电子邮件。然而,由于一些奇怪的验证,一些用户在电子邮件中使用单引号和通配符 (%)。

当我尝试删除用户时,由于单引号和其他奇怪的字符,它会抛出一个错误。我可以直接在数据库中清理,但是,我想看看我是否可以在他们的电子邮件中搜索所有垃圾,并且仍然可以直接从应用程序中删除用户

例如:

DELETE FROM Customers
WHERE Email = 'test@yahoo.com'@countag%and1=1''
4

4 回答 4

1

可以使用通配符,因为您不匹配模式,但是对于单引号,您需要加倍以对其进行转义。

DELETE FROM Customers
WHERE Email = 'test@yahoo.com''@countag%and1=1'''
                      --     ^ this           ^ and this.
于 2013-11-07T01:15:49.337 回答
1

最简单的解决方法是使用双引号,因此不必担心嵌入的单引号:

DELETE FROM Customers
WHERE Email = "test@yahoo.com'@countag%and1=1"
于 2013-11-07T01:18:54.423 回答
0
DROP TABLE UsersToDelete

CREATE TABLE UsersToDelete
(
    id        INT IDENTITY
   ,email     VARCHAR(500)
)


INSERT INTO UsersToDelete
  (
    -- id -- this column value is auto-generated
    email
  )
SELECT 'test@yahoo.com''@countag%and1=1''' AS email
UNION
SELECT 'bill.gates@microsoft.com'
UNION
SELECT 'user2675939@stackoverflow.com'
UNION
SELECT 'iamkarlson''''''@google.com'

SELECT utd.id
      ,utd.email
      ,CASE 
            WHEN (SUBSTRING(email ,CHARINDEX('''' ,email ,0) +1 ,1) <>'''' 
            OR SUBSTRING(REVERSE(email) ,CHARINDEX('''' ,REVERSE(email) ,0) +1 ,1) <>'''')
                 AND email LIKE '%''%'
                THEN 1
            WHEN SUBSTRING(email ,CHARINDEX('''' ,email ,0) +1 ,1) =''''
                 AND SUBSTRING(REVERSE(email) ,CHARINDEX('''' ,REVERSE(email) ,0) +1 ,1) =''''
                 AND email LIKE '%''%' 
                THEN 0
            ELSE 0
       END            AS [Contains]
      ,SUBSTRING(email ,CHARINDEX('''' ,email ,0) +1 ,1)
      ,SUBSTRING(REVERSE(email) ,CHARINDEX('''' ,REVERSE(email) ,0) +1 ,1)
FROM   UsersToDelete     utd
于 2013-11-07T09:27:25.687 回答
0

你可以试试

delete from customers where charindex('''', email) > 0

删除电子邮件地址至少包含一个单引号的所有客户......或者先进行选择并检查结果

于 2013-11-07T06:29:02.883 回答