trouble accessing mssql db using perl and freetds on unix - sql-server

I'm trying to connect to a MS-SQL Database using perl and freetds. I have tested the installation of freetds using the unix commandline
`/usr/local/exec/bin/tsql -S myDB -I freetds.conf -U userName -P passw0rd -D DataBase1 -o q < query.sql`
where query.sql contains my sql query. It runs perfectly well. But when I try the same with perl it gives me the following error -
`Your sybase home directory is /opt/sybase. Check the environment variable SYBASE if it is not the one you want! Cannot access file /opt/sybase/config/objectid.dat`
but running
$ set | grep SYBASE yields SYBASE=/usr/fsf/freetds
Below is my perl code:
#!/usr/bin/perl5/core/5.8.8/exec/bin/perl
use lib qw(/usr/perl5/core/5.8.8/exec/lib);
use lib qw(/usr/perl5/DBI/1.607/exec/5.8/lib/perl5);
use lib qw(/usr/perl5/DBD-Sybase/1.09/exec/5.8/lib/perl5);
use DBI;
use DBD::Sybase;
my $user = "userName";
my $passwd = "passw0rd";
my $server = "myDB";
`export SYBASE=/usr/fsf/freetds`;
`export LD_LIBRARY_PATH=/usr/fsf/freetds/0.82/exec/lib`;
`export FREETDSCONF=./freetds.conf`;
my $dbh = DBI->connect("DBD:Sybase:server=$server", $user, $passwd, {PrintError => 0});
unless ($dbh) {
die "ERROR: Failed to connect to server ($server).\nERROR MESSAGE: $DBI::errstr";
}
else {
print "\n";
print "Successful Connection.";
}
Any help much appreciated!

The path to your drivers says 5.10. You might have downloaded the drivers for the wrong version of perl. Either update to 5.10.1 or get the drivers for 5.8.8.

I have figured it out. Well, you need to first set the SYBASE environment variable before you install DBD-Sybase. And that's the reason behind Your sybase home directory is /opt/sybase when it is supposed to point to the freetds installation location. Ref: http://www.idevelopment.info/data/SQLServer/DBA_tips/Programming/PROG_4.shtml#Install%20DBD-Sybase

Related

How to run a sql command from the command prompt in a batch file

I'm looking for a simple way to run the following command from a command line. I need to run it as part of a regular old .bat file on a windows server, so I need some kind of command line utility. I looked into sqlcmd but not sure on the correct syntax and it somewhat looked complicated. I need to be able to enter a sql username and password as part of the utility to connect to the sql server, since this will be running on a different machine.
I need to run a simple command like this:
UPDATE ABC SET PictureName = ID + '.jpg' WHERE TYPE = 'active'
Ideally I would want some tool that I can simply run in this way or something similar:
AwesomeTool.exe -sqlUsername "username" -sqlPass "password" -sqlStatement "UPDATE ABC SET PictureName = ID + '.jpg' WHERE TYPE = 'active'"
By the way this needs to work in MS SQL 2008 and newer. Thank you.
Just use sqlcmd:
sqlcmd -U "username" -P "password" -S MySqlServer -d MyDatabase -q "UPDATE ABC SET PictureName..."

How to restore Microsoft SQL Server database from .sql file? [duplicate]

