使用SQL查找包含具有特殊字符的值的列

时间:2017-11-23 10:25:05

标签: sql sql-server special-characters sql-search

我在DB中有一些表,如下所述。

**Table1**
+-------+--------------+-------------+
|**ID** |**Name**      |**Country**  |
+-------+--------------+-------------+
|  1    |Avinash Kumar |India        |
|  2    |Sat!sh@S      |USA          |
|  3    |$32kjs        |UK           |
|  4    |#$@@@         |AFRICA       |
|  5    |Krishnas_has  |USA          |
+-------+--------------+-------------+

**Table2**
+-------+--------------+-------------+-----------+
|**ID** |**Name1**     |**Country1** |**Region1**|
+-------+--------------+-------------+-----------+
|  1    |Avinash Kumar |India        |EMEA       | 
|  2    |Sat!sh@S      |USA          |ASIA@@     |
|  3    |$32kjs        |UK           |EU._A      |
|  4    |#$@@@         |AFRICA       |HAS&#      |
|  5    |Krishnas_has  |USA          |KALc!!     |
+-------+--------------+-------------+-----------+

**Table3**
+-------+--------------+-------------+-----------+-----------+
|**ID** |**Name2**     |**Country2** |**Region2**|**State2** |
+-------+--------------+-------------+-----------+-----------+
|  1    |Avinash.Kumar |India$       |EMEA       |BANG_&     |
|  2    |Sat!sh@S      |US!!A        |ASIA@@     |SO$TH      |
|  3    |$32kjs        |U@K          |EU._A      |TRUTH      |
|  4    |#$@@@         |AFRICA       |HAS        |HAPPY      |
|  5    |Krishnas_has  |USA#         |KALc!!     |!ASDF#     |
+-------+--------------+-------------+-----------+-----------+

还有一件事就是我希望将表名作为变量传递。像这样的东西

Declare @Table Select * from @Table 我传递给@Table的代码必须动态地工作

现在,如果我通过@Table = Table3

,如何找到特殊字符列

例如:当我想在 Table3

中找到特殊字符时,我的输出应该是这样的
**EXPECTED OUTPUT**
+-------+--------------+-------------+-----------+-----------+
|**ID** |**Name2**     |**Country2** |**Region2**|**State2** |
+-------+--------------+-------------+-----------+-----------+
|  1    |Avinash.Kumar |India$       |           |BANG_&     |
|  2    |Sat!sh@S      |US!!A        |ASIA@@     |SO$TH      |
|  3    |$32kjs        |U@K          |EU._A      |           |
|  4    |#$@@@         |             |           |           |
|  5    |Krishnas_has  |USA#         |KALc!!     |!ASDF#     |
+-------+--------------+-------------+-----------+-----------+

有人可以帮帮我吗?

提前致谢!

2 个答案:

答案 0 :(得分:0)

看看这种方法:

CREATE TABLE Test (
  ID INT,
  Name2 NVARCHAR(100),
  Country2 NVARCHAR(100),
  Region2 NVARCHAR(100),
  State2 NVARCHAR(100)
)
GO

DELETE test

INSERT INTO Test VALUES(1, 'Avinash.Kumar', 'India$', 'EMEA', 'BANG_&'), (2, 'Sat!sh@S', 'US!!A', 'ASIA', 'SO$TH'), (3, 'Test1', 'Test2', 'Test3', 'Test4')
GO

CREATE FUNCTION chkStrg4Chars(@InputString NVARCHAR(4000)) RETURNS NVARCHAR(4000)
AS
BEGIN
  DECLARE @RetVal NVARCHAR(4000);
  SET @RetVal = CASE WHEN @InputString LIKE '%[^A-Za-z0-9 .]%' THEN @InputString ELSE NULL END;
  RETURN @RetVal
END;
GO

