Execute stored procedure by passing the Script File(.sql) as Parameter - sql-server

I have a stored procedure, in wWhich I m passing the script file (.sql file) as a parameter.
I want to know how .sql file gets executed through command (not command prompt).
exec my_sp VersionNumber, SqlDeltaScript.sql (it is a file)
I want my stored procedure to execute SqlDeltaScript.sql
Can anyone please help regarding this ...
Thanks in advance ...

This does not sound like an ideal situation, but if you have to do this then you could use xp_cmdshell to run the sqlcmd utility to run your script file.
The xp_cmdshell SP must be enabled in order ot use it - see Enable 'xp_cmdshell' SQL Server.

Related

BCP Command to Export Query Results to File with Pipe Delimiter

I have a requirement to create a SQL Job that exports a query to a Pipe / Vertical bar delimited file (|) and save it on a network drive in either *.txt or *.csv format. Right now, I am just trying to get this to work inside SSMS, calling the BCP command and exporting the stored procedure in the proper format to the network location, but not able to get it working.
I have been researching this and there are two methods for this.
Use the export data wizard to create a job and schedule that to run. But this method, if we need to make changes, I believe we cannot change the SSIS package that is created so we lose flexibility
Use the BCP command to export the file.
I greatly prefer to use option #2, the BCP command, but I am having problems. I just cannot seem to get the syntax correct and hoping someone could show me what I am missing:
This is my command:
Exec master..xp_cmdshell 'bcp EXEC [dbo].[usp_Report_1123] ''7786'' -t| out \\networkDrive\Reports\REPORT_1123\report1123.csv -T'
But I get the following messages:
output
'out' is not recognized as an internal or external command,
operable program or batch file.
NULL
The stored procedure does work and returns data. The network path, if I enter it into my computer, finds the path. But I am not sure what I am missing and hoping someone could help.

xp_cmdshell command not executing last command when run as job

First off, before everybody shouts at me - I'm bug fixing in legacy code and a re-write is off the cards for now - I have to try to find a fix using the xp_cmdshell command.
I have a proc which is executed via a scheduled job. The proc is full of TSQL like the below to dump data to a log file.
SELECT *
INTO Temp
FROM MyView
SET #cmd1 = 'bcp "SELECT * FROM [myDatabase].dbo.Temp" queryout "C:\temp.txt" -T -c -t" "'
SET #cmd2= 'type "C:\temp.txt" >> "C:\output.txt"'
EXEC master..xp_cmdshell #cmd1
EXEC master..xp_cmdshell #cmd2
DROP TABLE Temp
The problem is that the last of these commands in the proc doesn't appear to run. I can see the result in the text.txt file, but not the output.txt file. All of the preceding work fine though and it works fine when I run this on it's own.
Can anyone suggest why this might happen or suggest an alternative way to achieve this?
Thanks
I think, that BCP as external process runs async. So it could be, that your file is not yet written in the moment you are trying to copy its content.
Suggestion 1: Include an appropriate wait time
Suggestion 2: Call your first command a second time with changed target file name
Suggestion 3: Use copy rather than type
You might create a file c\temp.txt with just hello world in it. Try to type it into one file before the BCP and type it into another file after the BCP.

Passing parameters from CMD file to SQL script

I am somewhat new at this and I am having trouble. I am calling a CMD file that then calls a SQL script to execute some database commands.
The relevant CMD file code is:
set days = '360'
sqlplus #\file.sql %days%
The SQL script excerpt is:
define numOfDays = &1
EXEC (numOfDays, .....)
When executed, I get an error saying the numOfDays variable is undeclared. FYI, this is a Windows OS with a Oracle database. Any help would be appreciated and more info can be provided if needed. Thanks.
EDIT:
Added the '%' signs around the 'days' parameter that were accidentally left out.
You can try by surrounding the param with %.
e.g:
SET days=360
sqlplus #\file.sql %days%
This is how bat/cmd files recognize the variables.

How to read a file's contents into an SQL variable

Could someone tell me how to read a file's contents into an MS SQL variable using T-SQL?
DECLARE #FileContents VARCHAR(MAX)
SELECT #FileContents=BulkColumn
FROM OPENROWSET(BULK'PathToYourFile.sql',SINGLE_BLOB) x; -- BINARY
--FROM OPENROWSET(BULK'PathToYourFile.sql',SINGLE_CLOB) x; -- CHAR
The SQL Server service account needs to have permissions to read the file obviously.
Make use of SQLCMD to execute the .sql (either from command prompt or within SSMS). If you want to use it within SSMS then first turn the SQLCMD mode (Query >> SQLCMD Mode)
Check out http://msdn.microsoft.com/en-us/library/ms174187.aspx
:r yourFilename
something like:
:r d:\scripts\sample.sql

Executing a bat file inside a Stored Procedure using SQL server 2005

When i try to execute a bat file using xp_CMDShell, i am getting a message as not recognized command.
Following is the command i executed:
EXEC master..xp_CMDShell 'C:\Documents and Settings\adcxqcv\Desktop\PQA\sample.bat'
I got a message as follows:
'C:\Documents' is not recognized as an internal or external command,
operable program or batch file.
NULL
Any Suggestions. Let me know how to execute a bat file inside a Stored Procedure.
I am new to SQl Server.
Thanks,
Vinu
Put the path inside ""
EXEC master..xp_CMDShell '"C:\Documents and Settings\adcxqcv\Desktop\PQA\sample.bat"'
xp_cmdshell can be a bit picky on the long file names, you are using quotes and it isn't playing ball, double quotes can sometimes work but if it still doesn't want to play ball then try use the older 8.3 filename instead.
exec master..xp_cmdshell 'c:\docume~1\adcxqcv\Desktop\PQA\sample.bat'
Without parameter
exec(' xp_cmdshell ''C:\script\test.bat'); --your bat file location(path)
With parameter
exec(' xp_cmdshell ''C:\script\test.bat '+#ecistate+' '+#stateid+' '+#pcno+''''); --your bat file location(path)
Execute and enjoy the solution:)

Resources