How to get output from SQLCMD or OSQL? - sql

Looked everywhere... to no avail.
I am trying to do a basic select using SQLCMD from the command line:
sqlcmd -S myServer -d myDB -E
So far so good.
select * from myTable
Nothing, just goes to the next line. Shouldn't it display a table with values ? Or at least "n row(s) returned" ?
I also tried the -o param: it creates an empty file.

When you use the SQLCMD tool in interactive mode statements that you enter are sent to the server when you use the keyword GO.
GO signals both the end of a batch and the execution of any cached
Transact-SQL statements. When specifying a value for count, the cached
statements will be executed count times, as a single batch.
See Use the sqlcmd Utility specifically the section titled Running Transact-SQL Statements Interactively by Using sqlcmd
So in your case:
select * from myTable enter
GOenter

Related

sqlcmd SQL returning string value as #name? - value is displayed correctly in SSMS

I have a simple SQL query that is pulling the value from a varchar2 column. It returns the correct value in SSMS, however in sqlcmd it returns: "#NAME?" (without the double-quotes).
The actual value it is trying to return is "- W" (without the double-quotes)
The code I'm running is:
sqlcmd -S \\servername -d DBNAME -E -Q "select val from tab;"
Any idea what's wrong here? Potentially a character-set issue?
Software versions:
SQL Server Management Studio V17.1
sqlcmd Version 14.0.500.272 NT
Apologies. The reason I got different answers on sqlcmd vs SSMS is because I was connected to different databases.
Let's close this thread and never speak of this again.

How to execute sql query files via RPostgreSQL

I am accessing my PostgreSQL database (9.3) via R using the RPostgreSQL package.
I have a few very long and big sql queries (several MB big. generated from raster2pgsql).
How can I send / execute sql query files as statement within R?
The normal way
\i query.sql
does not seem to work via dbSendQuery.
I tried to read in the whole sql file as character vector via readLines, however this also fails, because dbSendQuery only supports a single command apparently?
dbSendQuery or dbGetQuery is just for the "SQL" part, not the psql commands such as \i.
In your case the simplest is indeed to use readLines but then wrap dbGetQuery in a sapply call.
con <- dbConnect(...) #Fill this as usual
queries <- readLines("query.sql")
sapply(queries, function(x) dbGetQuery(con,x))
dbDisconnect(con)
Since I use this very often, I have a shortcut for this in my .Rprofile file:
dbGetQueries<-function(con,queries)sapply(queries,function(x)dbGetQuery(con,x))
Of course, you can also go the system way:
system("psql -U username -d database -h 127.0.0.1 -p 5432 -f query.sql") #Remember to use your actual username, database, host and port

Suppress 'Use' command output while running MSSQL Server script

I have a basic sql script that I am executing via sqlcmd. The first line in the output is the Use command to specify the database. I'm writing the results out to a text file, but don't want to include the response from the 'use' command, just the query. In other words, I don't want to see "Changed database context to 'database_name'." at the top of the output. Is there a way to suppress this output?
Instead of:
USE someDB
GO
SELECT * FROM dbo.SomeTable
You could try:
SELECT * FROM someDB.dbo.SomeTable

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.

execution time of a stored procedure

What is the age old method of testing execution time of stored procedures on informix 11.5. I am thinking of doing something like this from a unix prompt:
$ time (echo 'execute procedure foo(1)' | dbaccess ...)
Any other ideas?
Sure, you can do something more elaborate, but if that's all you need, why bother? Obviously if there are more steps, move the sql into a separate file and run
time dbaccess <dbname> file.sql
btw, there's a quote missing from your code fragment.
I use my SQLCMD program for this sort of job. It has a benchmark mode (-B option), and also makes it easier to write the SQL:
sqlcmd -d stores -B -e 'execute procedure foo(1)'
It is open source and available from the IIUG Software Archive.