使用Dynamic SQL遍历数据库中的多个Float列

时间:2019-01-15 16:15:35

标签: sql sql-server sql-server-2008-r2

如何遍历数据库中不同表的多个列?我通过一次查询一列来找到我需要的结果,这需要很多时间。

SELECT MAX
       (CASE Charindex('.', COLUMN1)
                    WHEN 0 THEN 0
                    ELSE
   Len (Cast(Cast(Reverse(CONVERT(VARCHAR(50), COLUMN1, 128)
                     ) AS FLOAT) AS BIGINT))
                  END) AS MAX_LENGTH
FROM   DB1.dbo.TABLE1

我的代码在这里,但是因为我发送多个值而无法使用

DECLARE @cols NVARCHAR(MAX)
    ,@sql NVARCHAR(MAX)

SET @cols = (SELECT quotename(COLUMN_NAME) FROM BRSDATA.INFORMATION_SCHEMA.COLUMNS A
inner join BRSDATA.INFORMATION_SCHEMA.TABLES B
on A.TABLE_NAME = B.TABLE_NAME
where TABLE_TYPE = 'BASE TABLE'
and DATA_TYPE = 'Float')

Set @sql =  'SELECT MAX(CASE Charindex(''.'',' + @cols + ')
                    WHEN 0 THEN 0
                    ELSE
   Len (Cast(Cast(Reverse(CONVERT(VARCHAR(50), ' + @cols + ', 128)
                     ) AS FLOAT) AS BIGINT))
                  END) AS MAX_LENGTH'

                  print(@sql)

Msg 512, Level 16, State 1, Line 101
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

我也尝试使用游标,并且“查询已成功执行”,但即使不关闭并取消分配游标,我也看不到结果。

DECLARE @SchemaName SYSNAME = 'dbo'
DECLARE @TableName SYSNAME 
DECLARE @ColumnName SYSNAME


DECLARE FLOAT_COLUMNS CURSOR FOR
SELECT t.name,c.name 
  FROM BRSDATA.sys.tables AS t
  INNER JOIN BRSDATA.sys.schemas AS s
  ON t.[schema_id] = s.[schema_id]
  inner join BRSDATA.sys.columns AS c
  on c.object_id = t.object_id
  WHERE s.name = 'dbo'
  and type_desc = 'USER_TABLE';

OPEN FLOAT_COLUMNS

FETCH NEXT FROM FLOAT_COLUMNS
INTO @TableName, @ColumnName

    WHILE @@FETCH_STATUS = 0
    BEGIN

       DECLARE @Columns NVARCHAR(MAX)
       SET @Columns = 
       STUFF(
          (SELECT
             ',' + QUOTENAME(name)
          FROM
             sys.columns
          WHERE
             object_id = OBJECT_ID(QUOTENAME(@TableName) + '.' + QUOTENAME(@ColumnName))
          FOR XML PATH(''))
          ,1,1,'')

        DECLARE @SQL AS NVARCHAR(MAX)
        SET @SQL = 'SELECT' + QUOTENAME(@TableName) + ',' + QUOTENAME(@ColumnName) +  ', MAX(CASE Charindex(''.'',' + @Columns + ')
                    WHEN 0 THEN 0
                    ELSE
   Len (Cast(Cast(Reverse(CONVERT(VARCHAR(50), ' + @Columns + ', 128)
                     ) AS FLOAT) AS BIGINT))
                  END) AS MAX_LENGTH'

        --use print to view and copy your dynamic sql string to see if you have formed it correctly
        PRINT @SQL
        --EXECUTE (@SQL)

        FETCH NEXT FROM FLOAT_COLUMNS
        INTO @TableName, @ColumnName
    END

CLOSE FLOAT_COLUMNS
DEALLOCATE FLOAT_COLUMNS

3 个答案:

答案 0 :(得分:1)

不确定这是否是您的需要,但这是我的处理方式:

   DECLARE @cols NVARCHAR(MAX)
    ,@table NVARCHAR(MAX)
    ,@sql NVARCHAR(MAX)
    ,@X INT = 1


WHILE @X < (
    SELECT MAX(ROWID) FROM 
    (
        SELECT quotename(COLUMN_NAME) col
        ,ROW_NUMBER()over(ORDER BY COLUMN_NAME) ROWID
        FROM INFORMATION_SCHEMA.COLUMNS A
        inner join INFORMATION_SCHEMA.TABLES B
        on A.TABLE_NAME = B.TABLE_NAME
        where TABLE_TYPE = 'BASE TABLE'
        and DATA_TYPE = 'Float'
    ) AS X
  WHERE ROWID < 10
)
 BEGIN

SET @cols = 
(SELECT col FROM 
(
    SELECT quotename(COLUMN_NAME) col
    ,ROW_NUMBER()over(ORDER BY COLUMN_NAME) ROWID
    FROM INFORMATION_SCHEMA.COLUMNS A
    inner join INFORMATION_SCHEMA.TABLES B
    on A.TABLE_NAME = B.TABLE_NAME
    where TABLE_TYPE = 'BASE TABLE'
    and DATA_TYPE = 'Float'
) AS X
WHERE ROWID = @X 
)

SET @table = 
(SELECT tablename FROM 
(
    SELECT quotename(A.TABLE_NAME) tablename
    ,ROW_NUMBER()over(ORDER BY COLUMN_NAME) ROWID
    FROM INFORMATION_SCHEMA.COLUMNS A
    inner join INFORMATION_SCHEMA.TABLES B
    on A.TABLE_NAME = B.TABLE_NAME
    where TABLE_TYPE = 'BASE TABLE'
    and DATA_TYPE = 'Float'
) AS X
WHERE ROWID = @X 
)


Set @sql =  'SELECT MAX(CASE Charindex(''.'',' + @cols + ')
                    WHEN 0 THEN 0
                    ELSE
        Len (Cast(Cast(Reverse(CONVERT(VARCHAR(50), ' + @cols + ', 128)
                     ) AS FLOAT) AS BIGINT))
                  END) AS MAX_LENGTH
        ,MAX(CASE Charindex(''.'',' + @table + ')
                    WHEN 0 THEN 0
                    ELSE
        Len (Cast(Cast(Reverse(CONVERT(VARCHAR(50), ' + @table + ', 128)
                     ) AS FLOAT) AS BIGINT))
                  END) AS MAX_LENGTH'

PRINT(@sql)
SET @X = @X +1
END

答案 1 :(得分:1)

我相信该脚本可以为您提供帮助:

DECLARE @Query TABLE (SchemaName    VARCHAR(100)
                    ,TableName  VARCHAR(100)
                    ,ColumnName VARCHAR(1000)
                    )
INSERT INTO @Query
SELECT A.TABLE_SCHEMA,A.TABLE_NAME,' MAX(CASE Charindex(''.'',' + QUOTENAME(COLUMN_NAME) + ') 
                                        WHEN 0 THEN 0 
                                        ELSE Len (Cast(Cast(Reverse(CONVERT(VARCHAR(50), ' + QUOTENAME(COLUMN_NAME) + ', 128) ) AS FLOAT) AS BIGINT)) 
                                        END) AS ['+COLUMN_NAME+'_MAX_LENGTH]'
FROM INFORMATION_SCHEMA.COLUMNS A
INNER JOIN INFORMATION_SCHEMA.TABLES B
ON A.TABLE_NAME = B.TABLE_NAME
WHERE TABLE_TYPE = 'BASE TABLE'
AND DATA_TYPE = 'FLOAT'

SELECT 'SELECT '+
        STUFF((
            SELECT ',' + ColumnName
            FROM @Query Q2
            WHERE Q1.SchemaName=q2.SchemaName AND  Q1.TableName=q2.TableName
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)')
            , 1, 2, '')
            +' FROM '+Q1.SchemaName+'.'+Q1.TableName
FROM 
(SELECT DISTINCT SchemaName,TableName FROM @Query) Q1

答案 2 :(得分:0)

use BRSDATA;

begin try drop table #temp end try begin catch end catch ;

DECLARE @TableName nvarchar(250); 
DECLARE @ColumnName nvarchar(250); 

declare @x int;

DECLARE @FLOATV as float = 3.141592653;--searching for this in all float vals in all tables

SELECT t.name,c.name colname 
    into #temp
  FROM sys.tables AS t
  INNER JOIN sys.schemas AS s
  ON t.[schema_id] = s.[schema_id]
  inner join sys.columns AS c
  on c.object_id = t.object_id
  WHERE s.name = 'dbo'
  and type_desc = 'USER_TABLE'
  and exists(select * from  sys.types where name = 'float' and system_type_id = c.system_type_id);

  SET Nocount on;

  DECLARE FLOAT_TAB CURSOR FOR
    SELECT DISTINCT [name] from #temp;
        OPEN FLOAT_TAB;
        FETCH NEXT FROM FLOAT_TAB
        INTO @TableName;

        WHILE @@FETCH_STATUS = 0
        BEGIN
            DECLARE @SQL nvarchar(max) = '';
            DECLARE @COND nvarchar(max) = '';
            SET  @SQL = 'SELECT ';

             DECLARE COL_TAB CURSOR FOR
                SELECT colname from #temp where name = @TableName;
                    OPEN COL_TAB;
                    FETCH NEXT FROM COL_TAB
                        INTO @ColumnName;

                WHILE @@FETCH_STATUS = 0
                BEGIN

                    SET @SQL = @SQL + ' ' + @ColumnName + ',';
                    SET @COND = @COND + @ColumnName + ' = ' + cast(@floatv as nvarchar(60)) + ' OR ' ;
                    FETCH NEXT FROM col_tab INTO @ColumnName

                END

                SET @SQL = LEFT(@SQL, len(@sql) - 1) + ' FROM ' + @TableName +  ' WHERE ' + LEFT(@COND, LEN(@COND) - 3);
                print @sql;
                EXECUTE ( @SQL);
                close col_tab;
                deallocate col_tab;
                --waitfor  delay '00:05';
            FETCH NEXT FROM FLOAT_TAB into @tablename;
        END
        close float_tab
        deallocate float_tab;