我必须根据存储在第二个表中的标志值显示和隐藏表的列

时间:2018-06-13 09:54:21

标签: sql sql-server entity-framework

First table contains flag(1 for show 0 for hide) for column that were present in second table. I have to show Columns of second table based on the flag stored in first table.

第一张表:

Id  columnName    isHidden  
1   Name           1           
2   Email          1           
3   Password       0           
4   Country        1           
5   State          1           
6   filename       1           
7   Salary         1           

第二张表:

Name    Password    Country   State     filename        Salary   Email
tom     tom           India  Haryana    6f0fbcec.jpg    9100.00  tom@mail.com
Mark    test          India  Haryana    b7f14d8a.png    8800.00  te@gmail.com
fdgd    gffdg         India   Delhi      d12f0438.txt   9100.00  abs@mail.com

3 个答案:

答案 0 :(得分:1)

您必须使用动态SQL:

declare @sqlcmd nvarchar(max)=
    'select '
    +STUFF( -- stuff is used to remove the first comma
     case when (select isHidden from your_first_table where Id=1)=0 then ',Name'        else '' end
    +case when (select isHidden from your_first_table where Id=2)=0 then ',Email'       else '' end
    +case when (select isHidden from your_first_table where Id=3)=0 then ',Password'    else '' end
    +case when (select isHidden from your_first_table where Id=4)=0 then ',Country'     else '' end
    +case when (select isHidden from your_first_table where Id=5)=0 then ',State'       else '' end
    +case when (select isHidden from your_first_table where Id=6)=0 then ',filename'    else '' end
    +case when (select isHidden from your_first_table where Id=7)=0 then ',Salary'      else '' end
    ,1,1,'')
    +' from your_second_table'

exec (@sqlcmd)

请注意,如果所有列都具有isHidden = 1

,则会抛出错误

答案 1 :(得分:0)

无需像George一样单独声明每个列,您可以使用FOR XML PATH构建它:

USE Sandbox;
GO

CREATE TABLE FirstTable (Id int, columnName sysname, isHidden bit);

INSERT INTO FirstTable
VALUES (1,N'Name',1),
       (2,N'Email',1),
       (3,N'Password',0),
       (4,N'Country',1),
       (5,N'State',1),
       (6,N'filename',1),
       (7,N'Salary',1);
GO

DECLARE @SQL nvarchar(MAX);

SET @SQL = N'SELECT ' + ISNULL(STUFF((SELECT N',' + NCHAR(10) + N'       ' + QUOTENAME(columnName)
                                  FROM FirstTable
                                  WHERE isHidden = 1 --0 (false) for the column "isHidden"? Seem counter intuitive. 
                                  FOR XML PATH(N'')),1,9,N''),N'*') + NCHAR(10) +
           N'FROM SecondTable;';
PRINT @SQL; --Your Best Friend
--EXEC sp_executesql @SQL; --Uncomment to execute
GO
DROP TABLE FirstTable;

但是,如果您正在使用SSRS,我实际上宁可使用隐藏列的表达式,而不是像这样构建动态SQL。特别是因为SSRS无法处理动态列。

答案 2 :(得分:0)

我认为这会有所帮助

DECLARE @SQLStatement NVARCHAR(MAX) = N''

DECLARE  @Columns_to_show NVARCHAR(MAX) = N''

SELECT @Columns_to_show = @Columns_to_show + ', QUOTENAME(columnName) 
FROM
    (SELECT columnName FROM [INPUT_TABLE1] WHERE isHidden = 0) DT

SET @SQLStatement = N'SELECT' +  @Columns_to_show + 'FROM [INPUT_TABLE2]'