I want to get all the fields from the table which i select in the query even if the fields contains no value(the value null or empty). the following is the query i have written.
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
the results i get is
<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>
but i want all the fields which are selected in the query. If you see the result it gives me the fields which are having value and omits the fields which don’t have values.
how to do this, Please help.
Best Regards
If you use
isnull()statements for each column you are selecting and convert null values to empty strings it should force the attributes to be generated for all columns. You may or may not also need to addrtrim()statements like I show below depending on if you have a datatype that is going to return a fixed size string:That worked for me when I tested it out but if you are also having issues with empty values not generating attributes you can first force all the empty values to be returned as
NULLfrom the select against your original data table usingNULLIF()