Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Script object into a text file Expand / Collapse
Author
Message
Posted Tuesday, January 29, 2013 3:02 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 9:56 AM
Points: 1,882, Visits: 1,459
Hi All,

After this:
EXEC master.dbo.sp_configure 'show advanced options', 1
RECONFIGURE
EXEC master.dbo.sp_configure 'xp_cmdshell', 1
RECONFIGURE

--This works
declare @str varchar(100)
select @str = 'declare @i int'
print @str
select @str = 'ECHO ' + @str + ' >>C:\file1.txt'
EXEC xp_cmdshell @str, no_output

--This does not work
declare @str varchar(100)
select @str = 'create table #test
(ind int primary key not null,
name varchar(50)
)'
print @str
select @str = 'ECHO ' + @str + ' >>C:\file2.txt'
EXEC xp_cmdshell @str, no_output


Additionally, if I want to save "print object_definition(object_id)" where object_id is an id for a view with definition longer than 8000 chars, then what is the solution to save its definition into a text file?

xp_cmdshell expects parameter 'command_string' of type 'varchar', and varchar(max) is not supported.

Thanks in advance,
IgorMi


Post #1413351
Posted Tuesday, January 29, 2013 3:10 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, May 22, 2013 6:20 PM
Points: 89, Visits: 877
Have a look at the scripter object that is available in Powershell. This link has some details.

This would be a much better way of doing what you want.
Post #1413355
Posted Tuesday, January 29, 2013 10:41 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: 2 days ago @ 8:24 AM
Points: 1,176, Visits: 1,253
yes VARCHAR(MAX) is not allowed, but you can use VARCHAR(8000).

Try executing the code below:
declare @str varchar(8000)
select @str = 'create table #test
(ind int primary key not null,
name varchar(50)
)'
PRINT @str
select @str = 'ECHO ' + REPLACE(REPLACE(@str,CHAR(10),' '),CHAR(13),' ') + ' >>D:\file2.txt'
EXEC xp_cmdshell @str, no_output

I have also replaced, line-feeds and next line characters with space.


~ Lokesh Vij

Guidelines for quicker answers on T-SQL question
Guidelines for answers on Performance questions

Link to my Blog Post --> www.SQLPathy.com

Follow me @Twitter

Post #1413447
Posted Thursday, January 31, 2013 3:51 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 9:56 AM
Points: 1,882, Visits: 1,459
Hi Guys,

Thank you for your replies! They are both useful.
Albeit I'll go with the Scripter class in C#.

Regards
IgorMi
Post #1414380
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse