0

我想从我在查询中选择的表中获取所有字段,即使字段不包含值(值 null 或空)。以下是我写的查询。

SELECT (
           SELECT T1.[CarrierCode_Destination] AS '@CarrierCode_Destination',
                  T1.[CarrierCode_Destination_Address] AS 
                  '@CarrierCode_Destination_Address',
                  T1.[CarrierCode_Destination_Address1] AS 
                  '@CarrierCode_Destination_Address1',
                  T1.[CarrierCode_Destination_Address2] AS 
                  '@CarrierCode_Destination_Address2',
                  T1.[CIMtrek_RegContact] AS '@CIMtrek_RegContact',
                  T1.[CIMtrek_CarrierContact] AS '@CIMtrek_CarrierContact',
                  [T1].[CIMtrek_AdditionalContacts] AS 
                  '@CIMtrek_AdditionalContacts'
           FROM   (
                      SELECT CD.[CIMtrek_DestinationName] 
                             CarrierCode_Destination,
                             CD.[CIMtrek_DestinationAdd] 
                             CarrierCode_Destination_Address,
                             CD.[CIMtrek_DestinationAdd_1] 
                             CarrierCode_Destination_Address1,
                             CD.[CIMtrek_DestinationAdd_2] 
                             CarrierCode_Destination_Address2,
                             CD.[CIMtrek_RegContact] CIMtrek_RegContact,
                             CD.[CIMtrek_CarrierContact] CIMtrek_CarrierContact,
                             CD.[CIMtrek_AdditionalContacts] 
                             CIMtrek_AdditionalContacts
                      FROM   CIMtrek_SystemTable_DatawareHouse CD
                      WHERE  LEN(
                                 LTRIM(
                                     RTRIM(ISNULL(LTRIM(RTRIM(CD.[CIMtrek_DestinationName])), ''))
                                 )
                             ) != 0
                  ) AS T1 
                  FOR XML PATH('Record'),
                  TYPE
       ) FOR XML PATH('CarrierCode_Destination'),
       TYPE

我得到的结果是

<CarrierCode_Destination>
  <Record CarrierCode_Destination="8S - San Fran" CarrierCode_Destination_Address="SAN FRANCISCO  -  Redistribution" CarrierCode_Destination_Address1="4025 Whipple Road, " CarrierCode_Destination_Address2="Union City CA  94587" CIMtrek_RegContact="RDC San Francisco" />
  <Record CarrierCode_Destination="8G - St Louis" CarrierCode_Destination_Address="ST. LOUIS  -  Redistribution" CarrierCode_Destination_Address1="126 Enterprise  Drive, " CarrierCode_Destination_Address2="Wentzville MO  63385" CIMtrek_RegContact="RDC St Louis" />
  <Record CarrierCode_Destination="V8     PHO/CYPR/CUST/TL  " />
</CarrierCode_Destination>

但我想要查询中选择的所有字段。如果您看到结果,它会为我提供有价值的字段并省略没有值的字段。

如何做到这一点,请帮助。

最好的祝福

4

2 回答 2

1

如果您isnull()对正在选择的每一列使用语句并将空值转换为空字符串,则它应该强制为所有列生成属性。您可能还需要也可能不需要添加rtrim()如下所示的语句,具体取决于您是否具有将返回固定大小字符串的数据类型:

