当前位置: 动力学知识库 > 问答 > 编程问答 >

sql - How to update only exact value matches?

问题描述:

I am using SQL update query [in powershell script] to update machine name present in column.

The column values are stored as URL. So machine name is part of the string.

( Column Name : URL

Example column value : "http://Machine1:80/Impl100/Core/Data/Config.0")

I am using following query to do the update.

$UpdateQuery="UPDATE [$DatabaseName].[dbo].[$TableName]

SET $columnName=Replace("$ColumnName", '$OldMachineName', '$NewMachineName');"

Assume my old machine name is "Machine1" and i am to replace with "AppMachine1". If i execute the script ,it will update "AppMachine1". But if i execute the script second time It will update it as "AppAppMachine1" as Machine1 is part of AppMachine1.

Is it possible to get the column value and check the machine name before update?

How to do this?

网友答案:

Try this

$UpdateQuery="UPDATE [$DatabaseName].[dbo].[$TableName] 
SET $columnName=Replace("$ColumnName", '$OldMachineName', '$NewMachineName')
WHERE "$ColumnName" <> '$NewMachineName';"
网友答案:

Include the double leading forward slashes:

 ... -replace '//machine1','//AppAppMachine1'

You also need to put quoted variables in double quotes. Single quotes disables variable expansion and as a result variables will no be replaced with its values.

Here's an example of a double replace:

PS> $url = 'http://machine1:80/Impl100/Core/Data/Config.0'
PS> $url -replace '//machine1','//AppAppMachine1' -replace '//machine1','//AppAppMachine1'

http://AppAppMachine1:80/Impl100/Core/Data/Config.0
分享给朋友:
您可能感兴趣的文章:
随机阅读: