sql-server - 如何使用PowerShell脚本从SQL Server中的表中删除行?

标签 sql-server powershell powershell-3.0 powershell-4.0

$uncServer = "\\10.243.174.102\e$"
$uncFullPath = "$uncServer\New folder\Demo.txt"
$username = "XYZ"
$password = "xyz"

net use $uncServer $password /USER:$username


$SQLServer = "AP-PUN-SRSTEP29\MSSQLSERVER12" #use Server\Instance for named SQL instances! 
$SQLDBName = "SystemDB"
$SqlQuery = "Delete * from V_Solution WHERE Notes ='9.4.4'";

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True"
#$SqlConnection.open()


$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection

$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd

我在远程服务器上安装了SQL Server 2012,我想使用PowerShell脚本从本地计算机的特定数据库中的特定表中删除一行。有可能这样做吗?

最佳答案

一种方法是像在任何.NET应用程序中一样使用ADO.NET对象。下面的PowerShell示例不需要安装SQL工具。

要使用Windows身份验证执行查询,请在连接字符串中指定Integrated Security=SSPI:

$connectionString = "Data Source=YourServer;Initial Catalog=YourDatabase;Integrated Security=SSPI";
$connection = New-Object System.Data.SqlClient.SqlConnection($connectionString);
$command = New-Object System.Data.SqlClient.SqlCommand("DELETE FROM dbo.YourTable WHERE YourTableID = 1", $connection);
$connection.Open();
$rowsDeleted = $command.ExecuteNonQuery();
Write-Host "$rowsDeleted rows deleted";
$connection.Close();

要使用SQL身份验证执行查询,请在连接字符串中指定User ID=YourSqlLogin;Password=YourSqlLoginPassword
$connectionString = "Data Source=YourServer;Initial Catalog=YourDatabase;User ID=YourSqlLogin;Password=YourSqlLoginPassword";
$connection = New-Object System.Data.SqlClient.SqlConnection($connectionString);
$command = New-Object System.Data.SqlClient.SqlCommand("DELETE FROM dbo.YourTable WHERE YourTableID = 1", $connection);
$connection.Open();
$rowsDeleted = $command.ExecuteNonQuery();
Write-Host "$rowsDeleted rows deleted";
$connection.Close();

无论哪种情况,都需要对该表具有DELETE权限。

我不确定您添加到问题中的脚本中NET USE命令的用途,除非那是为了在工作组环境中对服务器进行身份验证。就个人而言,我将只使用SQL身份验证并删除NET USE的丑陋性。

编辑:

如果同一批处理中有多个SELECT语句,则每个语句将返回一个单独的记录集。如果您使用的是NextRecordset,则需要调用DataReader,如果没有更多的记录集,它将返回false:
$reader = $command.ExecuteReader();
do {
    While($reader.Read()) {
        #process row here;
    }
} while($reader.NextResult());

或者,您可以使用DataAdapter填充“数据集”。 DataSet将为每个结果集包含一个单独的DataTable:
$da = New-Object System.Data.SqlClient.SqlDataAdapter($command);
$ds = New-Object System.Data.DataSet;
$null = $da.Fill($ds);
foreach($dt in $ds.Tables) {
    $dt | Out-GridView;
}

如果列数和数据类型相同,则还可以调整SQL查询以使用UNION ALL将结果连接到单个结果集中。这是一个示例片段:
$sqlQuery = @("
SELECT *
FROM DB926.dbo.Version_Solution 
WHERE Notes ='9.2.7'
UNION ALL
SELECT *
FROM DB_926.dbo.Version_Solution
WHERE Notes ='9.2.7'";
);
$command = New-Object System.Data.SqlClient.SqlCommand($sqlQuery, $connection);

关于sql-server - 如何使用PowerShell脚本从SQL Server中的表中删除行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47616979/

相关文章:

windows - 电源外壳 3.0 : Alternative to "Get-Volume"

powershell - 如何在PowerShell中将文件复制到多个文件夹

powershell - 使用 powershell 获取当月的天数

powershell - ConvertFrom-Json $ array只得到第一个元素?

powershell - Powershell-在运行之前检查脚本的更新

mysql - 使用 2 个表进行数据透视

json - 批量导入JSON到SQL Server

c# - 如何使用SSIS脚本任务删除Excel目标中的第二行? SQL Server 2012

sql-server - t-sql转义问题

c# - SQL Server Express 版本问题