Old 15th May 2009, 14:01   #1
H-Block
Junior Member
 
Join Date: May 2009
Posts: 5
Send a message via ICQ to H-Block
NSIS execute *.bat or *.exe

Hallo,
i try to explain my problem.
I want to execute a .bat file from to installer, that attach a database to a SQLEXPRESS 2005. The Same with the .exe file.

Here is the .bat content:
sqlcmd -U user -P pw -S %computername%\test -Q "sp_attach_db 'test','%Programfiles%\Microsoft SQL Server\MSSQL.1\MSSQL\Data\test.mdf','%Programfiles%\Microsoft SQL Server\MSSQL.1\MSSQL\Data\test_log.ldf'"

Now my problem when I execute the .bat or exe file self it works. When I execute it from the installer not.

Help me please.
H-Block is offline   Reply With Quote
Old 15th May 2009, 16:29   #2
jpderuiter
Major Dude
 
Join Date: Feb 2007
Posts: 672
How do you call the bat file?

You can use the MSSQL OLEDB plug-in to attach the database as well (usefull if you have to do more than just attaching).
http://nsis.sourceforge.net/MSSQL_OLEDB_plug-in
jpderuiter is offline   Reply With Quote
Old 18th May 2009, 05:52   #3
H-Block
Junior Member
 
Join Date: May 2009
Posts: 5
Send a message via ICQ to H-Block
Okay i tried it with MSSQL OLEDB.
Login into SQLEXPRESS Works but then came "Quary Failed".Why?

Here the script:
NSIS
SetOutPath "$Temp"
File "D:\db_connect.sql"
ReadRegStr $R3 HKLM "SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName" "ComputerName"
MSSQL_OLEDB::SQL_Logon "$R3\SQLEXPRESS" "SA" "TEST"
MSSQL_OLEDB::SQL_Execute "$Temp\db_connect.sql"
MSSQL_OLEDB::SQL_Logout

db_connect.sql
sp_attach_db 'test','%Programfiles%\Microsoft SQL Server\MSSQL.1\MSSQL\Data\test.mdf','%Programfiles%\Microsoft SQL Server\MSSQL.1\MSSQL\Data\test_log.ldf'

Thank you for your help

Okay I found my problem i used MSSQL_OLEDB::SQL_Execute and not MSSQL_OLEDB::SQL_ExecuteScript. But now came "Error initializing script". Why?

Last edited by H-Block; 18th May 2009 at 06:08.
H-Block is offline   Reply With Quote
Old 18th May 2009, 06:21   #4
jpderuiter
Major Dude
 
Join Date: Feb 2007
Posts: 672
Try following as your sql script
code:
USE [master]
GO
CREATE DATABASE [test] ON
( FILENAME = N'%Programfiles%\Microsoft SQL Server\MSSQL.1\MSSQL\Data\test.mdf' ),
( FILENAME = N'%Programfiles%\Microsoft SQL Server\MSSQL.1\MSSQL\Data\test_log.ldf' )
FOR ATTACH
GO

jpderuiter is offline   Reply With Quote
Old 18th May 2009, 07:34   #5
H-Block
Junior Member
 
Join Date: May 2009
Posts: 5
Send a message via ICQ to H-Block
Thank you for your fast answer.
I tried your code but it doesn't work.
There is the same error. Error initializing script.
When I testing it with the Example from MSSQL_OLEDB the Error is:
Extract: test.sql
Loggin on to SQL server Notebook\test
0
Logon successfull
0
SQL State: 0x00000000 - Native: 0 - Message:
1
Query Failed
0
SQL State: 0x80040e14 - Native: 0 - Message: Wrong Syntax in the near of '\'.
1
Error getting rowset
1
Error getting rowset
0
SQL State: 0x00000000 - Native: 0 - Message:
0
Script executed successfully
0
SQL State: 0x00000000 - Native: 0 - Message:
0
Microsoft SQL Server 2005 - 9.00.1399.06 (Intel X86)
Oct 14 2005 00:33:37
Copyright (c) 1988-2005 Microsoft Corporation
Express Edition on Windows NT 5.1 (Build 2600: Service Pack 3)

2
No more data
0
SQL State: 0x00000000 - Native: 0 - Message:
Completed

