SQL获取存储在不同表中的列的名称

时间:2018-12-11 10:57:49

标签: sql sql-server tsql alias

我有一个具有通用列名“ PARAM1”,“ PARAM2”,“ PARAM3”的Article表...

这些列的真实名称存储在另一个表中。

SELECT
A.PARAM1, -- This is the data
N1.NOMPAR, -- This is the column name
A.PARAM2 AS N2.NOMPAR -- This does not work, but the desired output
FROM dbo.ARTICULO AS A
LEFT JOIN dbo.NOMPARAMS AS N1 ON N1.NUMPAR = 1 AND N1.TIPPAR='ART'
LEFT JOIN dbo.NOMPARAMS AS N2 ON N2.NUMPAR = 2 AND N2.TIPPAR='ART';

我想使用真实的实时数据,而不是对Article表中的不同查询进行硬编码的别名。另外,有没有办法为每个paramX不做9个不同的左联接?数据库是一团糟,并在各处使用了这种设计。谢谢

编辑:

这是我目前的结论:

DECLARE @DynamicTSQLStatement NVARCHAR(MAX);
SET @DynamicTSQLStatement
= N'SELECT CODART, ' + STUFF(
                                (
                                    SELECT ',[PARAM' + RIGHT([NUMPAR] + 10, 1) + '] AS [' + [NOMPAR] + ']'
                                    FROM dbo.NOMPARAMS
                                    WHERE TIPPAR = 'ART'
                                          AND NOMPAR <> ''
                                    ORDER BY [NUMPAR]
                                    FOR XML PATH(''), TYPE
                                ).value('.', 'VARCHAR(MAX)'),
                                1,
                                1,
                                ''
                            ) + N'FROM dbo.ARTICULO';
EXEC sp_executesql @DynamicTSQLStatement;

仍在寻找如何向查询添加联接

SELECT C1.DESCCAR, -- This is the data
NP1.NOMPAR, -- This is the column name
C2.DESCCAR AS NP2.NOMPAR, -- This does not work, but the desired output
A.PARAM1, -- This is the data
N1.NOMPAR, -- This is the column name
A.PARAM2 AS N2.NOMPAR -- This does not work, but the desired output
FROM dbo.ARTICULO A
 LEFT JOIN dbo.CARACTERISTICAS AS C1 ON(C1.TIPCAR='A' AND C1.NUMCAR='1' AND C1.CODCAR=A.CAR1)
 LEFT JOIN dbo.CARACTERISTICAS AS C2 ON(C2.TIPCAR='A' AND C2.NUMCAR='2' AND C2.CODCAR=A.CAR2)
 LEFT JOIN dbo.NOMPARAMS AS NP1 ON(NP1.TIPPAR='ART' AND NP1.NUMPAR='1')
 LEFT JOIN dbo.NOMPARAMS AS N1 ON N1.NUMPAR=1 AND N1.TIPPAR='ART'
 LEFT JOIN dbo.NOMPARAMS AS N2 ON N2.NUMPAR=2 AND N2.TIPPAR='ART';

2 个答案:

答案 0 :(得分:1)

以下代码/逻辑可用于完成任务:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<form id="formId" method="get">
  <div class="container">
    <h4>Start Date:</h4>
    <input type="text" id="startdate" name="fromdate" width="276" placeholder="dd/mm/yyyy" required />
    <h4>End Date:</h4>
    <input type="text" id="enddate" name="todate" width="276" placeholder="dd/mm/yyyy" required />
    <h4>Outlets:</h4>
    <select name="outlet" id="myselect">
      <option>--------------------------------------</option>
      <option>ALL</option>
    </select>
    <div><br>
    </div>
    <div>
      <br>
      <button id="button" class="btn btn-default" type="submit">Search</button>
    </div>
  </div>
</form>

enter image description here

答案 1 :(得分:0)

您可以使用如下所示的一些动态SQL来做到这一点:

DECLARE @NOMPAR1 sysname, @NOMPAR2 sysname
SELECT
    @NOMPAR1 = CASE WHEN NUMPAR = 1 THEN NOMPAR ELSE @NOMPAR1 END,
    @NOMPAR2 = CASE WHEN NUMPAR = 2 THEN NOMPAR ELSE @NOMPAR2 END
FROM
    NOMPARAMS
WHERE TIPPAR = 'ART'


DECLARE @sql nvarchar(4000)
SET @sql = N'
SELECT 
    A.PARAM1 AS ' + QUOTENAME(@NOMPAR1) + N',
    A.PARAM2 AS ' + QUOTENAME(@NOMPAR2) + N'
FROM 
    ARTICULO A'
PRINT @sql

EXEC sp_executesql @sql

但是它是如此丑陋且对开发人员不友好,我希望对列别名进行硬编码。

更好的选择是依靠客户端应用程序来处理列名。