how to access DB from command prompt to write SQL queries? - sql

how to connect to DB from command prompt in windows 7 for the purpose of writing SQL queries and how to access all SQL tables and files?

You can use Oracle SQL*Plus
Which is an interactive and batch command-line query tool that is installed with Oracle Database Express Edition.
For MSSQL you may use the sqlcmd Utility.

For connect to SQL you can use osql.exe tool, which allow you to connect to database, write queries, and even administering.

Related

How to Execute SQL Server query (procedure) from another PC in network

Situation now
We have a netrowk set up. Some PCs have SQL Server 2012 installed, some don`t.
Is it possible to write a stored procedure and have it invoked/executed by another user/PC without SQL Server? For example by some .bat/.cmd script, I don`t know.
Desired situation
I write SQL query on my SQL Server management studio.
I save the query as a stored procedure. <-- up to here I am OK.
I do something that makes the procedure availible to other users. The advice I get from this question (I hope).
Other colleagues execute the procedure.
They get a result of the query in a CSV.
The client computers that need to run the stored procedure could install the sqlcmd utility. This allows for the execution of sql commands on another SQL Server.
SQLCmd -S<<SERVERNAME\INSTANCENAME>> -Q "Execute dbo.YourStoredProcedure"
And then have your stored procedure save the CSV file to a network drive or send it via e-mail.
Another way to retrieve data could be using the BCP utility. In this way the user could connect to your server, execute the query and receive a CSV file to a specified location on their computer.
In both cases it involves installing an additional program on the client computer.
You don't need SQL Server installed in the client machines to run queries or execute procedures. At most, depending on which programming language you are are using, you need the client installation for SQL Server. This is true for any DBMS, even Microsoft SQL Server 2014.
If you implement you code in Java, for example, you don't need even the MS SQL Server client. The JDBC driver for MS SQL Server is enough. It can be download from Microsoft and is not part of the SQL Server client installation.

Issuing Teradata SQL Assistant query via script

I use Teradata SQL Assistant to run SQL queries against a DB2 database accessed via an ODBC connection. This is an entirely interactive process whereby I first start the SQL Assistant app, then connect to the correct data source and finally write and execute my query.
What I would like to do is to be able to achieve the same result, i.e. get the result set from a query, but via some sort of script, which would connect to the the data source and run my query.
Is this possible?
Yes this is possible. Install the IBM Data Server Client software appropriate for your version of DB2, then use the DB2 command line processor with the -f option, as described in the manual.

backing up sql database from microsoft sql server

i just want to ask if how can i backup my database from sql server 2005 using sql server management studio express? i want a backup it using sql file (.sql and not .bak or .mdf) from creating database (if not exist), tables and even the records on the table..thanks in advance :)
This is not possible with one command. You can script single objects, but I'm not aware of a way to do this in SSMS. We use SQL Compare from redgate here - it's pretty great.
You can try powershell.
Docs here.

Simulation of generate scripts in SQL Server 2005+ (without using Management Studio)

Is there any built-it stored procedure / any database program by which I can generate all the database object script (like stored procedures, triggers, functions, tables etc.) from a particular database?
I am using SQL Server 2005 + 2008
Thanks in advance
SSMS itself uses SMO, namely the Scripter class. You can use SMO from any .Net application to extract scripts yourself.
from .net
http://www.sqlteam.com/article/scripting-database-objects-using-smo-updated
from powershell
http://www.mssqltips.com/tip.asp?tip=1745
sqlcmd
http://www.edumax.com/microsoft-sql-server-the-sqlcmd-tool-and-sql-server-management-objects.html
In Object Explorer in Microsoft Server Management Studio rightclick on the database and select Tasks -> Generate Script and then follow the guide...

Backup DB on Remote SQL Server Express

I need to create a TSQL script to backup a db on a remote SQL Server Express (2005). I have a SQL Server 2005 on another box. Not sure how I can run the script from this SQL Server 2005 to do the backup job.
The script is something like this:
RESTORE DATABASE [myDB] FROM DISK = N'C:\Tmp\myDB.bak' WITH FILE = 1,
NOUNLOAD, STATS = 10
Actually, I tried this SQL script on the remote SQL Server Express by using SQL Server Management Studio Express and it runs OK. The reason I ask this question is that I can schedule a job on SQL Server 2005, but I cannot create a schedule job on the remote SQL Server Express.
Another way, I think, is to create a SQL SP on the SQL Server Express first. Then I'll write a simple console application to connect to the SQL and run the SP as a Windows Scheduled job.
There is no need to do this by TSQL. SQL Server (also Express) includes a utility called sqlmaint.exe, which allows you to perform backup operations on a local or remote SQL server. Simply write a batch file calling sqlmaint with the correct command line parameters (documentation) and put this batch file in Windows Scheduler.
If you still want to do it by TSQL, SQL Server also contains osql.exe, which allows you to execute arbitrary SQL statements on a local or remote server. Again, you can automate it using simple batch files.
EDIT: If you want to call the TSQL script using your own application, it might be helpful to know about your programming language or data access technology of choice.