如何获取sql server 2008中的所有字段

时间:2013-01-25 06:49:36

标签: sql sql-server database sql-server-2008

我想从查询中选择的表中获取所有字段,即使字段不包含任何值(值为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>

但我想要在查询中选择的所有字段。如果你看到结果,它会给我带有值的字段,并省略没有值的字段。

怎么做,请帮忙。

最好的问候

2 个答案:

答案 0 :(得分: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

当我测试它时,这对我有用,但是如果您还遇到空值不生成属性的问题,您可以先将所有空值强制从原始数据表中的select返回为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

答案 1 :(得分:0)

我通常使用以下语法:

SELECT *
FROM   TABLE_NAME
WHERE  FIELD_NAME = "CRITERIA"