简体   繁体   English

使用 MS SQL 服务器搜索具有某些已知值范围的表和列名

[英]Search table and column names having some known range of values using MS SQL server

Environment: SQL Server 2016 (v13.00.4259), where I have read-only permissions (basically just SELECT from multiple tables).环境:SQL Server 2016 (v13.00.4259),我有只读权限(基本上只是来自多个表的SELECT )。

Use case: I want to find all table and column names from a database, where the column type is datetime and the column name has %END% in it, and any value (in the table.column) is found to be between some time frame (in this case one day).用例:我想从数据库中查找所有表名和列名,其中列类型为datetime时间,列名中包含%END% ,并且发现任何值(在 table.column 中)都在一段时间之间框架(在这种情况下是一天)。

I can find all table and column names from the database using this:我可以使用以下方法从数据库中找到所有表名和列名:

SELECT TABLE_NAME as tab, COLUMN_NAME as col
FROM INFORMATION_SCHEMA.COLUMNS
WHERE DATA_TYPE = 'datetime' and COLUMN_NAME like '%END%';

I guess I'd need a dynamic query, because table and column names should be static in static SQL query, so:我想我需要一个动态查询,因为表和列名应该是 static SQL 查询中的 static ,所以:

WITH enddates AS 
(
    SELECT TABLE_NAME AS tab, COLUMN_NAME AS col
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE DATA_TYPE = 'datetime' AND  COLUMN_NAME LIKE '%END%'
)
exec('select tab, col 
      from enddates
      where ' + tab + '.' + col + ' >= ''2019-10-21'' and ' 
        + tab + '.' + col + '< ''2019-10-22'' ')

  -- would want it to eval to:
  -- SELECT tab, col FROM enddates 
  -- WHERE $tab.$col >= '2019-10-21' AND $tab.$col < '2019-10-22'

Would want to get a list of table and column names where some %END% datetime is in 2019-10-21.想要获取一些 %END% 日期时间在 2019 年 10 月 21 日的表和列名列表。

But it does not work:但它不起作用:

"SQL Error [156] [S1000]: Incorrect syntax near the keyword 'exec'." “SQL 错误 [156] [S1000]:关键字 'exec' 附近的语法不正确。”

One reason could be, the 'enddates' alias for sub-query is not seen inside EXEC, but I assume that is not the only problem.一个原因可能是,在 EXEC 中看不到子查询的“enddates”别名,但我认为这不是唯一的问题。 For clarity I think it shows what I am after.为了清楚起见,我认为它显示了我所追求的。

I have worked more with PostgreSQL, but now I need to retrieve this kind of data from SQL Server with readonly access, so cannot make toy data tables there to test, nor views.我使用 PostgreSQL 进行了更多工作,但现在我需要从 SQL 服务器中以只读访问权限检索此类数据,因此无法在那里制作玩具数据表进行测试,也无法查看。 Would of course be simpler first to create a table of those interesting table.column pairs and avoid needing a sub-query.首先创建那些有趣的 table.column 对的表当然会更简单,并且避免需要子查询。 But yet I wouldn't know how to make dynamic queries from those table and column names then in SQL Server.但是我不知道如何在 SQL 服务器中从这些表和列名进行动态查询。

Edit: another more semantically correct "pseudo-code" idea, using previous WITH-sub-query:编辑:另一个在语义上更正确的“伪代码”想法,使用以前的 WITH-子查询:

SELECT tab, col 
FROM enddates 
WHERE EXEC('select ' + tab + '.' + col + ' >= ''2019-10-21'' and ' + tab + '.' + col + ' < ''2019-10-22''') = True

One possible approach is to generate and execute a dynamic statement:一种可能的方法是生成并执行动态语句:

-- Dynamic statement
DECLARE @stm nvarchar(max) = N''

SELECT @stm = CONCAT(
    @stm,
    CASE WHEN @stm = N'' THEN N'' ELSE N' UNION ALL ' END,
    N'SELECT ''',
    QUOTENAME(TABLE_NAME),
    N''' AS Tab, ''',
    QUOTENAME(COLUMN_NAME),
    N''' AS Col, COUNT(*) AS Cnt FROM ',
    QUOTENAME(TABLE_NAME),
    N' WHERE ',
    QUOTENAME(COLUMN_NAME),
    N' >= ''20191021'' AND ',
    QUOTENAME(COLUMN_NAME),
    N' < ''20191022'' ',
    N'HAVING COUNT(*) > 0'
)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE DATA_TYPE = 'datetime' and COLUMN_NAME LIKE '%END%'

-- Validate and execute
PRINT @stm
EXEC sp_executesql @stm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL Server - 如何仅更改具有某些特定值的表列? - SQL Server - How to change table column only having some specific values? 在SQL Server表中,如何在具有JSON值的列上基于JSON搜索过滤记录 - In a SQL Server table how do I filter records based on JSON search on a column having JSON values MS SQL Server 中是否有任何方法可以使用某种链接将表的一列映射到另一个数据库? - Is there any way in MS SQL Server to map one column of a table to another database using some sort of link? 如何使用PHP查询MS SQL Server而不列出列名? - How to query MS SQL Server using PHP and not list column names? SQL Server行到列枢纽,已知列名称未知行数据值 - Sql server Row to column pivot, known column names unknown row data values 选择“列”作为SQL SERVER中另一个表中值的列名 - Select Column as Column names of values in another table in SQL SERVER 使用动态SQL将表重新格式化为将列名称作为行 - Using Dynamic SQL to reformat a table to having column names as rows 使用组合条件的MS-SQL Server搜索表 - MS-SQL Server search table using assembled criteria 变量表名、列名和值的 SQL Server Update 语句 - SQL Server Update statement for variable table name, column names and values 在 SQL Server 中将工作表转换为列名作为值的表 - Transforming a sheet into a table with column names as values in SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM