How to display the result of SQL query in unix server (putty)? - sql

Suppose I want to display the result of a simple SQL query, say SELECT * FROM dual; that is executed in SQL developer and get the result in unix server using a simple command. I do not want to use scripts to get the result. Hoping to use a single line command. I already tried using the isql command, but that is not working.
echo "select * from dual" | isql -v dbname userid hostname
Please help me.

Related

How can i export the result set of an SQL Server stored procedure from one server into a table on another server?

I want to export the result set of an SQL stored procedure on one server into a table on another server. Is there a way I can do this?
Please help.
Sometimes using a Linked Server is also an option (I try to avoid them for several reasons), especially if you have enough permissions and if you need to do this as a one off. Then your code would simply look like:
INSERT INTO somelinkedservername.somedatabasename.dbo.sometable
EXEC dbo.thesproc
But pending on the size of the resultset I prefer to use BCP and a fileshare to keep it simple and put the code in a SQL Agent job for overview:
bcp.exe "EXEC [AdventureWorks].[dbo].[uspGetEmployees] #managerId = 666" queryout "\\SomeShare\Temp\emps.txt" -ServerA -T -c
And then
bcp.exe "\\SomeShare\Temp\emps.txt" in -ServerB -T -c
Server Objects > Linked Server > Right Click > New Linked Server
Data transfer= SELECT * FROM [Server Name].[Database Name].[Schema Name].[Table Name]
You can do something like this:
execute ('EXECUTE DatabaseName.Schema.ProcedureName #Parameter1=?,#Parameter2=?',#Parameter1Value,#Parameter2Value) at [ServerName]

sql query in shell script #xyz.sql

Can anyone please tell me the exact meaning/significance of #forecast2.sql in below query , the given query is a part of one shell script. Also how can I find the exact sql query executed by #forecast2.sql
Below is the code:
sqlplus -s / #forecast2.sql $SCHED_ID > /tmp/BR_forecast
I need to find the sql query that's been executed here.
This means that sqlplus will execute script called forecast2.sql.
You could locate it:
> env | grep SQLPATH
-- path where scripts are located

How do I run more than one sql query (from the psql interactive terminal)?

New to to psql and slightly confused.
I executed the query "select * from users" and got the following result:
However, I get "END" after I execute the query.
How do I go about executing subsequent queries? (Or exit psql etc.)
press q . It quits the viewer.

SQL script cshell

I am doing this in my cshell script sqlplus $ORA_UID/$ORA_PSWD #${SQL}test.sql ${DATA}${ext1}thats trying to get output from test.sql script.. in my sql script i am dumping output to spool &1 .. but when i run my script my files are blank i am not getting anything from database.. can someone tell what wrong with this
normally it is not a good idea to have your userid and password displayed in the processlist, as is happening now. Most of the times when sql scripts don't produce the expected output it is because the end of SQL marker is missing. Default the end of SQL is the ';' Reading the end of SQL marker actually starts the SQL statement.
First try the script with feedback on and check the error message in the spoolfile.
Is the spoolfile location OK ?
sqlplus /nolog <<eof
connect $ORA_UID/$ORA_PSWD
#${SQL}test.sql ${DATA}${ext1}
eof
This construction prevents the display of the credentials on the processlist.
In the sqlscript could be
select * from dual;
or
select * from dual
/
but each and every SQL statement has to have an end of SQL marker.

How to indicate in postgreSQL command in which database to execute a script? (simmilar to SQL Server "use" command)

I have the following problem, I need to put in a script that is going to run before the new version is rolled the SQL code that enables the pgAgent in PostgreSQL. However, this code should be run on the maintenance database (postgres) and the database where we run the script file is another one.
I remember that in SQL Server there is a command "use " so you could do something like:
use foo
-- some code
use bar
-- more code
is there something similar in PostgreSQL?
You can put in your file something like:
\c first_db_name
select * from t; --- your sql
\c second_db_name
select * from t; --- your sql
...
Are you piping these commands through the psql command? If so, \c databasename is what you want.
psql documentation
You can't switch databases in Postgres in this way. You actually have to reconnect to the other database.
PostgreSQL doesn't have the USE command. You would most likely use psql with the --dbname option to accomplish this, --dbname takes the database name as a parameter. See this link for details on the other options you can pass in you will also want to check out the --file option as well. http://www.postgresql.org/docs/9.0/interactive/app-psql.html
well after looking on the web for some time I found this which was what I need it
http://www.postgresonline.com/journal/archives/44-Using-DbLink-to-access-other-PostgreSQL-Databases-and-Servers.html