2013-10-01 80 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'" 
单引号