2

I have spent few hours searching with no luck today. What I need to do is to write a t-sql query which will be executed from within SSMS and which supposed save an output to a file. I have full admin access to the sql server 2008 r2 as well as to the OS (I believe Win 2008 Server)

I cannot use commandline, batch files etc. It needs to be done straight from SSMS as an execution of a t-sql script.

It doesn't even have to be pretty :-) It just needs to do the job.

I would very appreciate any help.

Regards Mariusz

2
  • Out of curiosity, if you have full access to the OS, why can't you use the commandline? Commented Sep 15, 2011 at 21:04
  • the script will be executed by support people of other company and that's the requirement I have been facing. I think it will be a part of some automated process which executes t-sql scripts. Commented Sep 15, 2011 at 21:15

3 Answers 3

5

Query > Results To > Results to File

You can also do a select all on the results pane, and right click the top-left cell. There's an option to "Save Results As," which will allow you save the results in CSV format.

If you're looking for something more advanced, I would suggest looking into SSIS (SQL Server Integration Services), which is the replacement for DTS. This is what's usually used for data file exports (DFEs)

EDIT

If you need to export the results to file via script, try this:

INSERT INTO OPENROWSET ('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=c:\Test.xls;','SELECT productid, price FROM dbo.product') 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks James, but it needs to be a t-sql script. It will be auto executed (I think) as a part of a bigger job, no human interaction involved. Otherwise I wouldn't be here asking for help :-)
James, I heard about OPENROWSET and will try it but isn't an Office on the machine requirement?
INSERT INTO OPENROWSET gives an error: Incorrect syntax near ';'.
You wouldn't need a full office install, we were able to get by with just installing the Access database engine (not Access or Office itself)
2

I can think of two options, given your constraints

  • BCP would be the best option, use it with the queryout option I spaced on bcp being a command line utility.

  • Create a sql agent job that has a single step which is an OS command. THe job has a single step which runs a sqlcmd which runs the query and redirects the output to a file. After creating the job, the script runs it and then deletes the job and any history. That is a blecherous solution that would delight Rube Goldberg but would satisfy the requirements of being done completely through SSMS/automated approach.

1 Comment

That probably will be the option for me. Thank you very much :-)
1

If you're within SSMS the option to output to a text file is one of the "Results to ..." options on the toolbar alongside the execute button. Are you trying to something more programmatic?

If you have permission to execute xp_commandshell from SSMS you may be able to take advantage of some of the techniques suggested by the psuedonymous Phil Factor at http://www.simple-talk.com/sql/t-sql-programming/the-tsql-of-text-files/.

4 Comments

I have to deliver a t-sql script that can be executed from within SSMS. It won't be me who will be doing this unfortunately.
If you can't use the suggestions in the Phil Factor examples can you let us know whether or not you're able to create static database objects (procs, jobs, functions, etc.) on the system to be run later by the tsql?
script will be run under full machine admin privileges I believe, so yes
I have just checked this fact again and no, there will be an error: SQL Server blocked access to procedure 'sys.xp_cmdshell' of component 'xp_cmdshell' because this component is turned off as part of the security configuration for this server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.