Is it possible to assign a value to already declared variable in NuoDB - sql

Is it possible to assign a value to a variable in NuoDB after the variable is created.
I've tried:
VAR $test string;
$test = 'test';
But it says:
[Code: -1, SQL State: 42000] syntax error on line 1
$test = 'test'
^ expected statement got $test
According to their examples "Example 2: Redefining variables within a stored procedure"
"A variable cannot be redefined, using the same name, within the same
scope or code block."
Wasn't able to find anything else regarding the variables in the docs. But something might have slipped out from me..
And moreover, it's incredible to have a variable without an opportunity to change it's value! Sounds like a nonsense.
Thanks in advance.

From the error message, I suspect that you simply need to change the DELIMITER.
Try:
SQL> set delimiter #
Delimiter is now [#]
SQL> var $test string;
> $test = 'abc';
> select $test from dual#
$TEST
------
abc
SQL>
Cheers!
Nik

eventually found that the problem was in the way I've been launching the script.
It was so silly of me... It was just because of the way I've been launching script. There were 3 buttons, to execute a query in a certain way, but the one I've used was just to execute a line under current position of a cursor.. Seems it has been trying to launch a separate line but not all the query. I've just tried a shortcut and it had gone well.
Thus, as a matter of fact, it was no problem with the code, but just with crooked hands =)

Related

Oracle SQL if statement based on parameter passed in via .sh script

I have a shell script which calls some SQL like so
sqlplus system/$password#$instance #./oracle/mysqlfile.sql $var1 $var2 $var3
Then in mysqlfile.sql, I define properties like this:
DEFINE var1=&1
DEFINE var2=&3
DEFINE var3=&3
Later in the file, I call another SQL script:
// i wish to wrap this in a if statement - pseudo-code
if(var3="true") do the following
#./oracle/myOthersqlfile.sql &&varA &&varB
I am not sure how to implement this though, any suggestions appreciated
You could (ab)use substitution variables:
set termout off
column var3_path new_value var3_path
select case
when '&var3' = 'true' then './oracle/myOthersqlfile.sql &&varA &&varB'
else '/dev/null'
end as var3_path
from dual;
set termout on
#&var3_path
The query between the set termout commands - which just hide the output of the query - uses a case expression to pick either your real file path or a dummy file; I've used /dev/null, but you could have a 'no-op' file of your own that does nothing if that's clearer. The query gives the result of that the alias var3_path. The new_value line before it turns that into a substitution variable. The # then expands that variable.
So if var3 is 'true' then that runs:
#./oracle/myOthersqlfile.sql &&varA &&varB
(or, actually, with the varA and varB variables already replaced with their actual values) and if it is false it runs:
#/dev/null
which does nothing, silently.
You can set verify on around that code to see when and where substitution is happening.
You can't implement procedural logic into sqlplus. You have these options :
Implement the IF-THEN-ELSE logic inside the shell script that is running the sqlplus.
Use PL/SQL, but then your SQL Script should be called as a process inside an anonymous block, not like an external script.
In your case the easiest way is to change your shell script.
#/bin/bash
#
# load environment Oracle variables
sqlplus system/$password#$instance #./oracle/mysqlfile.sql $var1 $var2 $var3
# if then
if [ $var3 == "true" ]
then
sqlplus system/$password#$instance #./oracle/myOthersqlfile.sql
fi
You should realise that sqlplus is just a CLI ( Command Line Interface ). So you can't apply procedural logic to it.
I have no idea what you do in those sql scripts ( running DMLs, creating files, etc ), but the best approach would be to convert them to PL/SQL, then you can apply whatever logic you need to.

How to ignore responding to stored '&lt' and '&gt' text in Oracle SQL statement?

I have an issue when writing the sql query below in SQL Command line. It asks me "Enter a value for lt:" and then gives error
ORA-00933: SQL command not properly ended
I need to properly read the column that includes '&lt' or '&gt' as a string. How can I edit the query to make it works?
Delete from authorization1
where role = 'staff' AND object = ' /department/gradstudent/gpa'
AND predicate = ' & l t ; 2.0') AND action = 'read'
Assuming you are using SQL*Plus or SQL Developer as your front end, this issue is that &foo is the syntax for defining substitution variables. You can
set define off;
before running your script to disable substitution variables. That will stop the front end from prompting you for a value.

