2016-08-24 70 views
0

我想使用powershell作爲較大任務的一部分運行以下訪問查詢。使用powershell運行使用detediff和逗號的訪問查詢

SELECT PG.LastName+`", `"+PG.FirstName AS Name, PG.Gender, PG.Ethnicity, PG.Language, PG.PhoneNumber, PG.FinancialClass AS FinClass, PG.BirthDate, Int(DateDiff(`"d`",PG.BirthDate,Now())/365.2425) AS Age, Switch(Int(DateDiff(`"d`",PG.BirthDate,Now())/365.2425)<12,PG.GLastName,Int(DateDiff(`"d`",PG.BirthDate,Now())/365.2425)>=12,Null) AS GLastName, Switch(Int(DateDiff(`"d`",PG.BirthDate,Now())/365.2425)<12,PG.GFirstName,Int(DateDiff(`"d`",PG.BirthDate,Now())/365.2425)>=12,Null) AS GFirstName FROM TestData INNER JOIN PG ON TestData.PI = PG.PINumber WHERE (((PG.Id) Not In (734,735))) 

當我經常從訪問運行查詢時,它工作正常。因爲我試圖通過PowerShell來運行它,我添加了`登錄引號和逗號的前面,但查詢還是拋出下面的錯誤,當我運行它

IErrorInfo.GetDescription失敗,E_FAIL(0x80004005的)

我檢查了一些以前的線程,並進行了上述更改,但它似乎仍然沒有工作。

任何有關上述查詢可能出錯的建議?

感謝, Sreekar

回答

0

我將宣佈它在下面的字符串(下面的例子),或不可擴張的字符串(單引號,「東西」),所以我不擔心轉義字符。

正常情況下,您可以在雙引號字符串中包含PowerShell變量("SELECT $myVariable ...")。

打擾格式化,這是這樣,我可以找到所有的轉義字符。

例如

$query = @" 
SELECT PG.LastName + ", " + PG.FirstName AS Name, 
    PG.Gender, 
    PG.Ethnicity, 
    PG.Language, 
    PG.PhoneNumber, 
    PG.FinancialClass AS FinClass, 
    PG.BirthDate, 
    Int(DateDiff("d", PG.BirthDate,Now())/365.2425) AS Age, 
    Switch(
     Int(DateDiff("d", PG.BirthDate, Now())/365.2425) < 12, PG.GLastName, 
     Int(DateDiff("d", PG.BirthDate, Now())/365.2425) >= 12, Null 
    ) AS GLastName, 
    Switch(
     Int(DateDiff("d", PG.BirthDate, Now())/365.2425) < 12, PG.GFirstName, 
     Int(DateDiff("d", PG.BirthDate,Now())/365.2425) >= 12, Null 
    ) AS GFirstName 
    FROM TestData 
    INNER JOIN PG ON TestData.PI = PG.PINumber 
    WHERE (((PG.Id) Not In (734,735))) 
"@ 

還有這下面的字符串的單引號變種:

@' 
SELECT ... 
'@ 

注:當打開此處的字符串開始之後不能再被任何東西,但一個換行符。當關閉這個字符串時,末尾不能有任何事物(包括空格縮進)。