我不知道如何在Invoke-Sqlcmd中使用Powershell变量 我在msdn emxample:
中看到了 $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
不起作用。我也在Invoke-Sqlcmd中尝试cast($LastDayOfLastMonth AS date)
。
然后尝试$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
答案 0 :(得分: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)
答案 1 :(得分:0)
这对我来说没有笨拙的格式化:
$timevariable = get-date
$MyArray = "MyVar1 = '$timevariable' "
Invoke-Sqlcmd -ServerInstance $server -Database $database -Query "insert SomeTable (datetimecolumn) VALUES (`$(MyVar1));" -Variable $MyArray -Verbose