how to define a string variable with space in variable name in GAMS with $set - gams-math

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%'

Related

Escape character within single quotes

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.

Tcl SQLite update variable substitution cannot have apostrophe

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"

Redis command line SET value containing double quotes

I want to use redis command line (using redis-cli) to store json values. This is what I do
redis 127.0.0.1:6379> set test '{"a":"b"}'
This command fails with message :
Invalid argument(s)
I don't have problem with setting values that don't contain double quotes. What is the correct way to escape double quotes?
Add slashes to quotes
set test "{\"a\":\"b\"}"
We can use single quotes for storing the JSON value:
set name '{"a":"b"}'
Run get query like: get name
output : {"a":"b"}
later redis has fixed this problem. single quote works fine.

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

Why is Powershell ignoring my quotation marks?

I have a Powershell script and no matter what I try it completely ignores any quotation marks in the script. The error occurs when I run the script from my VB.NET code yet, I have been running scripts successfully through VB.NET for a while now.
For example, my script starts with finding out the server name...
This works:
$name = gc env:computername
This does not:
$name = "SERVERNAME"
The error given is something along the lines of $name = SERVERNAME is not recognized as a cmdlet.... and so on.
This is causing several lines in my script to fail and the error message always displays the command WITHOUT the quotation marks.
When assigning string values you have to quote them (single or double quotes).
$name = 'SERVERNAME'
or
$name = "SERVERNAME"
or assign it directly to a variable (doesn't require quotes):
$name = $env:COMPUTERNAME
I'm not familiar with doing it from vbs, but the symptoms indicate the parser is discarding the quotes. If you double-qoute the strings:
$name = ""SERVERNAME""
It should discard the outer set, and leave you with the inner set.
Dim name As String = "SERVERNAME" maybe?