SQL Server Magazine Forums

Spacer
< REFRESH >
Topic Title: xp_cmdshell alternative
Topic Summary:
Created On: 12/09/2008 08:28 AM

View topic in raw text format.
 12/09/2008 08:28 AM
 User is offline  Print this message

Author Icon
Desperado

Posts: 34
Joined: 08/06/2002

I need help here...
xp_cmdshell is disabled and I need to run this type of code within a SQL Server Agent Job...

Can anyone help me on how to use SQLCMD or similar code within a SQL Server Agent job without having to use xp_cmdshell?

Thanks

DECLARE
@Date char(8),
@SQLCmd nvarchar(360)
@ServerName varchar(100),
@DatabaseName varchar(100),
@QueryStmt varchar(1000),
@TargetFile varchar(256)


SELECT
@ServerName = 'DEVSERVER',
@DatabaseName = 'DEVDB',
@QueryStmt = 'EXEC uspMyProc ''2005'', ''2008-04-01 00:00:00.000'',''2009-03-31 00:00:00.000''',
@TargetFile = '\\FILESERVER\FILESAHRE\Budget_',
@Date = convert(varchar(8), getdate(), 112),

SELECT @TargetFile = @TargetFile + SUBSTRING(@Date,1,4) + '_' + SUBSTRING(@Date,5,2) + '_' + SUBSTRING(@Date,7,2)+ '.txt'

SELECT @SQLCmd = 'exec master.dbo.xp_cmdshell ''sqlcmd -S ' + @ServerName + ' -E -d ' + @DatabaseName + ' -Q "' + @QueryStmt + '" -w 300 -o "' + @TargetFile +'"'''

EXEC sp_executesql @SQLCmd
    Reply     Quote     Top     Bottom    
 12/09/2008 08:35 AM
 User is offline  Print this message

Author Icon
russellb

Posts: 1964
Joined: 06/14/2004

seems a good job for SSIS package. or use an ActiveX script to dynamically generate the file, then BCP

-- Russell --
------------------------------------------
SQL Server Magazine Forum Pro
    Reply     Quote     Top     Bottom    
 12/09/2008 09:06 AM
 User is offline  Print this message

Author Icon
Desperado

Posts: 34
Joined: 08/06/2002

Nice...
Excuse my stupidity but I need a kick start.
Can you give me a sample code based on what I have provided?
This will avoid my several hours of searching.
Much appreciated.
    Reply     Quote     Top     Bottom    
 12/10/2008 06:54 AM
 User is offline  View Users Profile  Print this message

Author Icon
gunneyk

Posts: 1128
Joined: 07/27/2007

What version of SQL Server are you using? If this is 2005 then look at using a Proxy account to run cmd shell type of job steps. In 2005 all jobs should use Proxy accounts with Crendentials that have permissions to do what needs to be done and no more. But in this case why not use a linked server? It appears as if all you are doing is executing a sp on a remote machine. So why not create a lined server and call it that way?

-------------------------
Andrew J. Kelly
    Reply     Quote     Top     Bottom    
 12/10/2008 12:11 PM
 User is offline  Print this message

Author Icon
Desperado

Posts: 34
Joined: 08/06/2002

To answer your first question, we're using SQL 2005 SP2
As for your linked server option, I wish it were that simple.
We need to output to file through a SQL Agent job.
The only way we've been able to easily do this was through a SQLCMD (ISQL), passing parameters by building the statement executed by xp_cmdshell.

Using SQLCMD from SSMS query window is an option, however this option is only good when we are logged on and running SSMS. This needs to be done through a scheduled job.....

So... apart from hunting in Books On Line for a viable proposal, how can I build an ActiveX script that would replicate what I need to do as indicated in my opening remarks?
    Reply     Quote     Top     Bottom    
 05/20/2009 07:46 AM
 User is offline  Print this message

Author Icon
MattiasLind

Posts: 14
Joined: 02/21/2008

Check out the tutorials on SSIS together with the samples.

Create a SSIS package that extracts the data from the database and creates the file.

I believe the tutorial Creating a Basic package will do this for you.

You will need a variable for the filename and configure the "Flat File Data Source"-Connectionsstring with an expression setting it to the variable.

Check out the tutorial, it'll rock you and you'll see the light!

Then deploy the package to the server of your choice and schedule it with SQL Agent.

-------------------------
Well, I'd like a cowboy-"Author Icon". In the meanwhile I'll just settle with a cow... yee-haw
    Reply     Quote     Top     Bottom    
View topic in raw text format.
< REFRESH >

FuseTalk Enterprise Edition - © 1999-2009 FuseTalk Inc. All rights reserved.