When I connect to the SQL server over cmd--> sqlcmd
and paste your code it works.
H-Block is offline   Reply With Quote
Old 18th May 2009, 08:38   #6
H-Block
Junior Member
 
Join Date: May 2009
Posts: 5
Send a message via ICQ to H-Block
Okay I found my problem and fixed it.
Now I have another Problem. I want to create a user with this script:
MSSQL_OLEDB::SQL_Execute "CREATE LOGIN test WITH PASSWORD = '123456';USE testdb;CREATE USER test FOR LOGIN test WITH DEFAULT_SCHEMA = dbo;Exec sp_addrolemember'db_owner','test'"

Login into server is okay. Have I anything vorgotten?
H-Block is offline   Reply With Quote
Old 18th May 2009, 08:50   #7
jpderuiter
Major Dude
 
Join Date: Feb 2007
Posts: 672
Quote:
Okay I found my problem and fixed it.
How did you solve it?
Quote:
Now I have another Problem. I want to create a user with this script: MSSQL_OLEDB::SQL_Execute "CREATE LOGIN test WITH PASSWORD = '123456';USE testdb;CREATE USER test FOR LOGIN test WITH DEFAULT_SCHEMA = dbo;Exec sp_addrolemember'db_owner','test'" Login into server is okay. Have I anything vorgotten?
You can pass only one SQL command to MSSQL_OLEDB::SQL_Execute.
Use this instead:
code:
MSSQL_OLEDB::SQL_Execute /NOUNLOAD "CREATE LOGIN test WITH PASSWORD = '123456'"
MSSQL_OLEDB::SQL_Execute /NOUNLOAD "USE testdb"
MSSQL_OLEDB::SQL_Execute /NOUNLOAD "CREATE USER test FOR LOGIN test WITH DEFAULT_SCHEMA = dbo"
MSSQL_OLEDB::SQL_Execute /NOUNLOAD "Exec sp_addrolemember 'db_owner','test'"

jpderuiter is offline   Reply With Quote
Old 18th May 2009, 09:37   #8
H-Block
Junior Member
 
Join Date: May 2009
Posts: 5
Send a message via ICQ to H-Block
Okay Thank you it Works.
Here is the code I used.

For SQLEXPRESS install:
Function ExistsMSDE
ExecWait "$Temp\SQL.EXE -q /norebootchk /qb reboot=ReallySuppress addlocal=all instancename=SQLEXPRESS SCCCHECKLEVEL=IncompatibleComponents:1;MDAC25Version:0 ERRORREPORTING=1 SQLAUTOSTART=1 SAPWD=test SECURITYMODE=SQL DISABLENETWORKPROTOCOLS=0"
Sleep 10000 ; wait for a 10 seconds
FunctionEnd

For Database:
ReadRegStr $R3 HKLM "SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName" "ComputerName"
MSSQL_OLEDB::SQL_Logon "$R3\SQLEXPRESS" "sa" "test"
MSSQL_OLEDB::SQL_Execute "CREATE DATABASE [DB_name] ON ( FILENAME = N'$Programfiles\Microsoft SQL Server\MSSQL.1\MSSQL\Data\DB.mdf' ),( FILENAME = N'$Programfiles\Microsoft SQL Server\MSSQL.1\MSSQL\Data\DB_log.ldf' ) FOR ATTACH GO"
MSSQL_OLEDB::SQL_Logout

For User:
ReadRegStr $R3 HKLM "SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName" "ComputerName" MSSQL_OLEDB::SQL_Logon "$R3\SQLEXPRESS" "sa" "test"
MSSQL_OLEDB::SQL_Execute /NOUNLOAD "CREATE LOGIN user WITH PASSWORD = 'PW'"
MSSQL_OLEDB::SQL_Execute /NOUNLOAD "USE DB_name"
MSSQL_OLEDB::SQL_Execute /NOUNLOAD "CREATE USER user FOR LOGIN user WITH DEFAULT_SCHEMA= dbo_owner"
MSSQL_OLEDB::SQL_Execute /NOUNLOAD "Exec sp_addrolemember 'db_owner', 'user'"
MSSQL_OLEDB::SQL_Logout

Thank you for your fast Help.
H-Block is offline   Reply With Quote
Reply
Go Back   Winamp & Shoutcast Forums > Developer Center > NSIS Discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump