0

我不知道如何在 Invoke-Sqlcmd 中使用 Powershell 变量。
我在msdn 示例中看到:

 $MyArray = "MyVar1 = 'String1'", "MyVar2 = 'String2'"
 Invoke-Sqlcmd -Query "SELECT `$(MyVar1) AS Var1, `$(MyVar2) AS Var2;" -Variable $MyArray

,但似乎不是我需要的。而且我也无法理解示例,$MyArray = "MyVar1 = 'String1'"MyVar1变量名String1还是变量?填充到sql命令中会是SELECT MyVar1 = 'String1' AS Var1;但是,是什么var1意思呢?

我的代码:

$FirstDayOfLastMonth = (Get-Date -Day 1 -hour 0 -Minute 0 -Second 0).AddMonths(-1)

$LastDayOfLastMonth = (Get-Date -Day 1 -hour 23 -Minute 59 -Second 59).AddMonths(-1).AddDays([DateTime]::DaysInMonth($FirstDayOfLastMonth.year, $FirstDayOfLastMonth.month) - 1)

Write-Verbose "Invoke-Sqlcmd for winners"
Invoke-Sqlcmd -Query @"
    use iMAG;
    select L.Name AS LuckyDrawName, P.ParticipationTime, Z.Name as PrizeName, W.MobileNumber, T.ParticipantUniqueIdentifier
    FROM LuckyDrawWinner W
    INNER JOIN LuckyDrawParticipation P ON P.LuckyDrawParticipationId = W.LuckyDrawParticipationId
    INNER JOIN LuckyDrawParticipant T ON T.LuckyDrawParticipantId = P.LuckyDrawParticipantId
    INNER JOIN LuckyDrawPrize Z ON Z.LuckyDrawPrizeId = W.LuckyDrawPrizeId
    INNER JOIN LuckyDraw L ON L.LuckyDrawId = Z.LuckyDrawId
    WHERE cast(P.ParticipationTime AS date) < $LastDayOfLastMonth And cast(P.ParticipationTime AS date) > $FirstDayOfLastMonth
    and L.LuckyDrawId IN (3) and Z.Grade <> 0
"@ | Export-Csv -Path $LuckyDrawWinnerReportFilePath -NoTypeInformation -Encoding "UTF8"

直接使用$LastDayOfLastMonth是不行的。我也尝试cast($LastDayOfLastMonth AS date)使用 Invoke-Sqlcmd 。
然后尝试$s = $FirstDayOfLastMonth.ToString("yyyy-MM-dd HH:mm:ss"),然后使用cast($s AS date)。两者都不起作用。

输出错误:

Invoke-Sqlcmd : Incorrect syntax near '23'.
At D:\ScheduledTasks\EmailReport\DailyEmailReport.ps1:32 char:14
+ Invoke-Sqlcmd <<<<  -Query @"
    + CategoryInfo          : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerShellSqlExecutionException
    + FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand

我是powershell的新手,这个问题可能很简单,但我已经搜索了invoke-sqlcmd日期变量和其他密钥很长时间没有找到任何有用的东西。如果您能提供帮助,我将不胜感激。


更新
我像 msdn 一样编辑我的代码:

$Vars="FirstDayOfLastMonth='$FirstDayOfLastMonth'", "LastDayOfLastMonth='$LastDayOfLastMonth'"

Invoke-Sqlcmd -Query @"
    use iMAG;
    select L.Name AS LuckyDrawName, P.ParticipationTime, Z.Name as PrizeName, W.MobileNumber, T.ParticipantUniqueIdentifier
    FROM LuckyDrawWinner W
    INNER JOIN LuckyDrawParticipation P ON P.LuckyDrawParticipationId = W.LuckyDrawParticipationId
    INNER JOIN LuckyDrawParticipant T ON T.LuckyDrawParticipantId = P.LuckyDrawParticipantId
    INNER JOIN LuckyDrawPrize Z ON Z.LuckyDrawPrizeId = W.LuckyDrawPrizeId
    INNER JOIN LuckyDraw L ON L.LuckyDrawId = Z.LuckyDrawId
    WHERE cast(P.ParticipationTime AS date) > $(FirstDayOfLastMonth) AND cast(P.ParticipationTime AS date) < $(LastDayOfLastMonth)
    and L.LuckyDrawId IN (3) and Z.Grade <> 0
"@ -Variable $Vars | Export-Csv -Path $LuckyDrawWinnerReportFilePath -NoTypeInformation -Encoding "UTF8"

但事实证明:

The term 'FirstDayOfLastMonth' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of
 the name, or if a path was included, verify that the path is correct and try again.
At D:\ScheduledTasks\EmailReport\iDQDailyEmailReport.ps1:45 char:20
+ FirstDayOfLastMonth <<<< 
    + CategoryInfo          : ObjectNotFound: (FirstDayOfLastMonth:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : CommandNotFoundException
4

2 回答 2

3

您可以先尝试使用以下格式将字符串日期放入您的变量中:

$FirstDayOfLastMonth.tostring("yyyy/MM/dd hh:mm:ss")
$LastDayOfLastMonth.tostring("yyyy/MM/dd hh:mm:ss")

这个想法是有一个查询字符串,看起来像您将在查询管理器中使用的字符串。对于您的调试,将查询分配给一个字符串并打印它。

$a = @"
    use iMAG;
    select L.Name AS LuckyDrawName, P.ParticipationTime, Z.Name as PrizeName, W.MobileNumber, T.ParticipantUniqueIdentifier
    FROM LuckyDrawWinner W
    INNER JOIN LuckyDrawParticipation P ON P.LuckyDrawParticipationId = W.LuckyDrawParticipationId
    INNER JOIN LuckyDrawParticipant T ON T.LuckyDrawParticipantId = P.LuckyDrawParticipantId
    INNER JOIN LuckyDrawPrize Z ON Z.LuckyDrawPrizeId = W.LuckyDrawPrizeId
    INNER JOIN LuckyDraw L ON L.LuckyDrawId = Z.LuckyDrawId
    WHERE cast(P.ParticipationTime AS date) < $LastDayOfLastMonth And cast(P.ParticipationTime AS date) > $FirstDayOfLastMonth
    and L.LuckyDrawId IN (3) and Z.Grade <> 0
"@

write-host $a

Invoke-Sqlcmd -Query $a | Export-Csv -Path $LuckyDrawWinnerReportFilePath -NoTypeInformation -Encoding "UTF8"

已编辑

$(FirstDayOfLastMonth) 在您的上下文中没有任何意义,您应该使用($FirstDayOfLastMonth)$($FirstDayOfLastMonth)

于 2013-08-27T04:18:16.043 回答
0

这对我有用,没有尴尬的格式:

$timevariable = get-date

$MyArray = "MyVar1 =  '$timevariable' "

Invoke-Sqlcmd -ServerInstance $server -Database $database -Query "insert SomeTable (datetimecolumn) VALUES (`$(MyVar1));" -Variable $MyArray -Verbose
于 2017-06-02T12:48:09.897 回答