INSERT SQL query with Batch File only - sql

Is there a way of connecting and inserting data to a local SQL Server table through a batch file.
The table looks like on the database , ID | compName | availDiskSpace
For example, this would hold the local computer name and available space on the machine.
#echo off
set computerName = computer123
set diskSpace = 2GB
sqlcmd -S server1\SQLExpress
Is it possible to insert these variables to the SQL table with an INSERT, I know i can use SELECT to select data but cantfind anywhere that shows INSERT
.....Thanks

Assign variables using the -v argument for sqlcmd. For instance:
sqlcmd -S "server1\SQLExpress" -v diskSpace = "2GB" -v computerName = "Computer123" -q "insert into myTable (diskSpace, computerName) select '$(diskSpace)', '$(computerName)'"
You declare variables using -v and you reference them using $(<VariableName)
More detail on SQLCMD here: https://learn.microsoft.com/en-us/sql/tools/sqlcmd-utility
I believe all that should be fine to put in a batch script, but I imagine you can also set batch variables (as you did) and assign those to be the values of the sqlcmd variables.
FWIW, this is probably considerably easier to do with SSMS and/or a stored procedure.

Related

Extract a paragraph from a text file using batch and store it into a sql table

Im having a text file and hope to just extract one short sentence from the file and store it in to sql using batch. I am new to bash and does not know do i need to start it. Extract it with /f function or write function and store into a variable and insert the variable into sql table?
You can simple grep the line into variable and than use this variable in your SQL syntax like below:
#!/bin/bash
variable=$(grep -c "some text" /some/path);
echo "INSERT INTO some_table (col1, col2) VALUES ('some_value', '$variable');" | mysql -uroot -ppass db_name;

SQL Server commands in string

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.

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!

How to run select statment using batch file?

I need to query sql server database using batch file. I put these cmdlines in the batch file. When I run the batch file. Cursor stays there after making trusted connection.
OSQL -E
use db1
SELECT count(*) FROM table_01 t1
left join table_02 t2 on t1.tableID = t2.tableID
WHERE t1.Date < '20110724'
Go
Any suggestions please?
Here's how I do it.
First, build the SQL script that you want, and store it as a simple text file.
Next, use SQLCMD (or OSQL or, perish the thought, ISQL) to call that file, something like so:
SQLCMD -S %1 -E -b -h-1 -I -d tempdb -i BulkDeploy.txt > BulkDeploy_%DateString%.txt
Where:
S specifies the SQL instance server (here, specified with the first batch parameter)
E use NT authentication
b if SQL hits an error, return a value that the batch ERRORLEVEL can pick up and process
h-1 return no header rows (IF datasets are returned)
I set QUOTED_IDENTIFIER on (this blew up in my face once, I forget how or why, and I've included it ever since)
d database to connect to
i execute the following script and exit when done
> directs any output to the specified file for subsequent processing
SQLCMD et. al. have many parameters, check them out in Books Online. Further subtleties can be achieved with batch parameters.
osql has a simple fature.
For example I run an SQL command from e:\backupdb.txt with
osql -S servername -U user -P password -i e:\backupdb.txt
it does the job

Passing parameters to Oracle SQL file from Batch file

I have a SQL file that updates certain table. However, the table name depend on the machine name on which the software is installed. (Something like: TableFooBar-MyMachine). I have to write a batch file that calls an Oracle SQL script which will update this table.
So, BatchFile --> Update. Sql --> TableFooBar-MyMachine
The Update. Sql will have statement like:
Update TableFooBar-<Machine-Name> where fooid = -99;
This batch file needs to run on many machines. There are actually many update statements on such tables. I do not want people to edit the sql files. Instead if I set the machine name in the batch file and pass it to the sql, I'm pretty much done!
How do I pass the machine name parameter to the .sql file to achieve this?
you can use substitution variables
update.sql
--
Update TableFooBar-&1 set column_foo='bar' where fooid = -99;
--
and then call
sqlplus foo/bar#db #update.sql <Machine-Name>
Yes, you can do this, by creating the SQL file from the BATCH file.
It would look like this:
#echo off
set SQL = update.sql
ECHO connect username/password#database
ECHO Prompt Updating tables > %SQL%
ECHO Update TableFooBar-%1 where fooid = -99; >> %SQL%
sqlplus #update.sql