Unicode in sqsh connecting to MSSQL (or alternatives) - sql-server

I have a MSSQL database, which contains Unicode (utf8) data. My workstation is linux (currently Ubuntu) and looking for a tool to work with mssql database I found SQSH.
The problem is - when I select data in the sqsh console I get jibberish instead of unicode characters. Using switch "-J utf8" or "-J utf-8" didn't change anything.
The question is - how to set up sqsh to work with utf-8 data?
If it is not possible, do you know any alternative tools usable from linux for work with mssql databases filled with utf-8 data. I need to execute all kinds of T-SQL, run previsously prepared SQL script files, and pipe out results for processing afterwards. A good GUI (open source) could also be used, not limited to shell clients.

Are you using freetds with sqsh? If you are, edit your freetds.conf to set the charset.
http://www.freetds.org/userguide/localization.htm

Use Azure Data Studio to avoid data troubleshooting issue. it is a great SSMS alternative for Linux.

If you need command line tool I suggest to use official sqlcmd from mssql-tools.
It is available for all major Linux distributions including Ubuntu.
Connecting with sqlcmd
Another shell tool is mssql-cli
Features
Mssql-cli is a new and interactive command line tool that provides the
following key enhancements over sqlcmd in the Terminal environment:
T-SQL IntelliSense
Syntax highlighting
Pretty formatting for query results, including Vertical Format
Multi-line edit mode
Configuration file support

I had the same problem and it seems it has nothing to do with charchters encoding, but the problem was there are control character, unprintable characters, in the script.
I removed them from the sql script and everything works fine.

Related

How I can access DB2 using batch files(.bat)

Want a syntax where I can give my DB2 details like server name,user name,password and sql file which want to execute using batch file.
I found syntax like sqlplus,sqlcmd for oracle and sql server respectively but no luck for 'DB2'.
Pls help me in it.
Note that this is about DB2 on the IBM i platform (see comments).
For Windows, from a command prompt, type db2cmd, then from there run e.g. db2 connect to YOUR_DATABASE user YOUR_USER_ID then e.g. db2 -tf filename.sql
https://www.ibm.com/support/knowledgecenter/en/SSEPGG_11.1.0/com.ibm.db2.luw.admin.cmd.doc/doc/r0002036.html
https://www.ibm.com/support/knowledgecenter/en/SSEPGG_11.1.0/com.ibm.db2.luw.admin.cmd.doc/doc/r0010410.html
IBM i Access Client Solutions (which I assume you have available to you, this part of its functionality being free of charge) has a command line option for this, e.g.
java -jar acsbundle.jar /SYSTEM=MYAS400 /PLUGIN=rss /file=\mysqlscript.sql /autorun=1
See ftp://ftp.software.ibm.com/as400/products/clientaccess/solutions/GettingStarted_en.html for more information (specifically section 9.1.31).
I think, this might help somebody in future, who is looking for solution,Just make sure you have Db2 Client installed & add following snippet in your batch file.
SET DB2CLP=DB20FADE
runas /noprofile /user:db2admin "db2cmd -c db2 create database MyDatabase && db2 connect to MyDatabase && db2 -tvmf db2_create_tables.sql"
I too faced many issues but after some research i came up with this solution, i hope it will help you/someone.
Well, I guess you didn't really search.
Db2 has several command line interfaces that can be and are used for batch processing. Clpplus even offers Oracle-like syntax support and compatibility. There is even a tool named db2batch that allows to process statements with lots of options.

print to PDF using xp_cmdshell

We are trying to use TSQL on SQL 2012 and OS 2012 to print PDF file from a specific directory using variables based on a lookup that populates these variables. The command we have works on SQL 2005 and 2003 datacenter OS. During our upgrade this now does not want to work. I am fairly sure we are just dealing with a syntax issue that is OS related but SQL is not saying what is the issue.
Here is the base query --
EXEC master.sys.xp_cmdshell '"C:\Program Files (x86)\Foxit Software\Foxit Reader\Foxit Reader.exe /t" \\jde9appb\d$\JDEdwards\E910\PrintQueue\R5509108_TV0001_4970_PDF.pdf \\VMPS08\INF2808P'
The above runs in the OS 2003 and SQL 2005 but when we try it in OS 2012 and SQL 2012 it just spins. We have turned of UAC and verified that the execute user has all the necessary rights to the command shell and command shell is enabled. This has to be done thru a TSQL script since this is part a stored proc that gets called by the custom application. Also the foixut reader is the default application to read PDF files. The switch you see above is the silient mode to print directly to a queue.
Help help. This has been a real tough one to figure out.
I have actually gotten it to say Failure to initialize the printer by messing with the Syntax, but this is as far as I have gone. I even loaded the printer I am going to to make sure the system is trying to use the proper driver.
For some reason the double backslash is not showing correctly. Here is the fixed query that I need some help with please.
EXEC master.sys.xp_cmdshell '"C:\Program Files (x86)\Foxit Software\Foxit Reader\Foxit Reader.exe /t" \\jde9appb\d$\JDEdwards\E910\PrintQueue\R5509108_TV0001_4970_PDF.pdf \\VMPS08\INF2808P'
We faced the exact same issue. We found it was caused by the network printers, though created for the user, not being available to the background process. Creating the printers locally, thus bypassing the print server solved the issue.