I use RedGate SQL data compare and generated a .sql file, so I could run it on my local machine. But the problem is that the file is over 300mb, which means I can't do copy and paste because the clipboard won't be able to handle it, and when I try to open the file in SQL Server Management Studio I get an error about the file being too large.
Is there a way to run a large .sql file? The file basically contains data for two new tables.
From the command prompt, start up sqlcmd:
sqlcmd -S <server> -i C:\<your file here>.sql
Just replace <server> with the location of your SQL box and <your file here> with the name of your script. Don't forget, if you're using a SQL instance the syntax is:
sqlcmd -S <server>\instance.
Here is the list of all arguments you can pass sqlcmd:
Sqlcmd [-U login id] [-P password]
[-S server] [-H hostname] [-E trusted connection]
[-d use database name] [-l login timeout] [-t query timeout]
[-h headers] [-s colseparator] [-w screen width]
[-a packetsize] [-e echo input] [-I Enable Quoted Identifiers]
[-c cmdend] [-L[c] list servers[clean output]]
[-q "cmdline query"] [-Q "cmdline query" and exit]
[-m errorlevel] [-V severitylevel] [-W remove trailing spaces]
[-u unicode output] [-r[0|1] msgs to stderr]
[-i inputfile] [-o outputfile] [-z new password]
[-f | i:[,o:]] [-Z new password and exit]
[-k[1|2] remove[replace] control characters]
[-y variable length type display width]
[-Y fixed length type display width]
[-p[1] print statistics[colon format]]
[-R use client regional setting]
[-b On error batch abort]
[-v var = "value"...] [-A dedicated admin connection]
[-X[1] disable commands, startup script, environment variables [and exit]]
[-x disable variable substitution]
[-? show syntax summary]
I had exactly the same issue and had been struggling for a while then finally found the solution which is to set -a parameter to the sqlcmd in order to change its default packet size:
sqlcmd -S [servername] -d [databasename] -i [scriptfilename] -a 32767
You can use this tool as well. It is really useful.
BigSqlRunner
NB: Broken link, so have updated it.
Take command prompt with administrator privilege
Change directory to where the .sql file stored
Execute the following command
sqlcmd -S 'your server name' -U 'user name of server' -P 'password of server' -d 'db name'-i script.sql
I am using MSSQL Express 2014 and none of the solutions worked for me. They all just crashed SQL. As I only needed to run a one off script with many simple insert statements I got around it by writing a little console app as a very last resort:
class Program
{
static void Main(string[] args)
{
RunScript();
}
private static void RunScript()
{
My_DataEntities db = new My_DataEntities();
string line;
System.IO.StreamReader file =
new System.IO.StreamReader("c:\\ukpostcodesmssql.sql");
while ((line = file.ReadLine()) != null)
{
db.Database.ExecuteSqlCommand(line);
}
file.Close();
}
}
Run it at the command line with osql, see here:
http://metrix.fcny.org/wiki/display/dev/How+to+execute+a+.SQL+script+using+OSQL
Hope this help you!
sqlcmd -u UserName -s <ServerName\InstanceName> -i U:\<Path>\script.sql
I had similar problem. My file with sql script was over 150MB of size (with almost 900k of very simple INSERTs). I used solution advised by Takuro (as the answer in this question) but I still got error with message saying that there was not enough memory ("There is insufficient system memory in resource pool 'internal' to run this query").
What helped me was that I put GO command after every 50k INSERTs.
(It's not directly addressing the question (file size) but I believe it resolves problem that is indirectly connected with large size of sql script itself. In my case many insert commands)
==> sqlcmd -S [servername] -d [databasename] -i [scriptfilename] -a 32767
I have successfully done with this command with 365mb sql file.
this syntax runs in about 15 minutes.
it helped me solve a problem that took me a long time to figure out
Run the script file
Open a command prompt window.
In the Command Prompt window, type: sqlcmd -S <ServerName\InstanceName> -i C:\yourScript.sql
Press ENTER.
Your question is quite similar to this one
You can save your file/script as .txt or .sql and run it from Sql Server Management Studio (I think the menu is Open/Query, then just run the query in the SSMS interface). You migh have to update the first line, indicating the database to be created or selected on your local machine.
If you have to do this data transfer very often, you could then go for replication. Depending on your needs, snapshot replication could be ok. If you have to synch the data between your two servers, you could go for a more complex model such as merge replication.
EDIT: I didn't notice that you had problems with SSMS linked to file size. Then you can go for command-line, as proposed by others, snapshot replication (publish on your main server, subscribe on your local one, replicate, then unsubscribe) or even backup/restore
The file basically contain data for two new tables.
Then you may find it simpler to just DTS (or SSIS, if this is SQL Server 2005+) the data over, if the two servers are on the same network.
If the two servers are not on the same network, you can backup the source database and restore it to a new database on the destination server. Then you can use DTS/SSIS, or even a simple INSERT INTO SELECT, to transfer the two tables to the destination database.
There is probably another way for all the fellows still encountering problems importing really large SQL dumps.
What also be considered when possible: If you have access to the server you could export the database in multiple parts, like first the structure, then per table (or related objects) an export of the data in smaller pieces, instead of one big file.
When you don't have access to server and/or required to use the existing big file, you could try to split them into parts with SQLDumpSplitter: https://philiplb.de/sqldumpsplitter3/.
Then import the pieces to get a full copy of the database.
Good luck, guys.

Using variables in SQLCMD for Linux

I'm running the Microsoft SQLCMD tool for Linux (CTP 11.0.1720.0) on a Linux box (Red Hat Enterprise Server 5.3 tikanga) with Korn shell. The tool is properly configured, and works in all cases except when using scripting variables.
I have an SQL script, that looks like this.
SELECT COLUMN1 FROM TABLE WHERE COLUMN2 = '$(param1)';
And I'm running the sqlcmd command like this.
sqlcmd -S server -d database -U user -P pass -i input.sql -v param1="DUMMYVALUE"
When I execute the above command, I get the following error.
Sqlcmd: 'param1=DUMMYVALUE': Invalid argument. Enter '-?' for help.
Help lists the below syntax.
[-v var = "value"...]
Am I missing something here?
You don't need to pass variables to sqlcmd. It auto picks from your shell variables:
e.g.
export param1=DUMMYVALUE
sqlcmd -S $host -U $user -P $pwd -d $db -i input.sql
In the RTP version (11.0.1790.0), the -v switch does not appear in the list of parameters when executing sqlcmd -?. Apparently this option isn't supported under the Linux version of the tool.
As far as I can tell, importing parameter values from environment variables doesn't work either.
If you need a workaround, one way would be to concatenate one or more :setvar statements with the text file containing the commands you want to run into a new file, then execute the new file. Based on your example:
echo :setvar param1 DUMMYVALUE > param_input.sql
cat input.sql >> param_input.sql
sqlcmd -S server -d database -U user -P pass -i param_input.sql
You can export the variable in linux. After that you won't need to pass the variable in sqlcmd. However, I did notice you will need to change your sql script and remove the :setvar command if it doesn't have a default value.
export dbName=xyz
sqlcmd -Uusername -Sservername -Ppassword -i script.sql
:setvar dbName --remove this line
USE [$(dbName)]
GO
I think you're just not quoting the input variables correctly. I created this bash script...
#!/bin/bash
# Create a sql file with a parameterized test script
echo "
set nocount on
select k = '-db', v = '\$(db)' union all
select k = '-schema', v = '\$(schema)' union all
select '-', 'static'
go" > ./test.sql
# capture input variables
DB=$1
SCHEMA="${2:-dbo}"
# Exec sqlcmd
sqlcmd -S 'localhost\lemur' -E -i ./test.sql -v "db=${DB}" -v "schema=${SCHEMA}"
... and tested it like so:
$ ./test.sh master
k v
------- ------
-db master
-schema dbo
- static

How to use sqlcmd to create a database

I have a .sql script and I want to build a database from it. How to do it in sqlcmd? I know it's like:
CREATE DATABASE dbName
GO
But how to specify the .sql script and location to build the database?
Use #Jeremiah Peschka's answer to supply the sqlcmd utility with the script to execute.
As for the location for the newly created database, it can be specified as part of the CREATE DATABASE command:
CREATE DATABASE dbName
ON (
NAME = dbName_dat,
FILENAME = 'D:\path\to\dbName.mdf'
)
LOG ON (
NAME = dbName_log,
FILENAME = 'D:\path\to\dbName.ldf'
)
As you can see from the linked article, you can specify other properties as well, like initial size, maximum size etc.
This is very simple. Just enter following command to run sqlcmd:
sqlcmd -U sa -P password
Then enter:
CREATE DATABASE MYDB
GO
This will create the db. Then enter "quit" to exit.
without a file:
sqlcmd -Q "CREATE DATABASE HelloWorld"
sqlcmd -i C:\path\to\file.sql
More options can be found in SQL Server books online.
use this:
sqlcmd -i "c:\my scripts\my script.sql"
see sqlcmd description at MS for other options

How to connect to MS SQLServer using Vim dbext on Mac OSX?

I use MacVim and the dbext plugin to connect to Oracle and it works well. Now I need to connect to MS SQLServer,
but it showed error:
Connection: T(SQLSRV) H(localhost) U(user) at 14:38
/bin/bash: osql: command not found
Anyone know how to do this?
Make sure you have one of the FreeTDS CLI programs. I think tsql is more full featured then osql, but the same approach should work with either.
Create a shell script to wrap tsql. Put it somewhere in your path.
Then add the dbext config values to your .vimrc
" I'm using mssql.sh as the wrapper program.
" Re-title to whatever you name yours
let g:dbext_default_SQLSRV_bin = "mssql.sh"
" FreeTDS options for osql/tsql are not as feature rich as dbext expects
let g:dbext_default_SQLSRV_cmd_options = ' '
" set 'host' in you profile to the FreeTDS server config, which will be altered in the script
The wrapper I whipped up is nothing special, but it's tested and works.
#!/bin/bash
# -S is better for FreeTDS then -H
options=$( echo $# | sed -e 's/-H /-S /' -e 's/ -i.*//' )
# osql/tsql in freetds don't seem to accept a file flag
sql_scratch=$( echo $# | sed 's|^.* -i||' )
# and execute...
cat $sql_scratch | tsql $options
Osql comes with the library of FreeTDS, but probably another error will be prompted: "Illegal option -w".
You can use ODBC instead of SQLSRV in the type parameter in the connection of DBEXT. (The other option being using DBI perl interface)
Install iodbc and build freetds with the --with-iodbc option.
Edit your odbc.ini file, you may find it using iodbc-config --iodbcini or find -name odbc.ini / | grep odbc.ini.
My working odbc.ini (please take care with file names, im on a freebsd box):
[MYDNSNAME]
Driver = /usr/local/lib/libtdsodbc.so
Description = Sample OpenLink MT DSN
Server = 192.168.100.4
Port = 50436
TDS_Version = 8.0
Database = initial_db
ServerOptions =
ConnectOptions =
Options =
ReadOnly = no
And my dbext connection on .vimrc:
let g:dbext_default_profile_CONN = 'type=ODBC:dsnname=MYDSNNAME:user=domain\user:passwd=pass:dbname=initial_db'
You can also configure DBExt to use the richer sqsh instead of the osql program in freetds. An example connection profile that does so can be found in :h dbext by searching for "sqsh". You should of course already have sqsh in working order.

Resources