BAT if exist %environment variable folder% (System folders)

i'd like to test if %PROGRAMFILES(X86)%\Folder exists but it seems like if EXIST doesn't like environment variables :/
my way would be :
if EXIST %PROGRAMFILES(X86)%\Folder (
set VAR=%PROGRAMFILES(X86)%\Folder
)
no matter how i try, it always outputs false...
if EXIST "%PROGRAMFILES(X86)%\Folder" (
set "VAR=%PROGRAMFILES(X86)%\Folder"
)
To check whether an environment variable exists:
if defined VARIABLE echo Yep, it's defined.
The following also works and prints the current value of the variable if set.
set VARIABLE && echo Found it! || echo Nope, sorry!
Note that SET also responds to prefixes, though, so if you have a var named VARIABLE, then "set var" and "set v" will also return true.
Not the question you asked, but it was how I read the title, so somebody else might have the same question.

Perl DBI SQL string error

I'm writing a perl script that will access a MySQL database to determine whether or not a tool has passed or failed and I'm getting an error when I try to fetchrow_array on the code.
Basically what is happening is I'm looping through a list of tests to see if they have passed or failed so the loop looks like:
foreach my $test (#tests){
$sth = $dbh_k->prepare("select fn_get_test_status(' $test ');");
$sth->execute();
my #data = $sth->fetchrow_array();
print("$test\n");
print("#data\n");
}
and that function returns either the string "passed" or "failed" however I can't figure out how to pull just that one string out and incorporate it into my perl scrip.
I keep getting the error Use of uninitialized value in join or string at line xx.
Any tips or ideas? thank you
EDIT:
I may have an array of arrays that is being returned as my data type, how to I go into this to check it's contents?
Here's the solution we figured out in the chat:
my $sth = $dbh_k->prepare("select fn_get_test_status(?);");
foreach my $test (#tests){
$sth->execute($test);
my #data = $sth->fetchrow_array();
printf "%s: %s\n", $test, #data;
}
Main problem were the spaces next to ' $test '.

powershell assigning output of a ps1 script to a variable

Let me start with I am very new to powershell and programming for that matter. I have a powershell script that takes some arguments and that outputs a value.
The result of the script is going to be something like 9/10 where 9 would be the number active out of the total amount of nodes. I want to assign the output to a variable so I can then call another script based on the value.
This is what I have tried, but it does not work:
$active = (./MyScript.ps1 lb uid **** site)
I have also tried the following which seems to assign the variable an empty string
$active = (./MyScript.ps1 lb uid **** site | out-string)
In both cases they run and give me the value immediately instead of assigning it to the variable. When I call the variable, I get no data.
I would embrace PowerShell's object-oriented nature and rather than output a string like "9/10", create an object with properties like NumActiveNodes and TotalNodes e.g. in your script output like so:
new-object psobject -Property #{NumActiveNodes = 9; TotalNodes = 10}
Of course, substitute in the dynamic values for num active and total nodes. Note that uncaptured objects will automatically appear on your script's output. Then, if this is your scripts only output, you can do this:
$obj = .\MyScript.ps1
$obj.NumActiveNodes
9
$obj.TotalNodes
10
It will make it nicer for those consuming the output of your script. In fact the output is somewhat self-documenting e.g.:
C:\PS> .\MyScript.ps1
NumActiveNodes TotalNodes
-------------- ----------
9 10
P.S. When did StackOverflow start sucking so badly at formatting PowerShell script?
If you don't want to change the script ( and assuming only that $avail_count/$total_count line is written by the script), you can do:
$var= powershell .\MyScript.ps1
Or just drop the write-host and have just $avail_count/$total_count
and then do:
$var = .\MyScript.ps1
you could just do a $global:foobar in your script and it will persist after the script is closed
I know, the question is a bit older, but it might help someone to find the right answer.
I had the similar problem with executing PS script with another PS script and saving the output into variable, here are 2 VERY good answers:
Mathias
mklement0
Hope it helps!
Please up-vote them if so, because they are really good!