Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Running bcp from powershell

$
0
0

Hi,

I have the below job step, which I need to convert into powershell, because the security policies do not allow xp_cmdshell to be used.

How can I change the below TSQL into powershell?

Thanks

 

DECLARE  

 @saveas VARCHAR(2048)

,@query VARCHAR(2048)

,@bcpquery VARCHAR(2048)

,@bcpconn VARCHAR(64)

,@bcpdelim VARCHAR(2)

,@tablename sysname

,@s NVARCHAR(500)

SET @bcpdelim   = '|'

SET @bcpconn    = '-T' -- Trusted

select @tablename = MIN(name) from MyDb_Archive..sysobjects (nolock) where name like 'MyTableStaging_______'

while @tablename is not null 

begin

   SET @query      = 'select * from MyDb_Archive.dbo.' + @tablename

   SELECT @saveas = [value] FROM [SupportDB].[dbo].[PartitioningConfiguration] (nolock) WHERE name = 'ArchivePath'

   SET @saveas     = @saveas + REPLACE (@@Servername, '\', '_') + '_MyDb_Archive_' + @tablename + '.txt'

   SET @bcpquery = 'bcp "' + replace(@query, char(10), '') + '" QUERYOUT "' + @saveas + '" -n -t^' + @bcpdelim + ' ' + @bcpconn + ' -S ' + @@servername

   EXEC master..xp_cmdshell @bcpquery  

   select @tablename = MIN(name) from MyDb_Archive..sysobjects (nolock) where name like 'MyTableStaging_______' and name > @tablename

end 


Viewing all articles
Browse latest Browse all 6937

Trending Articles