Issues running scripts through osql.exe - sql

I try and run update scripts for my software in this format:
osql.exe -i "path\to\script" -U "user" -P "Password" -S "Location of sqlserver" -d "Database name" -n -b
Most of the scripts are in the same format and all end in GO. A lot of them run just fine, but all the time random scripts return an error and won't run. The error is "Incorrect syntax near '∩'. on line 1. The script might be as simple as just an INSERT, but it is always this error. I can't seem to find anything online that has been able to help me. Can anyone provide any insight?
The scripts run just fine manually. Also something interesting is if I create a new text document and paste the script in the new file and change it to the .sql and run that file then it works just fine. I'd just do this for all the 'broken' scripts, but it continues to happen to new ones and will happen on changed ones as well eventually.

Most likely because the file is encoded as Unicode instead of UTF-8. You can check this out in Notepad++ among other free utilities. Try converting it to UTF-8 and see if that helps.
UPDATE
Correction: As the article linked in comments explains, osql can parse text files encoded as UTF-16 (Unicode 1200) or 'ANSI' (Windows-1252), but it cannot parse UTF-8 encoded files.

It does sound like a Unicode issue (particularly since copy/paste to a new document works). To test that, you can use type and redirect to a temporary file which will force it to ANSI like so:
type \path\to\script.sql > %TEMP%\newscriptname.sql & osql.exe -i "%TEMP%\newscriptname.sql" -U "user" -P "Password" -S "Location of sqlserver" -d "Database name" -n -b

Related

what cmd command I can use ANSI formatting for my OSQL output text file?

I am running OSQL command in .bat file and I am running my SQL script and generating the output in a text file, however the data in the file is scattered and I want to generate the file in the same format as i can generate via SQL Server manually.
I have also tried with .rpt and .txt format but still facing the same issue.
So if anyone can suggest any command or any alternative way, so that I can generate the file same as I am generating manually through SQL server.
Any suggestions or idea is appreciated.
Thanks.
You can use some options like -s "," to separate your columns by a comma. In my following example I am executing a store procedure from command line. e.g :
osql -E -Q "SET NOCOUNT ON;EXECUTE REPORTDB.[DBO].[SP_LOAD_USERDATA] %date%" -o C:\ADELPHE\OUTPUT_FOLDER\_%date%.txt -w 700 -s","
Have a look on this manual.

How to use iTunes Connect Transporter

Is there anyone that can explain to someone that doesn't know how to use Terminal what are the commands to use Transporter for iTunes Connect?
I tryed to follow the guide but with no results....
These are my steps till now:
I put this command in terminal:
export TRANSPORTER_HOME=`xcode-select --print-path`/../
Applications/Application\ Loader.app/Contents/MacOS/itms/bin
and my terminal change like this:
~ myname$ Applications/Application\ Loader.app/Contents/MacOS/itms/bin
so I guess with this now I am in the transporter folder...
Now I want to etrieve my app’s current metadata Using Lookup Mode, and I tryed with this command:
$ iTMSTransporter -m lookupMetadata -u [myname#gmail.com] -p [mypassword] -vendor_id [id999999999] -
destination [Applications/Application\ Loader.app/Contents/MacOS/itms/bin]
but I get this:
$ iTMSTransporter -m lookupMetadata -u [myname#gmail.com] -p [mypassword] -vendor_id [id999999999] -
-bash: Applications/Application Loader.app/Contents/MacOS/itms/bin$: No such file or directory
I assume I'm writing the destination in a wrong way....
So how should I write that command?
And also... when I will have to upload my edited file... what shoud I put?
Thanks a lot for any help with this issue
Start by putting the export command into a single line.
export TRANSPORTER_HOME=`xcode-select --print-path`/../Applications/Application\ Loader.app/Contents/MacOS/itms/bin
Then you have to use the full path to the iTMSTransporter Binary. You can use the variable you just defined for this.
"$TRANSPORTER_HOME/iTMSTransporter" -m lookupMetadata -u ... -vendor_id ... -destination ~/myapp
The destination is the directory where the app data will be put. ~ means your user directory. So if your username is blue ~/myapp means /Users/blue/myapp.
Don't use Xcodes directory for this.
I would recommend to NOT specify your password with the -p parameter. You don't want your password to appear in bash_history. If you don't specify the passwort you will be asked for it.
Again. Make sure that this is in one line. You must not spread the command over more than one line. Unfortunately if you copy and paste from the pdf document you get a multi line command that won't work.
I suggest to open a text editor, paste the command from the pdf into the text editor and format the command so it is on a single line.
Then go to https://bugreport.apple.com and file a bug about the crappy documentation of iTMSTransporter

T-SQL - Scripting variable not defined

I'm new to T-SQL and I'm trying to backup my databases (using SQL Server 2008).
When I try to run the script via sqlcmd -i inputfile I got this error messages:
'DATE' Scripting variable not defined.
The problem is I have a line like this:
...TO DISK = "FileName_$(ESCAPE_NONE(DATE)).BAK" ...
With a date in a filename, it will prevent it from replacing my old backups.
If I run it in management studio, it works, but if I run it in command line with the sqlcmd -i command, then it doesn't work.
EDIT:
I looked at the job history and I saw this error message:
"For SQL Server 2005 SP1 or later, you must use the appropriate ESCAPE_xxx
macro to update job steps containing tokens before the job can run"
I don't quite understand what that means. I've already used $ESCAPE_NONE(DATE), what's wrong?
Old question I know but this is one of the first results and if anyone else has the same problem the answer isn't particularly easy to find.
Including the -x switch to disable environment variables fixed the problem for me;
sqlcmd -x -i inputfile
If you're trying to backup your sql server databases and append the date to them using sqlcmd there's an easy thing you can try.
First, create the sp called sp_BackupDabases which you can find here:
http://support.microsoft.com/kb/2019698
You can invoke it from sql cmd using some command like this:
sqlcmd -U Damieh -P ilovechocolate -S (local) -Q "EXEC sp_BackupDatabases #backupLocation ='C:\MyBackups\', #BackupType='F'"
I'm sure you know this already, but just in case: -U is the user, -P is password, -S is server, and -Q is query. You can either backup all of your databases or some of them, there are parameters for that. You can find the stored proc parameters details on the same link I gave you.
The date will be automatically appended and you can play with the sp's code if you want it in a different place/way/format. I use this regularly on servers which don't have a non-express sqlserver (meaning that I can't schedule backups without using a .bat and task scheduler) with great success.
I apologize if this wasn't the answer you were looking for =). Have a nice day!
I know I'm coming along late on this thread, but you can use the following:
SQLCMD -S YourServer -E -d YourDatabase -i YourScript 2> nul
That will send the StdErrorOut to the bit bucket.

Vaccum full and Reindex Heroku database

I want to perform a full vacuum and reindex on my database for my app hosted on Heroku.
I can't work out how to do it via the heroku command line remotely.
I can do it on my local Mac osx machine via the below commands in terminal...
psql database_name
>> vaccuum full;
>> \q
reindex database database_name
How can i perform a full vaccuum and reindex all my tables for my app on Heroku?
If possible I would like to do it without exporting the database.
Okay so it seems Heroku doesn't support this functionality unless you pay up. Looks like i'll have to pull the database, perform the actions and push it back upstream! Fun times.
You can use the psql interactive terminal with Heroku. From Heroku PostgreSQL:
If you have PostgreSQL installed on your system, you can open a direct psql console to your remote db:
$ heroku pg:psql
Connecting to HEROKU_POSTGRESQL_RED... done
psql (9.1.3, server 9.1.3)
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
Type "help" for help.
rd2lk8ev3jt5j50=>
You can also pass-in the parameters at the psql command-line, or from a batch file. The first statements gather necessary details for connecting to your database.
The final prompt asks for the constraint values, which will be used in the WHERE column IN() clause. Remember to single-quote if strings, and separate by comma:
#echo off
echo "Test for Passing Params to PGSQL"
SET server=localhost
SET /P server="Server [%server%]: "
SET database=amedatamodel
SET /P database="Database [%database%]: "
SET port=5432
SET /P port="Port [%port%]: "
SET username=postgres
SET /P username="Username [%username%]: "
"C:\Program Files\PostgreSQL\9.0\bin\psql.exe" -h %server% -U %username% -d %database% -p %port% -e -v -f cleanUp.sql
Now in your SQL code file, add the clean-up SQL, vacuum full (note the spelling). Save this as cleanUp.sql:
VACUUM FULL;
In Windows, save the whole file as a DOS BATch file (.bat), save the cleanUp.sql in the same directory, and launch the batch file. Thanks for Dave Page, of EnterpriseDB, for the original prompted script.
Also Norto, check out my other posting if you want to add parameters to your script, that can be evaluated in the SQL. Please vote it up.

Run Mysql scripts in a batch?

Can someone link me to a tutorial or explain if there is a way to create some sort of batch file of mysql scripts / stored procs and run them all at the same time? I can not seem to find any documentation on this online but I feel that I might be searching using the wrong terms.
You can chain mysql scripts by calling them from within a script using the source command (details of command line options)
# my_textfile.sql
# ---------------
USE my_database;
\. subscript1.sql
\. subdir/subscript2.sql
\. /full/path/to/subscript3.sql
Command Line:
mysql < my_textfile.sql
Don't forget the command line options, if you are going to script the files you might need the password/ user account.
mysql -uyouraccount -pyourpassword YourDatabase < mytextfile.sql
This isn't the most secure way to do it because it puts your username/ password on the command line but it works. If you are doing much scripting I suggest you look into .my.cnf and the various options for saving your account/ password in there (and securing that file).
You can simply create a text file with SQL statements separated with ; and then execute all statements with the MySQL command line client:
# my_textfile.sql
# ---------------
USE my_database;
SELECT * FROM table1;
UPDATE table2 SET foo='bar';
Command Line:
mysql < my_textfile.sql
For peeps running MAMP PRO on OS X Yosemite, I was able to get all my *.sql scripts executed (import) by running from terminal:
/Applications/MAMP/Library/bin/mysql -h localhost -u root -p < /Applications/MAMP/myDBRestore.sql
myDBRestore.sql contained a reference to all the MySQL DB scripts as thus:
\. /full/path/to/sql/file1.sql
\. /full/path/to/sql/file2.sql
\. /full/path/to/sql/file3.sql
...
\. /full/path/to/sql/file(n).sql
where n is the last .sql file in the directory.