Вопрос

I'm using the following code to try to use Log Parser to fire off and dump the contents of a .csv file I have into a SQL database. I'm having to try to use a custom function to strip out non-alphanumeric characters so that the columns can be created dynamically, because the end goal is to have this work with any .csv file I give it.

Here's the code:

start-process -NoNewWindow -FilePath "C:\Program Files (x86)\Log Parser 2.2\LogParser.exe" -ArgumentList @"

"Create Function [dbo].[RemoveNonAlphaNumCharacters](@Temp VarChar(1000))
Returns VarChar(1000)
AS
Begin

    Declare @KeepValues as varchar(50) = '%[^a-z0-9]%'
    While PatIndex(@KeepValues, @Temp) > 0
        Set @Temp = Stuff(@Temp, PatIndex(@KeepValues, @Temp), 1, '')

    Return @Temp
End
SELECT.RemoveNonAlphaNumCharacters * INTO SQLCounters FROM C:\Users\SeanLon\Desktop\SQL_Log_0.csv" -i:CSV -o:SQL -server:MJNHNX4 -database:PerfmonCounters -driver:"SQL Server" -createTable:ON

"@

And the error:

start-process : Process with an Id of 221104 is not running.
At C:\Users\seanlon\Desktop\Performance\Powershell examples\LogParser.ps1:1 char:1
+ start-process -NoNewWindow -FilePath "C:\Program Files (x86)\Log Parser 2.2\LogP ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Start-Process], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.StartProcessCommand

Can start-process or log parser simply not handle that complex of SQL within the argument list? Or is the LogParser process closing out before something else happens?

Это было полезно?

Решение

On PowerShell 3.0 it looks like it is passing the parameters cleanly through. However, to simplify this you could put your SQL in a file foo.sql and then call logparser like so:

$logParser = "C:\Program Files (x86)\Log Parser 2.2\LogParser.exe"
& $logParser file:c:\foo.sql -i:CSV -o:SQL -server:MJNHNX4 -database:PerfmonCounters -driver:"SQL Server" -createTable:ON

Другие советы

LogParser knows nothing about the database, it interprets and executes simple SQL queries in its internal engine, which doesn't handle stored procedures. Also see Logparser not recognizing SQL command.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top