使用 PowerShell 将 Null 值插入 SQL Server

发布于 2025-01-17 18:46:26 字数 971 浏览 3 评论 0原文

我不想在这个特定事件的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

征﹌骨岁月お 2025-01-24 18:46:26

尝试使用 [System.DBNull]::Value 而不是 powershell 的 $null

$null1 = [System.DBNull]::Value

Try using [System.DBNull]::Value rather than powershell's $null:

$null1 = [System.DBNull]::Value
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文