查询以仅提取与特定where条件相关或具有特定where条件数据的列

t5zmwmid  于 2021-07-29  发布在  Java
关注(0)|答案(2)|浏览(307)

我正在尝试编写一个sql查询(最好不是存储过程)来提取特定的列,这些列包含与where条件相关的数据。
如图所示,如果我有 where 条件 Report column = 'ABC' ,则查询应仅返回列 COL1 & COL4 ,因为他们只有报表的数据。同样,如果 Report='PQR' 然后是列 COL2 & COL6 .
在真实场景中,我有大约90个列。最好需要一个子查询或简单的sql语句,而不是存储过程,因为我必须从vb应用程序调用它。
这是一张测试表

create table report_test (
        report varchar(100),
        col1 int,
        col2 varchar(100),
        col3 int,
        col4 varchar(100),
        col5 varchar(100),
        col6 varchar(100)
    );
    insert into report_test (report, col1, col4) values ('abc', 1, '1');
    insert into report_test (report, col2, col6) values ('pqr', '1', '1');
    insert into report_test (report, col3) values ('xyz', 1);
    insert into report_test (report, col1, col4) values ('abc', 1, '1');
    insert into report_test (report, col2, col6) values ('pqr', '1', '1');
    insert into report_test (report, col3) values ('xyz', 1);

提前谢谢。

5rgfhyps

5rgfhyps1#

这是一个存储过程,可以得到你想要的。

表格示例

create table report_test (
    report varchar(100),
    col1 varchar(100),
    col2 varchar(100),
    col3 varchar(100),
    col4 varchar(100),
    col5 varchar(100),
    col6 varchar(100)
);
insert into report_test (report, col1, col4) values ('abc', '1', '1');
insert into report_test (report, col2, col6) values ('pqr', '1', '1');
insert into report_test (report, col3) values ('xyz', '1');

存储过程

注解是内联的。基本思想是:
询问元数据表信息\u schema报告表的所有列
循环遍历每列
询问报表表给定报表的该列是否为空
如果不为null,则将其添加为要从中选择的列
使用报表和选定列(不为null)执行最终查询
现在,程序。

create procedure getReportInfo
    @report varchar(100)
as
begin

    -- holds name of the column as each column is checked
    declare @col nvarchar(100);

    -- holds 1 or 0 - 1 means column was not null for that report
    declare @cnt int;

    -- this is the SQL that asks DB whether a given column is not null in the database
    declare @colSQL nvarchar(max);

    -- holds parameter definition for dynamic queries
    declare @parameter_definition nvarchar(1000);

    -- this is the final SQL that will be executed
    declare @s nvarchar(1000);
    set @s = 'select report';

    declare c cursor read_only for
        select column_name from INFORMATION_SCHEMA.columns
        where table_name = 'report_test' and column_name <> 'report'
        order by ORDINAL_POSITION;

    open c;

    fetch next from c into @col;
    while @@FETCH_STATUS = 0
    begin

        -- ask DB whether column was not null in the table for a given report
        set @cnt = 0;
        set @colSQL = concat(
            'select @cnt_out = count(*) from report_test where report = @rep_temp and ',
            @col, ' is not null'
        );
        set @parameter_definition = N'@rep_temp nvarchar(100), @cnt_out int OUTPUT';
        execute sp_executesql @colSQL,
            @parameter_definition,
            @rep_temp = @report,
            @cnt_out = @cnt output;

        -- if column was not null, add it as a selectable field in the final query
        if @cnt > 0
        begin
            set @s = concat(@s, ', ', @col);
        end;

        fetch next from c into @col;
    end;

    close c;
    deallocate c;

    -- execute final query
    set @s = concat(@s, ' from report_test where report = @rep_temp');
    set @parameter_definition = N'@rep_temp nvarchar(100)';
    execute sp_executesql @s,
        @parameter_definition,
        @rep_temp = @report;

end
go

执行程序

execute getReportInfo 'pqr'
report  col2    col6
pqr       1      1

execute getReportInfo 'abc'
report  col1    col4
abc      1        1

execute getReportInfo 'xyz'
report  col3
xyz      1

这只是一个想法。根据报表表中的内容,必须对其进行调整。本例中有几个概念:
如何创建存储过程
如何向其发送参数
如何使用光标
如何使用动态查询
如何向动态查询发送参数
如何从动态查询中检索输出
如何执行存储过程

从vb调用存储过程

请参阅以下文章:
vb6版本:https://www.codeproject.com/articles/15222/how-to-use-stored-procedures-in-vb6
vb.net:sql server存储过程并在vb.net中执行

zqdjd7g9

zqdjd7g92#

您可以为每个筛选条件编写一个带有单独结果集的查询,并为它们应用union all,如下所示:

SELECT Col1 AS Colum1, col4 AS Column2
FROM TableName
WHERE Report = 'ABC'
UNION ALL
SELECT Col2 AS Colum1, col6 AS Column2
FROM TableName
WHERE Report = 'PQR'
.
.
.

相关问题