Hello ,
First of all
remove the no of columns in select statement as far as possible.
Consider It in no of parts..Then consider without union the first part.
for example
SELECT 'Settings' AS Section, ddsref.DeviceDefinitionID, ddsref.DeviceDefinitionSettingID,
ddsref.DeviceDefinitionCategoryID, dds.SettingName,dds.SettingDescription,
dds.IsInitialDeviceSetting, sdt.DefaultValue, sdt.MaximumLength, sdt.RegularExpression,
sdt.MinimumValue, sdt.MaximumValue,sdt.EnumValue,ddsv.DeviceDefinitionSettingValueID,
ddsv.HasBeenExportedOnce,ddsv.DeviceDefinitionSettingDefaultValue, dt.DataTypeName,
NULL AS DeviceCommunication, NULL AS UserAuthenticationType, NULL AS SoftwareVersion,
NULL AS DeviceType, NULL AS Model, NULL AS Manufacturer, NULL AS DeviceDefinitionCategoryType,
NULL AS DeviceDefinitionCategoryName, NULL AS DeviceDefinitionCategoryDescription,
NULL AS CreatedDate,dds.IsReadOnly,dds.SettingLabel,dds.DisplayOrder
FROM DeviceDefinitionSettingXref AS ddsref
INNER JOIN DeviceDefinitionSetting AS dds ON
dds.DeviceDefinitionSettingID = ddsref.DeviceDefinitionSettingID
INNER JOIN DeviceDefinitionSettingValues as ddsv ON
ddsv.DeviceDefinitionSettingXrefID=ddsref.DeviceDefinitionSettingXrefID AND
ddsv.DeviceDefinitionSettingId=dds.DeviceDefinitionSettingID AND
ddsv.DeviceInfoId=@deviceInfoId
INNER JOIN SettingDataType AS sdt ON
sdt.DeviceDefinitionSettingId = dds.DeviceDefinitionSettingID
INNER JOIN DataType AS dt ON dt.DataTypeID = sdt.DataTypeID
INNER JOIN DeviceDefinition AS dd ON
dd.DeviceDefinitionID = ddsref.DeviceDefinitionID
INNER JOIN DeviceInfo AS di ON
di.di_DeviceType = dd.DeviceType AND
di.di_Model = dd.Model AND di.di_Manufacturer = dd.Manufacturer
WHERE (di.di_DeviceInfoId = @deviceInfoId AND DeviceDefinitionCategoryID not in('7E792E2F-B488-4C9F-A811-9E12501EA6A9','896FC890-A386-4D40-8051-1D0F3120F1C0')
AND dd.DeviceDefinitionID = di.di_DeviceDefinitionId)
then consider second part .
SELECT 'Categories' AS Section, ddsXref.DeviceDefinitionID, NULL AS DeviceDefinitionSettingID,
ddc.DeviceDefinitionCategoryID, NULL AS SettingName, NULL AS SettingDescription,
NULL AS IsInitialDeviceSetting, NULL AS DefaultValue, NULL AS MaximumLength,
NULL AS RegularExpression, NULL AS MinimumValue, NULL AS MaximumValue, NULL AS EnumValue,
NULL as DeviceDefinitionSettingValueID,NULL as HasBeenExportedOnce,
NULL as DeviceDefinitionSettingDefaultValue,NULL AS DataTypeName, NULL AS DeviceCommunication,
NULL AS UserAuthenticationType, NULL AS SoftwareVersion, NULL AS DeviceType, NULL AS Model,
NULL AS Manufacturer, ddc.DeviceDefinitionCategoryType,ddc.DeviceDefinitionCategoryName,
ddc.DeviceDefinitionCategoryDescription, NULL AS CreatedDate,NULL AS IsReadOnly,NULL AS SettingLabel,
NULL AS DisplayOrder
FROM DeviceDefinitionCategory AS ddc
INNER JOIN DeviceDefinitionSettingXref as ddsXref ON
ddsXref.DeviceDefinitionCategoryID=ddc.DeviceDefinitionCategoryID
INNER JOIN DeviceDefinition AS dd ON
dd.DeviceDefinitionID = ddsXref.DeviceDefinitionID
INNER JOIN DeviceInfo AS di ON
di.di_DeviceType = dd.DeviceType AND
di.di_Model = dd.Model AND
di.di_Manufacturer = dd.Manufacturer
WHERE (di.di_DeviceInfoId = @deviceInfoId AND ddc.DeviceDefinitionCategoryID not in('7E792E2F-B488-4C9F-A811-9E12501EA6A9','896FC890-A386-4D40-8051-1D0F3120F1C0')
AND dd.DeviceDefinitionID = di.di_DevicedefinitionId)
then consider 3rd part. Try to combine the first part & second part. then 3rd part .this may get you closer to your desired output
If Not Getting then try the join with less number of tables .
.keep patience while debugging.
Happy Coding :-)