SQL Server commands in string - sql

I need a command in a .sql file for SQL Server 2012 that lets me run strings as commands, like:
set #command= 'create table mytable (...);';
run(#command);
Preferably with some kind of string format for strings and/or numbers. Usually I do this in C# but I was wondering if I can keep it all in a .sql file.

Yes you can, save your SQL command in a *.sql file say test.sql and use SQLCMD Utility to run the sql file in a command prompt like below
sqlcmd -S <ComputerName>\<InstanceName> -i test.sql -o result.txt
Not sure but if you mean running the *.sql file in your c# code behind; then I would suggest, create a stored procedure and pull up your SQL queries in that stored procedure. Then you can simply call that procedure in your application code instead of reading the SQL file and running them one by one.

Related

Using SQL*Plus from command line

I have a small vbscript to load data and process it using sqlldr and sqlplus. I have 2 questions about sqlplus usage though:
1) Can I exec a stored procedure without using an .sql script file?
e.g. sqlplus user/pass#server #exec proc_myname
2) Can I use .sql script files on a shared UNC path?
e.g. sqlplus user/pass#server #\server\path\script.sql
I've tried playing around and at the moment am working around the problem by using a local temp directory to store the sql files. But I'm curious if there's another/better way.
Thanks
Can I exec a stored procedure without using an .sql script file?
Yes. I don't know VisualBasic good enough but the basic principle is that you create the child process for sqlplus and then send the commands via stdin (i.e. you have your script write to standard input of the child process).
Can I use .sql script files on a shared UNC path?
If the path is correct, then that should work. You can also try I/O redirection:
sqlplus user/pass#server < \\server\path\script.sql
The drawback of this approach is that the error messages won't include the .sql script name.
Re your second question:
Running a script from a UNC path works for me without drive mapping or I/O redirection:
sqlplus /nolog "#\\server\path\file.sql"
sqlplus version 12.2.0.1
The quotes and '#' are important, apparently.

Is it possible to create a batch script with sql commands using cmdsql?

I basically want to create a batch script that has embedded sql commands and I was wondering if there is a way to do this using cmdsql. I'm using sql server 2008 r management studio and I've downloaded sqlcmd v2.0.
I made a batch script which attempted to connect to a database and execute a simple select statement, but when I ran the script it went into interactive mode after connecting to the database. It wouldn't execute the sql in the script, it would only allow a user to type in sql commands. The code is below:
sqlcmd -S <servername>\<instancename>
Select Number FROM Table1
GO
I changed the column/table/database etc. names as this is work-related but you get the idea. I'm quite new to batch scripting and don't have much experience, I have more experience with sql.
You could try to read the documentation. A synopsis of the documentation is available from the command line by typing sqlcmd -?
To run a single SQL-Server query from within a batch file, using the default database:
sqlcmd -S <servername>\<instancename> -Q "Select Number FROM Table1"
The standard way to feed input into a program is preparing the input and redirecting it via a | pipe. For example:
(
echo Select Number FROM Table1
echo GO
echo . . .
echo EXIT or QUIT or BYE...
) | sqlcmd -S <servername>\<instancename>
However, if the purpose of your Batch file is just to execute sql commands (and have no Batch logic), an easier way is to prepare a .txt file with the same input you would type via the keyboard:
sqlcmd -S <servername>\<instancename>
Select Number FROM Table1
GO
... and then feed that file into cmd.exe this way:
cmd < theFile.txt
In this case, don't forget to insert both the exit command for sql AND the exit command for cmd.exe!

different command line used to extract tables from an sql server file into one that is usable by mysql

