Hi,
when it fetch result it should write in excel file..
First of all if you want to create a file which include XML structure, then you need that the query will return the result in XML format.
This can be done using the hint FOR XML. You can read more information and example in the following documnetation:
Now, that your query returns the result as XML, we can discussion about passing the result to a new XML file.
There are tools which can do both steps for you
Option 1: Bulk exporting XML data using bcp unitality
Note: This is the proffered option probably
Option 2: using SSMS and FOR XML
Execute the query using FOR XML -> click the result -> this will open the result of the XML in new tab -> save the XML file
Note: this is the simplest option and most flexible since you can control the XML structure using the query
Option 3: Using Excel
Open Excel -> click the tab Data -> select get data -> from sql server
This will import the data directly to the excel
Next you can save the data as XML
https://support.microsoft.com/en-us/office/export-xml-data-0b21f51b-56d6-48f0-83d9-a89637cd4360
Note: this solution makes no sense for DBA but can fit people that do not familiar with the option FOR XML in queries and do not want to write any code