简体   繁体   English

Select 某些列仅在满足条件时

[英]Select certain column only if condition met

I want to have a select that will not have fixed number of columns.我想要一个没有固定列数的 select。 Column OptionalColumn should be selected only if variable @check = 1仅当变量@check = 1 时才应选择列OptionalColumn

This is what I had (syntax is bad but maybe it explains my problem)这就是我所拥有的(语法不好,但也许它解释了我的问题)

SELECT 
    Column1, Column2,
    (IF @Check = 1
       BEGIN OptionalColumn END)
FROM table

It is crucial to have it only if @Check = 1 and not having it if @Check = 0. Is it doable?只有在@Check = 1 时才拥有它,而在@Check = 0 时不拥有它是至关重要的。它可行吗?

You could do this with an IF ELSE block like this;你可以用这样的 IF ELSE 块来做到这一点;

CREATE TABLE #table (Column1 varchar(10), Column2 varchar(10))
INSERT INTO #table
VALUES
('column1', 'column2')

DECLARE @Check bit; SET @Check = 0

IF @Check = 1 
(SELECT Column1, Column2
FROM #table)

ELSE

(SELECT Column1
FROM #table)

Change the variable from 0 to 1 for testing to see the change in number of columns.将变量从 0 更改为 1 以进行测试以查看列数的变化。

Rich Benner gave you a good solution, as an alternative you can use dynamic sql like that : Rich Benner 为您提供了一个很好的解决方案,作为替代方案,您可以使用这样的动态 sql:

DECLARE  @sqlvar VARCHAR(100)
select @Sqlvar= ' SELECT Column1, Column2 '+IIF (@check=1,',OptionalColumn','')+' from TABLE';
EXECUTE sp_executesql @sqlvar 

For reference on sp_ executesql look: [https://msdn.microsoft.com/it-it/library/ms188001(v=sql.120).aspx]有关 sp_ executesql 的参考:[https://msdn.microsoft.com/it-it/library/ms188001(v=sql.120).aspx]

Can use IF ELSE block.可以使用 IF ELSE 块。 Please try this.请试试这个。

--Creating a Table.
CREATE TABLE #MyTable 
    (
    Column1 VARCHAR(10), 
    Column2 VARCHAR(10),
    OptionalColumn VARCHAR(10)
    )
--Inserting value to the Table.
INSERT INTO #MyTable
VALUES('Value 1', 'Value 1','Optional Value')

--IF ELSE Logic to desired output.
DECLARE @Check bit 
SET @Check = 0

IF @Check = 1 
    (
    SELECT 
        Column1, 
        Column2
    FROM 
        #MyTable
    )
ELSE
   (
    SELECT 
        Column1, 
        Column2,
        OptionalColumn
    FROM 
        #MyTable
    )

I find that it's pretty clean and readable to use temp tables for this purpose, and it doesn't require duplicating queries or big branches of logic.我发现为此目的使用临时表非常简洁易读,并且不需要重复查询或大的逻辑分支。

Create a temp table with all the columns you would ever need.创建一个包含您需要的所有列的临时表。 Use IF statements to drop the columns you don't want to display from the temp table.使用 IF 语句从临时表中删除您不想显示的列。 Then finally, select * from the temp table.最后,来自临时表的 select *。

This will also allow you use the data returned to make a determination on whether to keep the column or not.这也将允许您使用返回的数据来确定是否保留该列。

 IF OBJECT_ID('tempdb..#tempTable') IS NOT NULL DROP TABLE #tempTable;
 select '---' as [1],
 '---' as [2],
 '---' as [3],
 '---' as [4],
 '---' as [5],
 '---' as [6],
 '---' as [7],
 null as [8]
 into #tempTable 

if RAND() > .5
begin
    alter table #tempTable drop column [1]
end

if RAND() > .5
begin
    alter table #tempTable drop column [2]
end

if RAND() > .5
begin
    alter table #tempTable drop column [3]
end

--Query using result set to dertmine if column should be dropped
if (select count(*) from #tempTable where [8] is not null) = 0
begin
    alter table #tempTable drop column [8]
end

select * from  #tempTable

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM