sqlcmd

How to conditionally exit .BAT file from SQL code run through SQLCMD

五迷三道 提交于 2020-03-22 14:28:53
问题 I have a .bat (Windows command) file that includes invocations of SQLCMD and other commands. (Of course SQLCMD is sending my T-SQL code to SQL Server.) I want to detect certain conditions in the SQL code, and conditionally exit the entire batch file. I've tried various combinations of RAISERROR, THROW, and deliberate division by 0 (I'm not proud) along with various command line switches on SQLCMD and handling of errorlevel in the .bat file. I tried the answer to 5789568 but could not get it

How to conditionally exit .BAT file from SQL code run through SQLCMD

徘徊边缘 提交于 2020-03-22 14:19:06
问题 I have a .bat (Windows command) file that includes invocations of SQLCMD and other commands. (Of course SQLCMD is sending my T-SQL code to SQL Server.) I want to detect certain conditions in the SQL code, and conditionally exit the entire batch file. I've tried various combinations of RAISERROR, THROW, and deliberate division by 0 (I'm not proud) along with various command line switches on SQLCMD and handling of errorlevel in the .bat file. I tried the answer to 5789568 but could not get it

How to conditionally exit .BAT file from SQL code run through SQLCMD

放肆的年华 提交于 2020-03-22 14:17:51
问题 I have a .bat (Windows command) file that includes invocations of SQLCMD and other commands. (Of course SQLCMD is sending my T-SQL code to SQL Server.) I want to detect certain conditions in the SQL code, and conditionally exit the entire batch file. I've tried various combinations of RAISERROR, THROW, and deliberate division by 0 (I'm not proud) along with various command line switches on SQLCMD and handling of errorlevel in the .bat file. I tried the answer to 5789568 but could not get it

How to conditionally exit .BAT file from SQL code run through SQLCMD

大城市里の小女人 提交于 2020-03-22 14:17:19
问题 I have a .bat (Windows command) file that includes invocations of SQLCMD and other commands. (Of course SQLCMD is sending my T-SQL code to SQL Server.) I want to detect certain conditions in the SQL code, and conditionally exit the entire batch file. I've tried various combinations of RAISERROR, THROW, and deliberate division by 0 (I'm not proud) along with various command line switches on SQLCMD and handling of errorlevel in the .bat file. I tried the answer to 5789568 but could not get it

Sqlcmd: Error: Syntax error at line 17 near command ':r ' in file

孤者浪人 提交于 2020-03-05 21:23:43
问题 I need to execute multiple sql files using a batch file. So, I created a file CREATE_DB.sql with following code: /* SCRIPT: CREATE_DB.sql */ /* BUILD A DATABASE */ -- This is the main caller for each script SET NOCOUNT ON GO PRINT 'CREATING DATABASE' IF EXISTS (SELECT 1 FROM SYS.DATABASES WHERE NAME = 'MSSQLTIPS') DROP DATABASE MSSQLTIPS GO CREATE DATABASE MSSQLTIPS GO :On Error exit :r C:\Users\XYZ\Documents\SQL Server Management Studio\BATCheck\CREATE_TABLES.sql :r C:\Users\XYZ\Documents

Sqlcmd: Error: Syntax error at line 17 near command ':r ' in file

梦想的初衷 提交于 2020-03-05 21:21:11
问题 I need to execute multiple sql files using a batch file. So, I created a file CREATE_DB.sql with following code: /* SCRIPT: CREATE_DB.sql */ /* BUILD A DATABASE */ -- This is the main caller for each script SET NOCOUNT ON GO PRINT 'CREATING DATABASE' IF EXISTS (SELECT 1 FROM SYS.DATABASES WHERE NAME = 'MSSQLTIPS') DROP DATABASE MSSQLTIPS GO CREATE DATABASE MSSQLTIPS GO :On Error exit :r C:\Users\XYZ\Documents\SQL Server Management Studio\BATCheck\CREATE_TABLES.sql :r C:\Users\XYZ\Documents

Sqlcmd: Error: Microsoft SQL Server Native Client 10.0 : Communication link failure

喜欢而已 提交于 2020-02-15 07:51:28
问题 Below, you'll notice some Windows Command Prompt commands that I'm trying to call from the 64 bit Program Files folder for Microsoft SQL Server 2008 C:\Program Files\Microsoft SQL Server\100\Tools\Binn>SQLCMD -S "SQLI2B2" -d "I2B2_CRC-DEV" -i "C:\SVN\ONT\SQL SERVER\DX_SQLSERVER.sql" -E HResult 0x2746, Level 16, State 1 TCP Provider: An existing connection was forcibly closed by the remote host. Sqlcmd: Error: Microsoft SQL Server Native Client 10.0 : Communication link failure. c:\Program

How to use :setvar properly?

痴心易碎 提交于 2020-01-23 06:59:10
问题 I call several sql files using: :r C:\Scripts\Script1.sql :r C:\Scripts\Script2.sql :r C:\Scripts\Script3.sql I have this idea about the :r call from here: TransactSQL to run another TransactSQL script. The Script1.sql script might as well also have other sql script calls in its code, etc. But now I want to define settings for each script. For example: I define LastInsertedID and set it to the value of SCOPE_IDENTITY() right before the the call for Script1.sql . And this script now uses this

Powershell Invoke-Sqlcmd Login Failed

旧街凉风 提交于 2020-01-15 07:02:49
问题 I am trying to run a sql query from powershell for Windows SQL Server 2008 R2. It looks like the code is failing to pass SQL Server Authentication credentials to run the sqlcmd correctly. I am running this first part directly from powershell, not as a script. Set-Location SQLServer:\SQL\Server\ Invoke-sqlcmd "SELECT DB_NAME() AS hist;" -username "username" -password "p@ss" I receive error, Login failed for user 'username'. The ideal end result for the code format would look more like this.

Powershell Invoke-Sqlcmd Login Failed

佐手、 提交于 2020-01-15 07:02:17
问题 I am trying to run a sql query from powershell for Windows SQL Server 2008 R2. It looks like the code is failing to pass SQL Server Authentication credentials to run the sqlcmd correctly. I am running this first part directly from powershell, not as a script. Set-Location SQLServer:\SQL\Server\ Invoke-sqlcmd "SELECT DB_NAME() AS hist;" -username "username" -password "p@ss" I receive error, Login failed for user 'username'. The ideal end result for the code format would look more like this.