CREATE PROCEDURE chkTableColumns4Chars(@InputTable NVARCHAR(500))
AS
BEGIN
  DECLARE @ColList NVARCHAR(MAX);
  DECLARE @ColListNonC NVARCHAR(MAX);
  DECLARE @ColListRes NVARCHAR(MAX);
  DECLARE @ColListPiv NVARCHAR(MAX);

  SELECT @ColList = (SELECT CASE WHEN t.name IN ('nvarchar','varchar') THEN 'dbo.chkStrg4Chars(' + c.name + ') AS ' + c.name ELSE c.name END + ','
                       FROM sys.columns c
                       JOIN sys.types t ON c.system_type_id = t.system_type_id AND c.user_type_id = t.user_type_id
                       WHERE c.object_id = OBJECT_ID(@InputTable)
                       ORDER BY c.column_id
                       FOR XML PATH('')
                     );

  SET @ColList = SUBSTRING(@ColList, 1, LEN(@ColList)-1);

  SELECT @ColListRes = (SELECT 'MAX(' + c.name + ') AS ' + c.name + ','
                       FROM sys.columns c
                       JOIN sys.types t ON c.system_type_id = t.system_type_id AND c.user_type_id = t.user_type_id
                       WHERE c.object_id = OBJECT_ID(@InputTable)
                         AND t.name IN ('nvarchar','varchar')
                       ORDER BY c.column_id
                       FOR XML PATH('')
                     );

  SET @ColListRes = SUBSTRING(@ColListRes, 1, LEN(@ColListRes)-1);

  SELECT @ColListNonC = (SELECT c.name + ','
                       FROM sys.columns c
                       JOIN sys.types t ON c.system_type_id = t.system_type_id AND c.user_type_id = t.user_type_id
                       WHERE c.object_id = OBJECT_ID(@InputTable)
                         AND t.name NOT IN ('nvarchar','varchar')
                       ORDER BY c.column_id
                       FOR XML PATH('')
                     );

  SELECT @ColListPiv = (SELECT c.name + ','
                       FROM sys.columns c
                       JOIN sys.types t ON c.system_type_id = t.system_type_id AND c.user_type_id = t.user_type_id
                       WHERE c.object_id = OBJECT_ID(@InputTable)
                         AND t.name IN ('nvarchar','varchar')
                       ORDER BY c.column_id
                       FOR XML PATH('')
                     );

  SET @ColListPiv = SUBSTRING(@ColListPiv, 1, LEN(@ColListPiv)-1);

  DECLARE @stmt NVARCHAR(MAX) = 'SELECT ' + @ColList + ' INTO #TTest1 FROM ' + @InputTable + ';
DECLARE @SubCol nvarchar(max) = (SELECT ColName + ' + CHAR(39) + ',' + CHAR(39) + ' FROM (SELECT ' + @ColListRes + ' FROM #TTest1) x UNPIVOT (StrVal FOR ColName in (' + @ColListPiv + '))u FOR XML PATH('+CHAR(39)+CHAR(39)+'));
SET @SubCol = SUBSTRING(@SubCol, 1, len(@SubCol)-1)
DECLARE @SubStmt nvarchar(max) = ' + CHAR(39) + 'SELECT + ' + @ColListNonC + char(39) + ' + @SubCol + ' + CHAR(39) + ' FROM #TTest1 WHERE COALESCE( ' + CHAR(39) + ' + @SubCol + '+ CHAR(39) + ') IS NOT NULL' + CHAR(39) + ';
EXEC sp_executesql @SubStmt;'

EXEC sp_executesql @stmt;

END
GO


EXEC chkTableColumns4Chars 'dbo.Test'

答案 1 :(得分:0)

如你所说,如果你的表中有很多列,那么编写sql查询来检查每一列将是一项繁忙的工作。为了方便起见,我们可以通过执行动态sql查询来实现。

<强>查询

declare @sql as varchar(max);

select @sql = stuff((
        select ', case when [' + [column_name] + '] like ' 
        + char(39) + '%[^A-Za-z0-9 ]%' + char(39) 
        + ' then [' + [column_name] + '] else ' 
        + char(39) + char(39) + ' end as [' + [column_name] + '] '
        from information_schema.columns
        where table_name = 'your_table_name'
        and [column_name] <> 'ID'
        for xml path('')
    )
    , 1, 1, ''
);

select @sql = 'select [ID], ' + @sql + ' from [your_table_name];';

exec(@sql);