What is the difference between these two command line used to extract tables from a database into one that can be used by mysql ?
C:> mysql -u user -p PASS database_name < ms.sql
And
mysql> source ms.sql ;
I used to do with the former and the database created contained all information but it didn't work. the second worked fine.
Second in the first case setting default character set is examplified but I found none in the homepage of the mysql an example for the second case. I am thankful for any help available.
Both of the commands can be referred as Batch Commands. I am pointing out the difference between them below.
First Command
mysql -u user -p PASS database_name < ms.sql
The above command is executing two commands at a time. One is to loggin to MySQL and other one is, passing the script file to execute using OS I/O Operator '<'.
After execution of this command it will display the sql result of the script and comes back to command prompt.(comes out of SQL
Prompt)
It is necessary to keep USE DB_name command in the begening of file to execute the script
This way is useful when you want to execute a big script without logging into mysql typically most often used.
Second Command
mysql> source ms.sql;
The above command is generally an SQL Command which will execute the script present in sql file.
It is used if you are already in MYSQL Prompt. After executing the script it will return back to Mysql prompt only
You may also use this command like executing the shell script something like mysql> ./filename
For more information please refer MySql Reference Link: https://dev.mysql.com/doc/refman/5.7/en/mysql-batch-commands.html

How do I import a sql data file into SQL Server?

I have a .sql file and I am trying to import it into SQL Server 2008. What is the proper way to do this?
If your file is a large file, 50MB+, then I recommend you use sqlcmd, the command line utility that comes bundled with SQL Server. It is easy to use and it handles large files well. I tried it yesterday with a 22GB file using the following command:
sqlcmd -S SERVERNAME\INSTANCE_NAME -i C:\path\mysqlfile.sql -o C:\path\output_file.txt
The command above assumes that your server name is SERVERNAME, that you SQL Server installation uses the instance name INSTANCE_NAME, and that windows auth is the default auth method. After execution output.txt will contain something like the following:
...
(1 rows affected)
Processed 100 total records
(1 rows affected)
Processed 200 total records
(1 rows affected)
Processed 300 total records
...
use readfileonline.com if you need to see the contents of huge files.
UPDATE
This link provides more command line options and details such as username and password:
https://dba.stackexchange.com/questions/44101/importing-sql-server-database-from-a-sql-file
If you are talking about an actual database (an mdf file) you would Attach it
.sql files are typically run using SQL Server Management Studio. They are basically saved SQL statements, so could be anything. You don't "import" them. More precisely, you "execute" them. Even though the script may indeed insert data.
Also, to expand on Jamie F's answer, don't run a SQL file against your database unless you know what it is doing. SQL scripts can be as dangerous as unchecked exe's
Start SQL Server Management Studio
Connect to your database
File > Open > File and pick your file
Execute it
Try this process -
Open the Query Analyzer
Start --> Programs --> MS SQL Server --> Query Analyzer
Once opened, connect to the database that you are wish running the script on.
Next, open the SQL file using File --> Open option. Select .sql file.
Once it is open, you can execute the file by pressing F5.
In order to import your .sql try the following steps
Start SQL Server Management Studio
Connect to your Database
Open the Query Editor
Drag and Drop your .sql File into the editor
Execute the import
A .sql file is a set of commands that can be executed against the SQL server.
Sometimes the .sql file will specify the database, other times you may need to specify this.
You should talk to your DBA or whoever is responsible for maintaining your databases. They will probably want to give the file a quick look. .sql files can do a lot of harm, even inadvertantly.
See the other answers if you want to plunge ahead.
Get the names of the server and database in SSMS:
Run the following command in PowerShell or CMD:
sqlcmd -S "[SERVER NAME]" -d [DATABASE NAME] -i .\[SCRIPT].sql
Here is a screenshot of what it might look like:
There is no such thing as importing in MS SQL. I understand what you mean. It is so simple. Whenever you get/have a something.SQL file, you should just double click and it will directly open in your MS SQL Studio.

Can I use TSQL to operate on normal os files? Such as create a .bat file and write some query result into that file?

Can I use TSQL to operate on normal operating system files? Such as create a .bat file at c:\test and write some query result into that batch file?
Thanks.
For general tips on reading/writing files, you can check out this link.
You can also use SQLCMD, like this (input.sql would be your input sql, Results.txt would be your output):
SQLCMD -i Input.sql -o C:\Results.txt -e
Yes use SQLCMD
You could also use xp_cmdshell:
xp_cmdshell
Executes a given command string as an
operating-system command shell and
returns any output as rows of text.
Grants nonadministrative users
permissions to execute xp_cmdshell.
link to: xp_cmdshell - msdn reference