如何使用PowerShell删除SQL记录
我有一个简单的脚本来比较两个表,我只是想知道如何修改它,以便如果hold_inv
表有差异,它将删除它。
因此,基本上我的temp_hold_inv
包含最新数据,我想始终与我的hold_inv
表同步。
假设从temp_hold_inv
表删除特定用户比较。
在这里,我能够获得差异,但不确定如何使用$ campelult删除与hold_inv
和daily_proc
表的所有差异的所有差异
$query2 = "
SELECT 'Temp_Hold_Inv' AS SRC, T1.*
FROM (
SELECT Hold, GID, Source FROM Temp_Hold_Inv
EXCEPT
SELECT Hold, GID, Source FROM Hold_Inv
) AS T1
UNION ALL
SELECT 'Hold_Inv' AS SRC, T2.*
FROM (
SELECT Hold, GID, Source FROM Hold_Inv
EXCEPT
SELECT Hold, GID, Source FROM Temp_Hold_Inv
) AS T2
;"
$compareResult = Invoke-Sqlcmd -Query $query2 -ServerInstance $Server -Database $Database
。
I have this simple script that compare the two table and I'm just wondering how can I modify it so that it'll delete if there is a difference in Hold_Inv
table.
So basically my Temp_Hold_Inv
contain the latest data and I wanted to always in sync with my Hold_Inv
table.
Let say a particular user is removed from Temp_Hold_Inv
table but the data is already Existed in Hold_inv
and Daily_Proc
table then removed from those two table too after comparison.
Here I'm able to get the difference but not sure how to use $comparedResult to delete all the difference from Hold_Inv
and Daily_Proc
Table
$query2 = "
SELECT 'Temp_Hold_Inv' AS SRC, T1.*
FROM (
SELECT Hold, GID, Source FROM Temp_Hold_Inv
EXCEPT
SELECT Hold, GID, Source FROM Hold_Inv
) AS T1
UNION ALL
SELECT 'Hold_Inv' AS SRC, T2.*
FROM (
SELECT Hold, GID, Source FROM Hold_Inv
EXCEPT
SELECT Hold, GID, Source FROM Temp_Hold_Inv
) AS T2
;"
$compareResult = Invoke-Sqlcmd -Query $query2 -ServerInstance $Server -Database $Database
Any help or suggestion will be really appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要一个
Merge
语句,但是您需要确定表的主要键是为了匹配行。You need a
MERGE
statement, but you need to work out what is the primary key of the table in order to match rows up.