I want to run
EXEC MANAGE_TEST
in petaho
IN SQLPLUS : EXEC MANAGE_TEST run fine
I got it solved ,
Instead of
EXEC MANAGE_TEST
I used
CALL MANAGE_TEST()
It worked .
Related
Hi I need to run a batch file from a sql-clp script:
The script is
CONNECT TO MYTAB1 USER xxxx using yyyyyyy;
QUIESCE DATABASE IMMEDIATE FORCE CONNECTIONS;
CONNECT RESET;
BACKUP DATABASE MYTAB1 TO "C:\temp\bcks" WITHOUT PROMPTING;
CONNECT TO MYTAB1 USER xxxx using yyyyyyy;
UNQUIESCE DATABASE;
CONNECT RESET;
cmd.exe /c "C:\Users\xxxx\Desktop\backup_neu.bat C:\temp\bcks C:\temp\bcks\zips 7z");
It runs great until it reaches the last line.
I tried
cmd.exe /c
exec(' xp_cmdshell ''script_here');
EXEC master..xp_CMDShell '"script here "'
but nothing worked.
OI have DB2 v10 running.
Any ideas on how I can get the batch file running?
Thanks for all your help.
TheVagabond
Ok I found the solution....
really simple somehow, just needed
!C:\Users\xxxx\Desktop\backup_neu.bat C:\temp\bcks C:\temp\bcks\zips 7z
so only a ! that was it.
I have been playing around with database backup automation scripts and in particular the one at this link:
http://support.microsoft.com/kb/2019698
I got everything working fine and even added automated compression using 7zip, logging, and with the help of vbscript an email scheduled notification. However, even without all that, you can see this is a bit heavy. Its now easily reaching 400 lines of code.
I am really not comfortable having all my stuff in one block like this and I want to separate it out. So I can have say a compression file called BackupCompress.sql, and an log file called BackupLogReport.sql all of which would be called from inside the main Backup.sql script.
The Backup.sql script is in turn run from a Backup.bat file which is set to run in the scheduler.
All of this works like a charm. But I am at a loss as to how to call BackupCompress.sql from within BackupLogReport.sql and pass in parameters and get a return value.
In the Backup.bat file I use this command to spin everything up and pass parameters to it:
SQLCMD -S %SQLDATABASE% -d master -i %BACKUP_FOLDER%\Backup.sql -v Pram1="%Pram1%"
In the Backup.sql file I get those parameters simply by:
DECLARE #Param1 NVARCHAR(256) = '$(Param)'
from then on as my script runs it uses whatever I want to pass in.
I tried using standard sql stored procedure logic to call another procedure like this:
EXEC BackupCompress.sql
#AnotherParam = #Param1
I also tried:
EXECUTE sp_executesql BackupCompress.sql #Param1
Finally I tried:
SET #cmd = 'SQLCMD -S ' + ##ServerName + ' -d master -i $(BACKUP_FOLDER)\BackupCompress.sql -v Param1 = ' + #Param1
EXEC xp_cmdshell #cmd, no_output
but it doesn't work and my files which were being compressed simply don't get compressed. I get no error message. everything else continues to work fine.
EDIT: I was getting an error message on the last one but I fixed it - however, I still don't get my little zip file. I even put print's into the file to see if it was actually be executed but it does not seem to be.
EDIT2: Another option I have tried, almost works, but cant figure out how to pass parameters from within the sql file to the other file... As a result it generates an error saying it cant find the file as it's treating the path as a literal string instead of the variable value I want to pass.
:!!SQLCMD -S ##ServerName -d master -i #CFG_BACKUP_PATH\BackupCompress.sql -v Param1 = #Param1
xp_cmdshell can return values. These values can be captured into a table variable that you could use to "see" the results, and perhaps determine where the problem lies:
DECLARE #cmd VARCHAR(255);
DECLARE #Param1 NVARCHAR(256) = '$(Param)';
DECLARE #Results TABLE
(
ResultsText NVARCHAR(MAX)
);
SET #cmd = 'SQLCMD -S ' + ##ServerName + '-d master -i $(BACKUP_FOLDER)\$(BackupCompress.sql) -v Param1 = ' + #Param1;
SET #cmd = 'DIR \';
INSERT INTO #Results (ResultsText)
EXEC xp_cmdshell #cmd;
SELECT *
FROM #Results;
You need to ensure xp_cmdshell is enabled for the instance, by executing:
EXEC sp_configure 'xp_cmdshell',1;
So I have been looking around for a way to develop a script that will execute other scripts from within my project folder using SQL Server Management Studio and so far none of the other solutions have worked. I tried writing a script that had the sqlcommandline stuff in it:
sqlcmd -S.\SQLExpress -imyScript.sql;
and that didn't work and from my understanding using #\path\to\script.sql won't work either so any other ideas? Or should I start looking into writing a procedure? In which case, could anybody point me in the right direction?
Thank you in advance for any assistance.
Personally, I'd look into writing stored procedures. The MSDN documentation is good and there are lots of resources on line if you do a quick search.
Alternatively you could do something like this to MAKE it happen (you'll need to have permission to execute command shell, etc):
CREATE TABLE ##SQLFiles ( SQLFileName VARCHAR(2000))
GO
INSERT INTO ##SQLFiles
EXECUTE master.dbo.xp_cmdshell 'dir /b "C:\SQL Scripts\*.sql"'
GO
DECLARE cFiles CURSOR LOCAL FOR
SELECT DISTINCT [SQLFileName]
FROM ##SQLFiles
WHERE [SQLFileName] IS NOT NULL AND
[SQLFileName] != 'NULL'
ORDER BY [SQLFileName]
DECLARE #vFileName VARCHAR(200)
DECLARE #vSQLStmt VARCHAR(4000)
OPEN cFiles
FETCH NEXT FROM cFiles INTO #vFileName
WHILE ##FETCH_STATUS = 0
BEGIN
-- The following SET command must be on a single line or else an error will be generated.
-- It is split in this script for readability purposes.
SET #vSQLStmt = 'master.dbo.xp_cmdshell ''osql -S Server Name -U User Name -P Password
-d Database Name -i "C:\SQL Scripts\' + #vFileName + '"'''
EXECUTE (#vSQLStmt)
FETCH NEXT FROM cFiles INTO #vFileName
END
CLOSE cFiles
DEALLOCATE cFiles
GO
DROP TABLE ##SQLFiles
GO
I created the following stored procedure:
CREATE PROCEDURE [dbo].[_StartWebcamStream]
AS
BEGIN
declare #command varchar(200)
set #command = 'C:\startStream.bat'
exec master..xp_cmdshell #command
END
for executing a the batch file startStream.bat. This batch contain the following code:
"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" -I dummy -vvv rtsp://mycamaddress/live_mpeg4.sdp --network-caching=4096 --sout=#transcode{vcodec=mp4v,fps=15,vb=512,scale=1,height=240,width=320,acodec=mp4a,ab=128,channels=2}:duplicate{dst=http{mux=asf,dst=:11345/},dst=display} :sout-keep}
The batch file is launched correctly, but the query continues to run until the vlc is stopped.
What can I do for stopping the query letting the vlc running?
DISCLAIMER: Don't run these examples in production!
SQL Server watches all processes spawned by xp_cmdshell and waits for them to finish. to see that you can just run this statement:
EXEC xp_cmdshell 'cmd /C "start cmd /K dir"'
This starts a command shell that in-turn starts another command shell to execute the dir command. The first shell terminates right away after spawning the second (/C switch) while the second executes the "dir" and then does not terminate (/K switch). Because of that second lingering process, even though the process that SQL Server started directly is gone, the query won't return. You cannot even cancel it. Even if you close the window in SSMS, the request continues to run. You can check that with
SELECT r.session_id, t.text
FROM sys.dm_exec_requests r
CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) t
The NO_OUTPUT parameter does not help either:
EXEC xp_cmdshell 'cmd /C "start cmd /K dir"', NO_OUTPUT;
You will see the same "sticky" behavior.
The only way to get rid of these is to restart the computer or manually kill the processes (requires taskmanager to be executed as administrator). Restarting the SQL Server service does not stop the spawned processes.
As a solution you can use the SQL Agent. It has a "Operating System (CmdExec)" step type that you can use to run your program. You can create the job and then start it using sp_start_job.
Either way, your process actually needs to finish at some point. Otherwise you will create a pile of process-"bodies" that will cause performance problems in the long run.
Need some help with script files.
I have an SQL script file in the following format:
Begin tran
insert..
select..
update..
Commit
exec linked_server.db1.dbo.storedproc1
I am calling the above script file from within a .js file in the following manner:
var sCommand = "sqlcmd -i C:\\scriptfile1"
var WshShell = new ActiveXObject("WScript.Shell");
var oExec = WshShell.Exec(sCommand);
When I run the .js file, the code between tran-commit gets executed but the storeproc1 is never called. I know for sure that the storedproc1 is not called because it has a list of insert statements that never shows up in the table.
Have you tried running the exec storedproc1 alone? Maybe it throws an error.
Also you can try adding go like this:
commit
go
exec storedproc1
You can try this in the management studio first. After you are sure it works in the management studio, you can go on running it through sqlcmd.
Edit: next you can check the permission of the user running the script, whether it is allowed to run stored procedure.