I've added an extra column to a table which I want to initialize using a query in the post deployment script. Unfortunately I can't seem to write a query which can be run every time so I'm looking for a way to check in the pre-deployment script if the column is available and pass an argument or variable to the post-deployment script which will then run the initialization query once.
Attempt 1: I tried setting a sqlcmd var in the pre-deployment script but the following syntax isn't allowed:
IF COL_LENGTH('dbo.Table','NewColumn') IS NULL
:setvar PerformInitQuery 1
Attempt 2: I've also tried using a normal variable in the pre-deployment script:
DECLARE #PerformInitQuery BIT = 0
IF COL_LENGTH('dbo.Table','NewColumn') IS NULL
SET #PerformInitQuery = 1
And accessing it in the post-deployment script:
IF #PerformInitQuery = 1
BEGIN
:r ".\DeploymentScripts\PerformInitQuery.sql"
END
This last attempt seemed to work when publishing the project from Visual Studio but not on our build server; which uses SqlPackage.exe to publish the generated .dacpac file to the database.
Error SQL72014: .Net SqlClient Data Provider:
Msg 137, Level 15, State 2, Line 12
Must declare the scalar variable "#PerformInitQuery"
You could try using a temp table to hold values you wish to pass from pre to post scripts;
/*
Pre-Deployment Script Template
--------------------------------------------------------------------------------------
This file contains SQL statements that will be executed before the build script.
Use SQLCMD syntax to include a file in the pre-deployment script.
Example: :r .\myfile.sql
Use SQLCMD syntax to reference a variable in the pre-deployment script.
Example: :setvar TableName MyTable
SELECT * FROM [$(TableName)]
--------------------------------------------------------------------------------------
*/
select 'hello world' as [Col] into #temptable
picked up in post deployment script;
/*
Post-Deployment Script Template
--------------------------------------------------------------------------------------
This file contains SQL statements that will be appended to the build script.
Use SQLCMD syntax to include a file in the post-deployment script.
Example: :r .\myfile.sql
Use SQLCMD syntax to reference a variable in the post-deployment script.
Example: :setvar TableName MyTable
SELECT * FROM [$(TableName)]
--------------------------------------------------------------------------------------
*/
declare #var nvarchar(200)
select #var = [Col] from #temptable
print #var
hello world
Update complete.
Related
I am trying to run a script that creates Environment variables for an SSIS package.
Depending on the destination i.e. SIT, UAT or PRD I want my script to use different variable values depending on the destination. In my script I have a variable #DepoyServer and I want this to be populated as a parameter or argument from Devops when using the Execute SQL Script Task.
My code then looks at what this is set to and sets others common variables for each environment
e.g. A database connection string variable will be set to that of the environment.
Example script code would be: -
-- DECLARE #DeployServer varchar(100)
Declare #DBConnectionString varchar(500)
IF #DeployServer = 'UAT'
SET #DBConnectionString = 'ConnectionStringForUAT'
IF #DeployServer = 'PRD'
SET #DBConnectionString = 'ConnectionStringForPRD'
/*
Code to create environment varaiables and populate the variable with #DBConnectionString
*/
The SQL Script file path is set using: -
$(System.DefaultWorkingDirectory)/path/SQLScript.sql
There is a field for Arguments.
I have oogled it to death but ll I'm getting are DACPAC examples.
You define the sql script with sqlcmd variable for deploy server.
:SetVar DeployServer UAT
Declare #DBConnectionString varchar(500)
IF $(DeployServer) = 'UAT'
SET #DBConnectionString = 'ConnectionStringForUAT'
IF $(DeployServer) = 'PRD'
SET #DBConnectionString = 'ConnectionStringForPRD'
Now, call this script in the sqlcmd tool with right value for the DeplyServer
sqlcmd -v DeployServer ="UAT" -i $(System.DefaultWorkingDirectory)/path/SQLScript.sql
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 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"
I am attempting to create a batch file in windows that will take a user's input, and pass that along to a sql file containing the following query, so that I can set a siteid, like in the following sql query:
exec sp_addlinkedserver [sqlserver1]
select * from [sqlserver1].onesource.dbo.admsites where siteid = '123'
I want to then take the results of this query, particularly the admsiteid, and then use the results of the query, and insert that into the originatorid (using another .sql file:
Use Onesource
update OSCsettings set originatorid = 'whatever-the-admsiteid-is'
How would I go about passing along these variables?
sqlcmd with the -v command line
-v var = "value"
You can specify multiple variables in the list.
See:
http://msdn.microsoft.com/en-us/library/ms162773.aspx
and
http://msdn.microsoft.com/en-us/library/ms188714.aspx
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.