I'm currently trying to get a SQL script working for SQL Server 2005. I'm using SQLCMD to execute the script and it fails giving me an "Error: Syntax error at line 7 near command ':r'" error.
The script:
--Main Script--
SET NOCOUNT ON
GO
:on error exit
:r C:\Documents and Settings\ZSmith\My Documents\Scripts\CreateDatabase
:r C:\Documents and Settings\ZSmith\My Documents\Scripts\CreateTables
I can provide the other SQL scripts if needed but they function fine on their own. I'm just trying to automate the entire process.
From what I know, you need to
put the SQL file name into double quotes - especially if the path contains spaces!
include the .sql extension on the file name
Try this:
:r "C:\Documents and Settings\ZSmith\My Documents\Scripts\CreateDatabase.sql"
:r "C:\Documents and Settings\ZSmith\My Documents\Scripts\CreateTables.sql"
Related
I have more than 1 sql scripts. I want to execute them using only 1 SQLCMD line instead of writing separate SQLCMD line for each script. There's a way where i can create a new .sql file and by using :r . I just need to assign a sql script name in-front :r but the scripts which i am going to execute refers to more than one DB. I prepared the following script but its not working as expected:
Script-1
INSERT INTO [$(dbuser1)].[dbo].[A](Name)
VALUES('Tod')
INSERT INTO [$(dbuser2)].[dbo].[B]( Name )
VALUES ('John')
Script-2
INSERT INTO [$(dbuser1)].[dbo].[A]( Email )
VALUES('tod#gmail.com')
INSERT INTO [$(dbuser2)].[dbo].[B]( Email )
VALUES ('john#gmail.com')
Script-3
SET NOCOUNT ON
SET NOEXEC OFF
-- Quit when error.
:On Error Exit
-- SQLCMD Variables
:setvar dbname1 TestDatabase1
:setvar dbname2 TestDatabase2
:r Script-1.SQL
:r Script-2.SQL
and CMD Line
sqlcmd -S <Serevrname> -i <Script-3 path>
is it a correct way by assigning DB name to variable?
I'm trying to have a job pass in a variable for multiple emails through the command line in SQL Server ManageMent Studio however, it is throwing an error for a specific SET call. FYI, User::gEmailTo is a string in the package.
In my job creation script, I have the following -
--Declaration
:setvar EmailTo "josh#test.com; mike#test.com; earl#test.com; mark#test.com; bill#test.com"
--Syntax throwing the error:
'/SET "\package.Variables[User::gEmailTo].Value";"$(EmailTo)" '
The output in the command line for the job in SSMS shows:
/SET "\"\package.Variables[User::gEmailTo].Value\"";"\"josh#test.com; mike#test.com; earl#test.com; mark#test.com; bill#test.com\""
The error
Argument ""\package.Variables[User::gEmailTo].Value;josh#test.com; mike#test.com; earl#test.com; mark#test.com; bill#test.com"" for option "set" is not valid. The command line parameters are invalid. The step failed.
I need to call another sql file within an sql file using sql plus. This si the script i have so far and its not working. I have to code it in a unix vi file. I am new to this so would someone mind helping. thank you in advance.
call=`$LIVE_SQL/sqlfile2.sql`
if &call = 0 then
echo "ERROR: $LIVE_SQL/sqlfile2.sql file not found"
exit 1
fi
The command to call other SQL files from within SQLPLUS is : start (or #)
Getting feedback from the SQL script : SQLPLUS is not Bash. It is possible, but not really beginner stuff.
I have a problem when I try to include SQL file between a BEGIN ... END statement.
Here is my sql script :
IF #Statement
BEGIN
:r .\myfile.sql
END
When I try to execute it, I have the following error :
Error 160 SQL80001: Incorrect syntax near ':'.
Does anybody knows what I am doing wrong ?
Thank you very much in advance.
BEGIN and END are Transact-SQL statements. They get parsed and executed by SQL Server engine
:r is a sqlcmd extension. The sqlcmd tool uses it to change whatever it sends to SQL Server.
If you send :r to the engine (which you did) you will get a syntax error. :r can be used in sqlcmd, in SSMS when sqlcmd mode is enabled, or you can sue a custom library to do it in your app, like DbUtilSqlCmd.
You could try enabling SQLCMD mode by hitting the button at the top right corner of the page. (At least I think that's where is should be)
Hope that helps.
//Houdini
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.