While installing with my NSIS installer script I need to run some script on SQL Server: select, update, create and insert.
How can I do that without having a SQL Server engine on the computer running the NSIS installer?
I thought about packing a SQL Server Compact Edition to my installer in order to use it to connect to the SQL Server. Is that the way I should go for?
You don't need the SQL Server engine to perform queries on a remote machine, you need a driver.
One approach is to use the command line client, which also requires the native driver. You probably want to bundle or search for the driver your application uses in your installer.
In SQL Server 2005 or later the command line client is called sqlcmd. It can be downloaded from the feature pack download pages (2005|2008|2008R2|2012).
So its simply a matter of bundling a SQL script with the installer and executing the script by calling sqlcmd with ExecWait.
You can run a script using a trusted connection via:
sqlcmd -S _SERVER\_INSTANCE_ -d _DBNAME_ -i _SCRIPT_FILE_
Or with a SQL Login:
sqlcmd -S _SERVER\_INSTANCE_ -d _DBNAME_ -U _USERNAME_ -P _PASSWORD_ -i _SCRIPT_FILE_
A SQL 2000 version of this approach can be found here on the nsis wiki.
I found a Plugin for NSIS that can connect to a MS SQL database: MSSQL_OLEDB_plug-in
It can be used like that:
${OLEDB}::SQL_Logon "$SQLSERVER" "$SQLUSER" "$SQLPASSWORD"
${OLEDB}::SQL_Execute "$SQLQUERY"
${OLEDB}::SQL_GetError
${OLEDB}::SQL_GetRow
Pop $0
DetailPrint $0
Pop $0
DetailPrint $0
${OLEDB}::SQL_Logout
Related
I'm trying to deploy a database (.bak / sql server 2012) by a batch(.bat) file , could you give me an example or how could I do that?
Best Regards.
You can use the SqlCmd command-line tool in a batch-file to do that. This command-line tool is installed when you install SQL Server Management Tools – Basic.
There is plenty of online information, this is a pseudo-code example I found:
SqlCmd -E -S <Server_Name> –Q “RESTORE DATABASE [Name_of_Database] FROM DISK='X:PathToBackupFile[File_Name].bak'”
I am using SQL Server 2008 (Express Edition).
I want to create a job which will delete all data from the all the table(>50) in the DB everyday at night 1:00.
Instead of Deleteting i decide to restore the DB from the Script.
It would have been easy by using SQL Server Agent, But this is limitation in SQL Server Express.
I figured out that we can create jobs "manually" by creating batch files and SQL script files, and running them via Windows Task Scheduler.
I have no clue what i have to write in bat file and sql file.This is my first time where i am working so deeply in SQL configuration. Can someone help please?
Name of the script which i need to restore is test.sql.
If any one has different approach , please share.
Thanks
Prat
Your batch files needs to look like this. Change the path to your .sql file and also put in the sql server info. You can read more about sqlcmd HERE. Also note the case on the switches -S and -i as it does matter.
sqlcmd -S <ComputerName>\<InstanceName> -i C:\test.sql
I am trying to downgrade a SQL Server 2014 database to a lower version (SQL Server 2012) by using the task Generating scripts found when right clicked on the database.
After I make the settings in order to generate the script, the server does that but when I'm trying to open the file (the script made) on a lower version instance of SQL Server, I get the following error.
System out of memory exception thrown
Could anyone provide some help? Thanks!
As suggested by #usr you can run the script from the command line using sqlcmd:
sqlcmd -S myServer\instanceName -i C:\myScript.sql
Alternatively you can download a trial edition of ApexSQL or Redgate SQL Compare (assuming you have not tried them already) and script over the changes using these tools.
The script is too big for SSMS (a shame!). Run it using SQL Server command line tools.
I find it easier to do this by using Redgate SQL Packager or the two compare tools. They can execute enormous scripts.
There is the sql-cli utlity (installed with npm) which I am using to connect to various SQL Server machines. It works perfectly, however when connecting to localdb it responds with an error. So the next command line works:
mssql -s SomeServerAddress
But this one doesnt work:
mssql -s (localdb)\v11.0
And it responds with
\v11.0 was unexpected at this time.
Is there any special formatting for the server name to work?
Thanks.
its because (localdb)\v11.0 is not an actual instance as it can only be used inside visual studio.
"The localdb is at the heart of SSDT; it’s similar to SQL Server Express under the hood and runs a full version of sqlserver.exe. However this is throttled by the numbers of CPUs and limits on resources. There are quite a lot of limitations; you cannot upgrade the instance and there is no management and the sqlserver.exe does not run as a service. It is not similar to SQL Server Compact as this is feature-less (no stored procedures or functions) it is actually a DLL file that runs in a process from within Visual Studio, but is not available to task manager or windows. It is awakened when the SQL Server Native Client requests a connection from within Visual Studio. It doesn't stay online forever, it shuts down after time. You can configure where it creates the SQL files required to run. The localdb does not support table partitioning or data compression at the moment. However there are not many features that it does not support. You can however configure SSDT to use a full version of SQL Server i.e. the Developer edition, if your project requires unsupported features; by changing the Debug Connections tring in the projects properies." - Andrew J Fenna
This works:
mssql-cli -E -S (localdb)\mssqllocaldb
The -E is for integrated auth, the -S is the server.
If it does not connect, try to start localdb:
sqllocaldb start mssqllocaldb
This is all that is needed. No need to start Visual Studio or any other tool.
You may alreaady have gone on to bigger and better things, but
mssql -s "(localdb)\MSSQLLocalDB"
has worked for me.
Regards
I downloaded the 53.5 MB setup of SQL Server 2005 Express from this URL:
Microsoft SQL Server 2005 Express Edition
Then I ran sqlcmd by going into command promp , but it shows error
HResult 0x2, Level 16, State 1
Named Pipes Provider: Could not open a connection to SQL Server 2.
Sqlcmd: Error: Microsoft SQL Native Client : An error has occurred
while establishing a connection to the server. When connecting to SQL Server 2005,
this failure may be caused by the fact that under the default settings SQL Server
does not allow remote connections..
Sqlcmd: Error: Microsoft SQL Native Client : Login timeout expired.
Then I downloaded and installed Sql Command Line Utility from this URL:
Feature Pack for Microsoft SQL Server 2005 - November 2005
Then also it shows the same error.
Then I installed Sql Native Client, then also it didn't work. But when I installed Management Studio, then from Management Studio the connection is established, but not from sqlcmd from Command Prompt.
Please help.
"Just typing sqlcmd on its own won't work - how should the utility know what server to connect, what database to use, and what command to run??"
Funny thing. As I read the Microsoft documentation, just typing sqlcmd while sitting on the server should open a connection to this server. At least it can, depending on settings for security. Typing sqlcmd without any options for login/security may very well fail, but the utility will defintely try.
And SQLCMD without quit is just that, right?
What if you do something like:
sqlcmd -S W\SQLEXPRESS -E -d master -q "select * from sys.tables"
Just typing sqlcmd on its own won't work - how should the utility know what server to connect, what database to use, and what command to run??
You need to tell sqlcmd what you want to do! Get a list of its command options using sqlcmd -?, study them, and use them! Or read the MSDN docs on Using the sqlcmd utility with SQL Server Express - btw: searching MSDN for answers and documentation would be a good idea for all your SQL Server problems......
The SQL Native Client is definitely needed, yes - not sure what you mean by the second package there.