Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Set Query Results to 'File' to Text in script
Message
From
25/03/2010 14:32:14
 
 
To
25/03/2010 13:29:14
General information
Forum:
Microsoft SQL Server
Category:
Scripting
Miscellaneous
Thread ID:
01457214
Message ID:
01457238
Views:
32
>A little but annoying thing. Is there any way to set Query Results to "File" or "Text" in a script?
>Googling has not turned up anything.
>
> I know I can do it in SSMS, but I would like to be able to just set it in my scripts.
>
>Thanks

Perhaps this will save you some time ( as Sergey and Naomi said you'll need to shell out - the first script is how the xp_cmdshell gets enabled )
--SQL SERVER – Enable xp_cmdshell using sp_configure



--The xp_cmdshell option is a server configuration option that enables system administrators to control whether the xp_cmdshell extended stored procedure can be executed on a system.
---- To allow advanced options to be changed.

EXEC sp_configure 'show advanced options', 1

GO

--To update the currently configured value for advanced options.

RECONFIGURE

GO

-- To enable the feature.

EXEC sp_configure 'xp_cmdshell', 1

GO

-- To update the currently configured value for this feature.

RECONFIGURE

GO 
Then this is an example of where I have a script that is writing a script ( to .sql file )
DECLARE @source VARCHAR(200)
DECLARE @result VARCHAR(200)
DECLARE @cmd VARCHAR(200)
SET NOCOUNT ON

SET @source = '"C:\Users\HARRY\Documents\SQL Server Management Studio\PDS\Create Script to drop all rowguidcols.sql"'
SET @result = '"C:\Users\HARRY\Documents\SQL Server Management Studio\PDS\Drop all Membership rowguidcols.sql"'

SET @cmd = 'SqlCmd -E -S (local)  -i '+@source + ' -o ' +@result 

EXEC master..xp_cmdshell @cmd;
Biggest gotcha is making sure you have permissions enabled on the folder where you are writing the file.

HTH


Charles Hankey

Though a good deal is too strange to be believed, nothing is too strange to have happened.
- Thomas Hardy

Half the harm that is done in this world is due to people who want to feel important. They don't mean to do harm-- but the harm does not interest them. Or they do not see it, or they justify it because they are absorbed in the endless struggle to think well of themselves.

-- T. S. Eliot
Democracy is two wolves and a sheep voting on what to have for lunch.
Liberty is a well-armed sheep contesting the vote.
- Ben Franklin

Pardon him, Theodotus. He is a barbarian, and thinks that the customs of his tribe and island are the laws of nature.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform