使用 PowerShell 将 Null 值插入 SQL Server
我不想在这个特定事件的 SQL Server 数据库中插入修改日期,并且我想传递 NULL 值,但它在 modifiedDate
列而不仅仅是 NULL 值。
DB
[ModifiedDate] [datetime] NULL,
PS 脚本
$null1 = $NULL
$EventId2 = Get-EventId 30
Set-UsrHistory $EventId2.Event_Id $IN $ID $UPN $OneDrive $Mailbox $CreatedDate $null1
Function Get-EventId($EventId) {
$query = "select Event_Id from List_of_Events where Event_Id = '$EventId'"
$result = $db.ExecuteWithResults($query)
return $result.Tables[0]
}
Function Set-UsrHistory($EvtId, $IN, $ID, $UPN, $OneDrive, $Mailbox, $CreatedDate, $ModifiedDate) {
$query = "insert into User_History (Event_Id,IN,ID,UPN,OneDrive, Mailbox,CreatedDate, ModifiedDate) VALUES ('$EvtId',$IN,'$ID','$UPN','$OneDrive', '$Mailbox','$CreatedDate', '$ModifiedDate')"
$result = $db.ExecuteWithResults($query)
}
Get-EventId
函数从我的事件表中获取事件 ID,Set-UsrHistory
函数执行查询逻辑并插入到我的历史记录表中。
I don't want to insert a modified date into my SQL Server database for this particular event and I want to pass NULL value but it inserting "1900-01-01 00:00:00.000" in the modifiedDate
column instead of just NULL value.
DB
[ModifiedDate] [datetime] NULL,
PS Script
$null1 = $NULL
$EventId2 = Get-EventId 30
Set-UsrHistory $EventId2.Event_Id $IN $ID $UPN $OneDrive $Mailbox $CreatedDate $null1
Function Get-EventId($EventId) {
$query = "select Event_Id from List_of_Events where Event_Id = '$EventId'"
$result = $db.ExecuteWithResults($query)
return $result.Tables[0]
}
Function Set-UsrHistory($EvtId, $IN, $ID, $UPN, $OneDrive, $Mailbox, $CreatedDate, $ModifiedDate) {
$query = "insert into User_History (Event_Id,IN,ID,UPN,OneDrive, Mailbox,CreatedDate, ModifiedDate) VALUES ('$EvtId',$IN,'$ID','$UPN','$OneDrive', '$Mailbox','$CreatedDate', '$ModifiedDate')"
$result = $db.ExecuteWithResults($query)
}
Get-EventId
function get the Event Id from my Event table and Set-UsrHistory
function does that querying logic and insert to my History table.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用
[System.DBNull]::Value
而不是 powershell 的$null
:Try using
[System.DBNull]::Value
rather than powershell's$null
: