My question is regarding to SQL schema compare.
Database Schema Compare
When i generate script using SQL schema compare, script is generating in below format.
/*
Deployment script for Student
This code was generated by a tool.
Changes to this file may cause incorrect behavior and will be lost if
the code is regenerated.
*/
GO
SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER ON;
SET NUMERIC_ROUNDABORT OFF;
GO
:setvar DatabaseName "Student"
:setvar DefaultFilePrefix "Student"
:setvar DefaultDataPath "F:\Data\"
:setvar DefaultLogPath "H:\Logs\"
GO
:on error exit
GO
/*
Detect SQLCMD mode and disable script execution if SQLCMD mode is not supported.
To re-enable the script after enabling SQLCMD mode, execute the following:
SET NOEXEC OFF;
*/
:setvar __IsSqlCmdEnabled "True"
GO
IF N'$(__IsSqlCmdEnabled)' NOT LIKE N'True'
BEGIN
PRINT N'SQLCMD mode must be enabled to successfully execute this script.';
SET NOEXEC ON;
END
GO
USE [$(DatabaseName)];
GO
PRINT N'Altering [dbo].[usp_InsertStudentData]...';
GO
.....
If i run below query in SQL Cmd mode then it is suppose to run.
But i dont want to run this query in SQL Cmd mode as our dba not approve this
Now my question is...
I need to execute script which not include SQL Command mode code ":setvar DatabaseName"
Let me know if you need more help in my question.
Related
I'm trying to execute some SQL file that contain a lot of SQL files inside. Just as a main.sql that contains all the others that should be executed.
main.sql :
SET ANSI_NULLS ON;
SET QUOTED_IDENTIFIER ON;
SET ANSI_PADDING ON;
#new_instance.sql
#new_instance_tbl.sql
#new_instance_views.sql
#new_instance_tbl_configs.sql
When I open the file in my SQL Server, the red underline appears:
Incorrect syntax near '#new_instance.sql'
and then if I try to execute this, the same failure appears, so...
How can I execute different "included" .sql files from one main SQL file?
If you are using SSMS (SQL Server Management Studio) you can enable SQLCMD Mode
However, the syntax for referencing and executing external files is a bit different, see this answer to: TransactSQL to run another TransactSQL script
:r C:\Scripts\Script1.sql
:r C:\Scripts\Script2.sql
:r C:\Scripts\Script3.sql
Please forgive me, I am fairly new to the art of crafting SQL Server triggers.
I've crafted a SQL Server trigger that will execute a PowerShell script (to send a JSON message to entity X) after a particular table has been updated. The script ran successfully as expected alone in DEV. However when instantiated as a trigger it caused an error on the Front End UI after the user submits an update. The users update did not post, and obviously did not instantiate the trigger.
I'm guessing it has something to do with table locks during the posting of the user input via the Web UI, but it's just a guess. Is there something I should consider in the trigger that would not interfere with the front end UI's controls process of updating the table first before my trigger runs?
This is my (rather primitive) trigger for everyone's perusal
USE [Hamburger_Chefs32];
GO
SET ANSI_NULLS ON;
GO
SET QUOTED_IDENTIFIER ON;
GO
CREATE TRIGGER [dbo].[WD_SendIngredientsMessageOnScoreOverrideUPD]
ON dbo.DeliciousBurgers
AFTER UPDATE
AS
BEGIN
DECLARE #cmd sysname
SET #cmd = 'powershell -File "E:\Program Files (x86)\TheWhopperCorporation\Burgers\v1.0.0\service\SendIngredients.ps1"'
EXEC xp_cmdshell #cmd
END
GO
My humble thanks in advance for any help provided.
Update: Had a suggestion not to run the script from within the TRIGGER as it would have to wait for it to finish. Good point. Is there a way to simply execute the script without having to wait for a success (1), or fail (0) from the script? It runs perfectly 100% of the time, but I don't want to suffer a rollback of the UPDATE because of timing and/or dependency on the script.
Change your trigger this way:
CREATE TRIGGER [dbo].[WD_SendIngredientsMessageOnScoreOverrideUPD]
ON dbo.DeliciousBurgers
AFTER UPDATE
AS
BEGIN
set xact_abort off;
begin try
DECLARE #cmd sysname
SET #cmd = 'powershell -File "E:\Program Files (x86)\TheWhopperCorporation\Burgers\v1.0.0\service\SendIngredients.ps1"'
EXEC xp_cmdshell #cmd
end try
begin catch
print ERROR_MESSAGE()
end catch
END
This way you'll catch the error.
The most probable error here is
The EXECUTE permission was denied on the object 'xp_cmdshell',
database 'mssqlsystemresource', schema 'sys'.
Unless the user that runs your app is sysadmin, or is granted explicitely this permission, the error will occur.
And the whole transaction is rolled back, that is why "The users update did not post".
We're using Visual Studio Database Professional and it makes heavy use of SQLCMD variables to differentiate between environments while deploying.
I know there's several directives available for setting context (like :connect for server name). Is there a way within the script itself to force SQLCMD mode for execution? Part of our deployment process is to have DBA's examine and execute the scripts and it would be a nice safety net (so I don't have to remind them to set their execution mode to SQLCMD).
Not a solution, but as a work-around, you could embed your script in some warning. This post inspired me to this code:
SET NOEXEC OFF; -- previous execution may have toggled it
:setvar IsSqlCmdEnabled "True"
GO
IF ('$(IsSqlCmdEnabled)' = '$' + '(IsSqlCmdEnabled)')
BEGIN
PRINT('Use SqlCmd-mode!!');
SET NOEXEC ON;
-- RAISERROR ('This script must be run in SQLCMD mode.', 20, 1) WITH LOG
END
ELSE
BEGIN
PRINT('Using SqlCmd-mode')
-- insert the code you really want to execute:
-- ...
END
SET NOEXEC OFF; -- do not disable next execution in this session
This does not seem to be possible. I even checked the SSMS project mode.
However, if you create a database project in BIDS, the pre-deploy & post-deploy scripts run in SQLCMD mode by default.
I know that is not the answer you want, but it is the best I can give you w/o resorting creating a custom SSMS plugin that would do it for you based on some text in the script file.
I'm facing a problem now. I use ruby and SQLCMD to generate some TSQL scripts. Now I want to check the syntax of the generated script. I use the following SQL:
SET PARSEONLY ON;
SELECT 888
SET PARSEONLY OFF;
I test it in SSMS, when you select these three statements as a whole batch, sql server will give me the result, which is 888. I don't want the actual result, I just want to test whether the syntax of my script is right. So I tried the following SQL:
SET PARSEONLY ON;
GO
SELECT 888
GO
SET PARSEONLY OFF;
GO
Now if I select all these statements as a whole batch, SQL Server will just tell me Command(s) completed successfully. So what I want to know is whether the SET command should in it's own batch?
Yousui, No what you are doing is almost right; Read here for more info.
Just one thing: you cannot use SET PARSEONLY ON and SET PARSEONLY OFF in the same statement because the query would be executed and not parsed.
So try this:
SET PARSEONLY ON
*-- YOUR QUERY*
GO
SET PARSEONLY OFF
Just remember that PARSEONLY only checks the syntax, not the objects, like tables etc. If you want to do object validation and checks use NOEXEC;
SET NOEXEC ON GO
*-- YOUR QUERY HERE*
SET NOEXEC OFF
I am trying to create some script variables in T-SQL as follows:
/*
Deployment script for MesProduction_Preloaded_KLM_MesSap
*/
GO
SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER ON;
SET NUMERIC_ROUNDABORT OFF;
GO
:setvar DatabaseName "MesProduction_Preloaded_KLM_MesSap"
However, when I run this, I get an error stating 'Incorrect syntax near ':'. What am I doing wrong?
The :setvar only works in SQL command mode, so you are possibly within normal SQL execution in the management studio and have not swapped to command mode.
This can be done through the user interface in SQL Server Management Studio by going to the "Query" menu, and selecting "SQLCMD mode."
Just enable sqlcmd mode in SQL Server Management Studio as described in following image.
FOR SQL2012:
go to :
tools/options/Query Execution and check the by default, open new queries in SQLCMD mode.
Hit the New Query button and make sure the variable definitions are highlighted, your script should run correctly now.
Previous versions:
http://blog.sqlauthority.com/2013/06/28/sql-server-how-to-set-variable-and-use-variable-in-sqlcmd-mode/
try replacing :setvar DatabaseName "MesProduction_Preloaded_KLM_MesSap"
with:
USE [MesProduction_Preloaded_KLM_MesSap]
GO