I am trying to save the response of a query made through SQLPLUS and save it in a local variable, but when i execute the following code, i get the path as output instead of the value of the query, could u please help me? I don't know what am i doing wrong:
#!/bin/bash
SQLPLUS="<Path to sqlplus> -s user/passwd"
X=$SQLPLUS<<EOF_SQL_1
set heading off;
select table1 from table 2 where parameter ='Properties';
exit;
EOF_SQL_1
echo $X
The result of this script is " -s user/passwd" when it should be the esult of the query I made.
Please tell me what am I doing wrong :S
using heredoc and command substitution in the same command would not be recommended, the easier is to use a function
fun_sql() {
sqlplus user/passwd#tnsname <<EOF
...
EOF
}
X=$(fun_sql)
Related
I have a *.sql script file and there are some PROMPT commands in this file which force user to type in something.
I would like to execute this script file with sqlplus and supress the prompt question somehow.
Is there any way to supress the question and substitute its value with a pre-defined variable?
This is my test code:
set define on
set define $
SET VERIFY OFF
SET HEADING OFF
DEFINE semaowner = "hello" (CHAR);
accept semaowner prompt "schema owner: "
select '$semaowner' semaowner from dual;
quit;
And the way how I execute it:
sqlplus sys/ora123#host:port/schema as sysdba #prompt-demo.sql
But it does not work because the prompt appears either the DEFINE command is applied or not.
You could supply the response to the prompt from the command line, wrapped in a shell script/batch file if necessary:
echo schema_name | sqlplus sys/ora123#host:port/schema as sysdba #prompt-demo.sql
That works on Windows and Linux.
So you can do that in dev with (presumably) a fixed known value; and in prod just run it as you were before and have to manually enter the value.
You will still see the prompt text in dev, but it won't stop and wait for input.
If you have multiple prompts you could use multiple echos:
(echo schema_name && echo something_else) | sqlplus ...
which also works in both; in Linux you could also use a single print statement with embedded newlines:
print "schema_name\nsomething_else\n" | sqlplus ...
or a heredoc:
sqlplus sys/ora123#host:port/schema as sysdba #prompt-demo.sql <<!EOF
schema_name
something_else
!EOF
but that doesn't help you on your Windows dev box. (There may be a heredoc equivalent on Windows but I think it's basically rearranging the echos...)
The following groovy script does nothing:
def cmd = /sqlcmd -S 127.0.0.1\MSSQLSERVER -d LocalDevelop10DB -Q "DELETE FROM T_TimeRegistration WHERE TimeRegLineNr > 36"/
cmd.execute()
While this groovy script works perfectly:
def cmd = /sqlcmd -S 127.0.0.1\MSSQLSERVER -d LocalDevelop10DB -Q "DELETE FROM T_TimeRegistration WHERE TimeRegLineNr = 37"/
cmd.execute()
I want to use the (effects of) first script. It seems the '>' character is somehow not supported, I tried escaping it but no joy. What am I missing, can someone help?
Thanks
I don't know Groovy at all, but if you want to avoid the greater than symbol, you could use between:
DELETE FROM T_TimeRegistration
WHERE TimeRegLineNr between 37 and 2147483647
2147483647 is maximum int value
Do you have any error when your run the script.
Try to run the query (DELETE FROM T_TimeRegistration WHERE TimeRegLineNr > 36) from SSMS and see if it works, or see why it doesn't work.
Maybe you have some fk restrictions and one of rows is refered in other table.
I know how to execute stored procedure by single command line
echo execute some_procedure | sqlplus username/password#databasename
But I am stuck how to pass IN parameter in procedure, actually My procedure taking two parameters.
I tried this but not working
echo execute some_procedure(123,234) | sqlplus username/password#databasename
It will be great if someone can help me on the same.
With what you've shown, you either need to escape the parentheses:
echo execute some_procedure\(123,234\) | sqlplus username/password#databasename
Or enclose your command in double-quotes:
echo "execute some_procedure(123,234)" | sqlplus username/password#databasename
Either will stop the shell trying to intepret the parathenses itself, which would give you an 'syntax error: '(' unexpected or similar error. It's nothing to do with Oracle really, it's just how the shell interpreter works, before it gets as far as piping the echoed string to SQL*Plus.
Incidentally, I'd generally use a heredoc for this sort of thing, and avoid putting the credentials on the command line so they aren't visible via ps; for example:
sqlplus -s /nolog <<!EOF
connect username/password#databasename
execute some_procedure(123,234)
exit
!EOF
I know how to setup a variable in a cmd file that passes the variable to SQL via sqlcmd.
Example:
sqlcmd -Usa -Ppass -d MASTER -v num="%num%" -i C:\scriptfile
My question is how can I define a variable in SQL that can be read outside of SQL. I know how to declare and define #variables in a SQL script but those are not recognized outside of when the SQL script runs.
My question is how to you pass a variable from SQL back to cmd?
Is there anyway to accomplish this?
Thank you
You can do this using scripting variable and using the -v option of SQLCMD utility. A small example from MSDN Documentation
Consider that the script file name is testscript.sql, Col1 is a scripting variable; your SQL script look like
USE test;
SELECT x.$(Col1) FROM Student x WHERE marks < 5;
You can then specify the name of the column that you want returned by using the -v option like
sqlcmd -v Col1 = "FirstName" -i c:\testscript.sql
Which will resemble to below query
SELECT x.FirstName FROM Student x WHERE marks < 5;
EDIT:
If you just want to capture the output from your script file then you can use -o parameter and specify a outfile like
sqlcmd -v Col1 = "FirstName" -i c:\testscript.sql -o output.txt
Thanks Rahul, you inadvertently answered my question. You can output your script results to a file via the -o option for SQLCMD.
Thinking about that I realized I could use the PRINT SQL to create a -o .cmd file that contains the .cmd syntax to define a variable. Then in the .cmd file I tell it to run the SQL created .cmd file and then the variable gets defined in the .cmd environment.
Kind of a round about way but works!!
Thanks!
If you just run a simple select to get your value or an exec spname that returns just the value you are after, you can use the following.
for /f "tokens=*" %a in ('sqlcmd -Usa -Ppass -W -h -1 -d MASTER -Q "select Column from table"') do set ResultVariable=%a
Remember to use %%a if putting this in a bat file
I very new to ksh script but I have 2 ksh scripts each of them calling sybase stored procedure via isql. The issue I'm seeing is that when I execute the first script the stored procedure runs fine but when I execute the second it fails with error of (isql -b -S value -U value -P value: not found). Here are code snipets
Values for $SERVER, $DBO_USER and $DBO_PASSWORD are set earlier in the script.
test1.ksh:
ISQL_CMD="isql -b -S ${SERVER} -U ${DBO_USER} -P ${DBO_PASSWORD}"
VAR=`${ISQL_CMD} << EOF
set nocount on
go
set proc_return_status off
go
declare #var_id int
,#rtnval int
exec #rtnval = DB_NAME..MY_STORED_PROC_1
#parameter1 = ${VAR_IN}
,#parameter_output = #var_id output
go
EOF`
This executes fine and I get value in VAR variable
test2.ksh (VAR variable gets passed in from test1.ksh):
ISQL_CMD="isql -b -S ${DSQUERY} -U ${DBO_USER} -P ${DBO_PASSWORD}"
RETURN_VALUE=`${ISQL_CMD} << EOF
set nocount on
go
declare #rtnval int
exec #rtnval = DB_NAME..MY_STORED_PROC_2
#var_id = '${VAR}'
go
EOF`
I get the following error:
isql -b -S value -U value -P value: not found
These scripts can be run independent of each other so there is no guarantee that isql may have been called before test2.ksh and that is why I set the the ISQL_CMD variable in each script.
test1.ksh runs properly but test2.ksh does not, whether called from test1.ksh or run on it's own. Tried running the scripts in debug, but it didn't really provide any further information.
Ok I have figured this out after a full day of head scratching. test2.ksh is performing some file processing and setting the IFS value several lines prior to isql command. I had to reset or unset the IFS once I was done and isql command worked fine! the command to unset the IFS value is:
unset IFS
Thanks for those that were trying to help!!