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
Related
I'm having an issue figuring out how to ignore signs and variables in a single quote string statement.
I am attempting to update a table with the new text with structure such as:
update xxx
set xxx =
'Our Ref. $BOOKING_NO$
.......
Kind regards'
If your $ chars are being interpreted, it isn't by Oracle ($ isn't special in Oracle anyway, and between single-quotes everything is a string), but rather by your client program or maybe shell script. If, for example, you are running this in SQL*Plus from a Unix-based shell script, you will need to use the appropriate means required by the shell you use to prevent the shell from interpreting $ and ' characters.
I would like to define a string variable in GAMS to call a server. Here is the syntax:
$set SERVER SERVER=ASERVER
The problem is that there is a space in the server name, so that the actual syntax is
$set SERVER SERVER=A SERVER
Then errors are reported like this "Error Unknow option "Server"". How should I handle a string variable with space in variable name? Thanks
Try it with quotes:
$set SERVER 'SERVER=A SERVER'
Edit: Another example how to use $call with an argument containing spaces (note that this is for Windows only, on Unix you would have to handle the spaces and quotes differently):
$echo $log %x% > log.gms
$call 'gams log.gms --x="With Space" lo=%gams.lo%'
I have a shell script script.sh, and a sql script modify_database.sql
In script.sh, I launch the sql script with some param using :
sqlplus user/password ... #modify_database.sql « param_id » « param_newValue »
And in the sql script, I get the parameters value with :
UPDATE T_TEST SET C1 = ¶m_newValue WHERE ID = ¶m_id
But I have a lot of problem with the « param_newValue » parameter.
In fact, this parameter could contain some spaces, slash, single quote and double quote character.
For example : L’avion du pilote était surnommé le « Redoutable »
I have a bunch of values to update.
Each new values are stored in a txt file, like that :
id;value
1; L’avion du pilote était surnommé le « Redoutable »
2; Another example of a « test » that’s it
How can I do to pass this param to sqlplus and set the value like that ? The quoted part is giving me a hard time :/
EDIT :
Example, in the input file I have :
123;;;"aaaa/vvvv/COD_039/fff=Avion d'office";"aaaa/vvvv/COD_039/fff=Hello d'orien";
I get this line, I do a cut command to get the first column (ID), the 4th column (to replace), and the 5th column (replacement).
I have a XML node, with a node which contain the 4th column text, and need to replace by the 5th column content.
So I do :
sqlplus -s $DBUSER/$DBPASSWORD#$DBHOST:$DBPORT/$DBSCHEMA #majConfXML.sql "$id" "$newcode"
update T_TEST set XML = updatexml(xmltype(XML_CONF),
'//A[#name="XXX"]//B[#name="YYY"]//pkValue','&2').getClobVal()
But in the param, there is some quotes.
So when I do the update request with '&2', the request is running but it does not work properly.
How can I escape/pass the param value into the updateXML request ?
Thank you
Finally, I find a problem in my bash script before the sqlplus call.
In fact, it was xargs command which I use who interpret simple quote. I simply add double quotes around each string with slash and quote in my input file.
Next, before calling sqlplus, I replace all “ ‘ “ (quote) by “ ‘’ “ (quote quote)
Here's the problem: if I use { } for the update command like so:
package require sqlite3
fileRepo eval {UPDATE uploads SET $col=$data WHERE rowid=$id}
I cannot substitute any variables inside the curly brackets. it all has to be hard coded.
However, if I use " " for the update command like so:
fileRepo eval "UPDATE uploads SET $col='$data' WHERE rowid=$id"
I can substitute variables inside the double quotes, but I must use ' ' in order to put in data with spaces so sql sees it as one input. If I don't I get an error if I send something like
$data = "Legit Stack"
Because it has a space the sql will choke on the word: Stack
unless it is placed within single quotes
Therefore...
If I send this data to the update command:
$col = description
$data = "Stack's Pet"
I get the following error:
near "s": syntax error while executing "fileRepo eval "UPDATE uploads
SET $col='$data' WHERE rowid=$id" ...
Thus given these rules I can see no way to pass a single quote or apostrophe to the update command successfully. Is there a different way to do this?
Thanks!
While it is true that you can escape the single quotes by doubling them (as usual in SQL), you open up your code to the dangers of SQL injection attacks.
It might be better to split your code into two distinct steps:
Substitute with format {UPDATE uploads SET %s=$data WHERE rowid=$id} $col
let sqlite3 magic eval turn the $data and $id into bound variables for a prepared statement
This way you only need to sanitize your col variable, to make sure it contains a valid column name and nothing else (should be easy), instead of all your data. In addition, you do not need to copy large values as often, so a two step approach will even be faster. To make it even clearer you want to use a bind variable, try the alternative syntax with a : in front of a variable name.
package require sqlite3
set stmt [format {UPDATE uploads SET %s=:data WHERE rowid=:id} $col]
fileRepo eval $stmt
Recommended Reading:
For the : syntax: https://www.sqlite.org/tclsqlite.html#eval
For more information about SQL Injections: https://www.owasp.org/index.php/SQL_Injection_Prevention_Cheat_Sheet
You have to use an escape apostrophe. So it should look like this:
$data = "Stack''s Pet"
I have a batch file which runs multiple sql files and passes parameters to them.
The input parameters are stored in a txt file which is read by batch file.
I am trying to pass varchar values to IN clause of SQL.
For e.g.
In my input file i this entry
var1="'tommy','jim'"
In Batch file
<code to read file , assuming %1 has the var1 value>
set param=%~1
sqlplus %DBCONN% #%programFolder%\test.sql %param%
test.sql (name is varchar2)
select * from table where name in (&1);
This gives error, saying invalid number
as it tries to run
select * from table where name in (tommy);
If i echo right before the sql stmt, its displays 'tommy','jim'
but in sql its removing jim and the single quotes ...
Please help!
Now i edited entry in Input file as
var1="'''tommy''','''jim'''"
it goes as select * from table where name in ('tommy');
But it truncates the second value
Any clue how to include comma ??
Finally found a way
input file -
var1=('tommy','jim')
Remove the braces from the sql file
select * from table where name in &1;
This works , i have no idea why was taking a comma was such an issue, there should have been some way to pass the comma !
If anyone finds out please let me know.
Thanks
The tilde (~) strips quotes from the variable.
Try using var1='tommy','jim' in your input file and set param=%1 in your batch script.