executing multiple SQL scripts encoded in UTF-8

I use Visual Studio 2008 database project for managing my database scripts and version control. by default visual studio script templates are encoded in UTF-8 format. when I try to concatenate the scripts for database release (using the DOS command "type *.sql > dbscripts.sql") and run the concatenated script from SQL server management studio, I get invalid character error. however if I convert each script to ANSI format (using notepad), and concatenate them then I don't get any errors when I run the concatenated script. but converting each script into a different encoding format is tedious. what is the best way to execute multiple SQL scripts regardless of the encoding format of the scripts? do I need to create a batch file and use sqlcmd to run it and redirect the output to a text file? are there any alternatives?
Thanks in advance.
The easy solution is to add the codepage flag to the command, -f 65001
sqlcmd /S myServer /d myDatabase -E -f 65001 -i MyUTFScriptWithoutBOM.sql
That's because of the BOM in the file. The TYPE command is too primitive to detect it and will just copy the 3 bytes to the output.
You can change the encoding used for the .sql file. Tedious too btw. File + Save As, click the arrow on the Save button, Save With Encoding and select your current code page. Like 1252 for Western Europe and Americas, it is probably the first one in the list. This is the same thing you did with Notepad.
Beware of the trouble you can get into if the server runs with a different code page and your scripts contain accented characters. I would myself just write a little utility that uses Directory.GetFiles and StreamReader/Writer to fix the problem.
found this link which generates "SQLCMD" commands using Windows Powershell. for concatenation, it looks like there is no solution except saving each file in ANSI format. I can at least generate a file with all sqlcmd commands using this without having to concatenate them
http://sqladm.blogspot.com/2010/02/generate-sqlcmd-statements.html
You should use the copy *.sql dbscripts.sql

SQL Server 2008 generate script wizard gives me a script that results in "unclosed quotation marks"

I have an SQL server 2008 database instance on one machine. Now I want to copy this database to another machine. I use the script wizard inside SQL Management Studio to generate a SQl-script with the schema and data. The script-file is rather big (around 17 GB).
Then I run the sql-script on the target machine it results in a :
Msg 105, Level 15, State 1 error with the message:
Unclosed quotation mark after the character string
I do understand the problem of what unclosed quotation marks mean. But I don't understand why the error happens. Isn't the script generator able to handle quotations inside text strings like...hello, what's up...correctly and create a script that will escape such characters?
Is their a limit on the length of text for the script wizard? Is this causing the problem.
I don't want to and I cannot open the script-file in a text editor (too large, text editor will crash) and manually fix the problems.
Do you have any ideas?
Solution for the SQL Server Import Issue
Pre-condition
In order to move the data from one SQL Server to another (e.g. from Production environment to Test environment) makes sense to use "Generate scripts" feature which is available in database options in SQL Server Management Studio. The result of this operation is text file with SQL commands that can be executed on another SQL Server. Usually these files are too big to execute them in SQL Server Management Studio, so we need to use sqlcmd command line utility from SQL Server installation package. In the most cases utility works smoothly and additional user actions are not necessary.
Issue description
In some rare cases the sqlcmd utility can fail with the import and raise the following error: "Unclosed quotation mark after the character string ..." which indicates that one of SQL queries has not been executed. This happens because sqlcmd works using stream processing, i.e. it reads some piece of data, processes it, reads next piece and so on. In some cases an input file can contain huge SQL instruction which size is bigger than the amount of the data that could be processed by sqlcmd at a time, so sqlcmd tries to execute broken SQL and fails.
Possible solutions
In order to fix this issue 2 approaches can be used:
The sqlcmd utility can accept the "-a" parameter which defines the maximum size of packet (piece of data) that will be used during processing. The maximum value is 32767, the default value is 4096, so it makes sense to always use this parameter with maximum value.
sqlcmd -i input.sql -a 32767 -o import_log.txt
If the first approach didn't help and issue still appears, there is another, more difficult solution:
Install the Cygwin
During the installation, after some standard screens, stop on the screen "Select packages"
In "Search" field, enter "sed", and in the tree below expand the "Base" category and choose version not less than 4.2.2 for installation
Complete installation
Note: "sed" is the Linux utility which allows stream-based file processing
After installation is completed, run "Cygwin64 Terminal" from the desktop. We will use it for next steps
Go to the directory where the SQL file generated by SQL Server Management Studio is located. You need to use Linux style slashes "/" instead of Windows style which is "\"
cd d:/temp
Change the encoding of the SQL file from UTF-16LE to UTF-8, because "sed" cannot process UTF-16LE, this conversion is safe for the data. The result will be a new file, that we will use in next step
iconv -f UTF-16LE -t UTF-8 input.sql > input_utf8.sql
Convert the new file, to have one SQL query in one batch. The result will be a new file, that we will use in next step
sed -e 's/^INSERT/GO\nINSERT/' input_utf8.sql > input_utf8_adapted.sql
Now the file "input_utf8_adapted.sql" should be processed by sqlcmd without any issues, so we can execute the following:
sqlcmd -i input_utf8_adapted.sql -a 32767 -o import_log.txt
After execution is done, please check import_log.txt to make sure that no errors appeared
I ended up on this question after trying to find a solution to a similar problem I had. I also needed to dump a DB (with data) via Generate scripts wizard and the resulting file was too big to be executed from SSMS. So I tried the sqlcmd but ended with the error
Sqlcmd: Error: Syntax error at line 10 near command '"' in file 'script.sql'.
It turned out the cause of the issue was a record containing data with jQuery syntax in it - $(".someclass"). It's because it is also a way how to insert a variable into sqlcmd.
The solution is to disable variable substitution by adding -x command line argument.
Not a direct answer to the question but to duck this issue you could use one of the following other methods of copying the database to the new location.
Copying Databases with Backup and Restore
Using Detach and Attach
Method 1 is usually preferable as it keeps the source DB online and detaching can cause information held in the master database about the source to be lost (e.g. full text enabled status)
EDIT: Just noted from your comment that you're running sqlcmd -S server\database -i script.sql. There is a -I switch that stands for "Enable Quoted Identifiers". Try to run the command with this switch.
Btw, to edit a large file, consider using a nice editor like Notepad++ or UltraEdit. I wouldn't use a workstation without em :)

Generating DDLs for Sybase tables and indexes

I'm looking for a command line tool to generate DDL for both tables and indexes (nothing more complicated is needed) for some Sybase tables in databases that I take care of. I have access to GUI tools for viewing the individual DDLs, and I could cut and paste them, but I would like something that will go through all the tables in a database and generate some nice text files that I can get checked into CVS.
I tried using a tool called ddlgen, which was provided by Sybase, but it just threw exceptions like this:
bash-3.00# ./ddlgen -SdatabaseServer:4100 -Uusername -PsecretPassword -TDB -NdatabaseName
U64: null: databaseName.dbo.firstTable
U64: null: databaseName.dbo.firstTable
at com.sybase.ddlgen.container.UserTableContainer.getDependentDDL(UserTableContainer.java:1065)
at com.sybase.ddlgen.container.UserTableContainer.open(UserTableContainer.java:1364)
at com.sybase.ddlgen.container.UserTableMetaContainer.open(UserTableMetaContainer.java:94)
at com.sybase.ddlgen.container.DDLBaseContainer.load(DDLBaseContainer.java:76)
at com.sybase.ddlgen.container.DatabaseContainer.addChildren(DatabaseContainer.java:552)
at com.sybase.ddlgen.container.DatabaseContainer.open(DatabaseContainer.java:104)
at com.sybase.ddlgen.container.DatabaseMetaContainer.open(DatabaseMetaContainer.java:114)
at com.sybase.ddlgen.DDLThread.run(DDLThread.java:89)
which wasn't very helpful. I keep thinking that there must be a nice Perlish way to do this, but I don't know what that would be.
You can also use the Perl-based dbschema.pl
http://www.isug.com/Sybase_FAQ/ASE/section9.html#9.3.2
use below command to get deffination
defncopy -P tester1 -S sqppdb2 -U pmestr -D ppdb2 -o tab4 ppdb2..tab4
Thanks
Download an evaluation version of Embarcadero DBArtisan and use its extract feature to get the DDL out.
You can turn Logging on in DBArtisan (Logfile ->Log SQL) and then see what SQL it's sending to Sybase to get the table DDL. Copy and paste the SQL in the logfile to a script that you run from the command line and that might work.
Apologies in advance if you are not using Windows...DBArtisan is Windows-only.
Another way of doing this is MyGeneration a code generator (like CodeSmith but open source) which uses templates to create code. That code could be anything you like - Sql, C# etc. I use Sql Server and I've used some of the freely available templates to create DDL as you specify, and automagically create NHibernate Mapping files too - brilliant.
ddlgen will give you what you require and works very well. You seem to be having an enviroment issue with Java. Try again and post the error that you have in it's entirety.

Resources