SELECT (
       SELECT rtrim(isnull(T1.[CarrierCode_Destination],'')) AS '@CarrierCode_Destination',
              rtrim(isnull(T1.[CarrierCode_Destination_Address],'')) AS 
              '@CarrierCode_Destination_Address',
              rtrim(IsNull(T1.[CarrierCode_Destination_Address1],'')) AS 
              '@CarrierCode_Destination_Address1',
              rtrim(IsNull(T1.[CarrierCode_Destination_Address2],'')) AS 
              '@CarrierCode_Destination_Address2',
              rtrim(IsNull(T1.[CIMtrek_RegContact],'')) AS '@CIMtrek_RegContact',
              rtrim(IsNull(T1.[CIMtrek_CarrierContact],'')) AS '@CIMtrek_CarrierContact',
              rtrim(IsNull([T1].[CIMtrek_AdditionalContacts],'')) AS 
              '@CIMtrek_AdditionalContacts'
       FROM   (
                  SELECT CD.[CIMtrek_DestinationName] 
                         CarrierCode_Destination,
                         CD.[CIMtrek_DestinationAdd] 
                         CarrierCode_Destination_Address,
                         CD.[CIMtrek_DestinationAdd_1] 
                         CarrierCode_Destination_Address1,
                         CD.[CIMtrek_DestinationAdd_2] 
                         CarrierCode_Destination_Address2,
                         CD.[CIMtrek_RegContact] CIMtrek_RegContact,
                         CD.[CIMtrek_CarrierContact] CIMtrek_CarrierContact,
                         CD.[CIMtrek_AdditionalContacts] 
                         CIMtrek_AdditionalContacts
                  FROM   CIMtrek_SystemTable_DatawareHouse CD
                  WHERE  LEN(
                             LTRIM(
                                 RTRIM(ISNULL(LTRIM(RTRIM(CD.[CIMtrek_DestinationName])), ''))
                             )
                         ) != 0
              ) AS T1 
              FOR XML PATH('Record'),
              TYPE
   ) FOR XML PATH('CarrierCode_Destination'),
   TYPE

当我对其进行测试时,这对我有用,但如果您还遇到空值不生成属性的问题,您可以首先强制所有空值NULL从选择中返回,使用针对您的原始数据表NULLIF()

SELECT (
       SELECT rtrim(isnull(T1.[CarrierCode_Destination],'')) AS '@CarrierCode_Destination',
              rtrim(isnull(T1.[CarrierCode_Destination_Address],'')) AS 
              '@CarrierCode_Destination_Address',
              rtrim(IsNull(T1.[CarrierCode_Destination_Address1],'')) AS 
              '@CarrierCode_Destination_Address1',
              rtrim(IsNull(T1.[CarrierCode_Destination_Address2],'')) AS 
              '@CarrierCode_Destination_Address2',
              rtrim(IsNull(T1.[CIMtrek_RegContact],'')) AS '@CIMtrek_RegContact',
              rtrim(IsNull(T1.[CIMtrek_CarrierContact],'')) AS '@CIMtrek_CarrierContact',
              rtrim(IsNull([T1].[CIMtrek_AdditionalContacts],'')) AS 
              '@CIMtrek_AdditionalContacts'
       FROM   (
                  SELECT NullIF(CD.[CIMtrek_DestinationName],'') 
                         CarrierCode_Destination,
                         NullIF(CD.[CIMtrek_DestinationAdd],'') 
                         CarrierCode_Destination_Address,
                         NullIF(CD.[CIMtrek_DestinationAdd_1],'') 
                         CarrierCode_Destination_Address1,
                         NullIF(CD.[CIMtrek_DestinationAdd_2],'') 
                         CarrierCode_Destination_Address2,
                         NullIF(CD.[CIMtrek_RegContact],'') CIMtrek_RegContact,
                         NullIF(CD.[CIMtrek_CarrierContact],'') CIMtrek_CarrierContact,
                         NullIF(CD.[CIMtrek_AdditionalContacts],'') 
                         CIMtrek_AdditionalContacts
                  FROM   CIMtrek_SystemTable_DatawareHouse CD
                  WHERE  LEN(
                             LTRIM(
                                 RTRIM(ISNULL(LTRIM(RTRIM(CD.[CIMtrek_DestinationName])), ''))
                             )
                         ) != 0
              ) AS T1 
              FOR XML PATH('Record'),
              TYPE
   ) FOR XML PATH('CarrierCode_Destination'),
   TYPE
于 2013-01-25T08:18:05.667 回答
0

我通常使用以下语法:

SELECT *
FROM   TABLE_NAME
WHERE  FIELD_NAME = "CRITERIA"
于 2013-01-25T06:52:10.720 回答