2013-10-01 112 views
0

嗨我想更新一列,雖然似乎沒有工作。它提取數據並在字符串中進行日期轉換,但下面我的代碼的更新部分不想插入Powershell所做的更改。Powershell更新SQL Server列

## connect to db and do stuff 
$Connection = new-object system.data.SqlClient.SQLConnection("Data Source=  (local);Integrated Security=SSPI;Initial Catalog=TESTDB"); 
$sqlqry = "select row_id, DestFileName from FL_Input Where DestFileName like '%[0-9] '  + 'Jan%' + ' [0-9]%'" 
$Command = new-object system.data.sqlclient.sqlcommand 
$Command.CommandText = $sqlqry 
$Command.Connection = $Connection 
$Connection.Open() 

$UpdateCmd = new-object System.Data.SqlClient.SqlCommand 
$UpdateCmd.Connection = $Connection 

## for each filename, update timestamp - this replaces your $file in $files loop. 
$Result = $Command.ExecuteReader() 
while ($Result.Read()) { 

$destfile = $Result['DestFileName'] 
$srcfile = $destfile 
$row_id = $Result['row_id'] 

## do all your regex stuff here 

$destfile -match '\d{2}\s\w+\s\d{2,4}' | Out-Null <# Test #> 
$destfile -match '\d{2}\-\w+\-\d{4}' | Out-Null <# Test #> 

$destFile -replace "$(($matches).values)" , "$(get-date "$(($matches).Values)" -Format  yyyyMMdd)" 

write-host "{0}->{1}" -f $destfile, $srcfile 

# when you're finished doing your regex, push the result result back to the db: 
$updateqry = "update FL_Input set DestFileName='{0}' WHERE Row_ID = {1} " -f $destfile, $row_id 
$UpdateCmd.CommandText = $updateqry 
$UpdateCmd.ExecuteNonQuery() 

} 

## all done 
$Connection.Close 

回答

0

你不缺周圍的ROW_ID例如爲:

$updateqry = "update FL_Input set DestFileName='$destfile' WHERE Row_ID = '$row_id'" 
單引號