Share via

SQL Server: Extract xml from table and save to file

T.Zacks 3,996 Reputation points
2021-10-24T07:21:40.427+00:00

Suppose i have a table where i have one xml type column where i save xml data. now i want to extract xml data to a file in my pc.
my scenario is i am using SSMS and db is in another pc. so how could i extract xml from db table and save to xml file in my pc instead of db server.

also tell me what permission is required for sql server to write file in my pc or server pc.

please guide me with sample code. thanks

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.

0 comments No comments

Answer accepted by question author

  1. Erland Sommarskog 134K Reputation points MVP Volunteer Moderator
    2021-10-24T09:54:51.077+00:00

    The short answer: You don't do this from SQL Server, period. You connect with BCP or some other client program from your PC to the server to write the file.

    Somewhat longer answer: SQL Server cannot really write files at all. There is no built-in functionality for this. You can spawn a Windows process with xp_cmdshell from where you run a client-side console-mode program. Or you can create a CLR stored procedure that writes to a file. Or, in recent versions of SQL Server, you use sp_external_script to run a script in R, Python or Java that writes the file. But there is not any native way of doing this from SQL Server (although you could argue that the CLR solution comes close.)

    If you are dead set on doing this, you need to create a Windows share on your PC and then grant permissions on this share. Exactly what to grant permissions to depends on the solution above you choose, but also on how you run that solution. That is, if you run it directly as yourself, or you schedule it from SQL Server Agent.

    No matter which way you go, you will get into a rabbit hole of Windows permissions that can take a long time to get right. And if you don't have a domain and your PCs are only in a workgroup, it will be even more difficult.

    All this when you simply could a program from your PC and everything will run smoothly.

    Was this answer helpful?

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.