How to execute SqlPackage command line to generate script without USE statement - sql-server

How to get rid of the USE statement when using SqlPackage command line method when the target DB is a Azure Database ?
When executing:
SqlPackage /a:script [...]
I always get :
GO
:setvar DatabaseName "databasename"
:setvar DefaultFilePrefix "databasename"
:setvar DefaultDataPath ""
:setvar DefaultLogPath ""
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
USE [$(DatabaseName)];
And when executing this script :
sqlcmd -S [...] $pathToUpdateScript
I am getting this error message :
USE statement is not supported to switch between databases. Use a new connection to connect to a different database.
So, How do I remove the USE statement from the script generated by SqlPackage /a:script ? Is there a way to use any command line parameter to support this action ?

Related

Why database creation command fails in SQLCMD in SQL Server docker for linux?

I use docker-compose.yml to load my SQL Server image inside a container.
After it's up and running, I create a command.sh shell and try to run it to create a database.
# command.sh
echo 'creating database from ->' $ModuleName
export query="'create database $ModuleName'"
echo $query
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P my_strong_password -Q $query
And it gives me this error:
Sqlcmd: 'database': Unknown Option. Enter '-?' for help.
Please note that I can't use -i switch to use an input .sql file, because I'm creating my queries programmatically in shell based on environment variables.
The output of sqlcmd -? shows how to use then -Q option. On windows this says [-Q "cmdline query" and exit].
But Windows and Linux differ (or are not consistent) in the use of single- or double quotes.
First option is to try: sqlcmd -Q "\"create database $ModuleName\""
Second option is:
Create a temporary file (i.e. /tmp/tmp.sql), and put the SQL statement in that script.
Use -i /tmp/tmp.sql to execute that script.

How to skip invalid servers in SQLCMD mode in SSMS

I am running sqlcmode mode in ssms agains multiple servers:
:CONNECT SERVER1
script
GO
:CONNECT SERVER2
script
GO
:CONNECT SERVER3
script
GO
etc, however when server2 is not accessible it stops there and won't connect to server3, is there a way to ignore connection errors in SQLCMD mode?
:CONNECT related exceptions stop script execution.
As a possible workaround for SQLCMD mode:
!!sqlcmd -SServerA -Q"select 1"
!!sqlcmd -SServerB -Q"select 2"
!!sqlcmd -SServerC -Q"select 3"
Update: removed :on error ignore

SQL Server Express backup script

I was able to perform backup for my SQL Server Express using this command at the command prompt:
SQLCMD -E -S testing\SQLEXPRESS –Q "BACKUP DATABASE testing3 TO DISK='C:\Users\Administrator\Desktop\test.bak'"
When I want to create a batch script using the command above in order to create a task scheduler to perform auto backup, it shows error as below:
Here is the batch code:
echo off
c: \
CD "C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn"
SQLCMD -E -S testing\SQLEXPRESS –Q "BACKUP DATABASE testing3 TO DISK='C:\Users\Administrator\Desktop\test.bak';"
I didn't put exit at bottom of it because I want to verify whether the command successfully run or not.
ERROR:
Sqlcmd: 'ûQ "BACKUP DATABASE testing3 TO DISK='C:\Users\Administrator\Desktop\test.bak' ': Unexpected argument. Enter '-?' for help.
Does anyone know how to fix this?
EDIT : I solve it by giving a semi column at the end and type it one by one instead of copy paste it

Can snowsql be configured to run a login.sql script and NOT exit?

[edit] now using snowsql Version: 1.2.10
When invoking snowsql with either the -q or -f options, a statement or sql file can be executed. In both of my test cases -- a statement or sql file -- they DO NOT contain an 'exit' yet snowsql exits the connection, returning me to the o/s prompt.
Ideally I want to run 1 or more queries immediately on starting snowsql and then still be connected and at the snowsql prompt. (Functionality like Oracle's login.sql behavior.)
For example, I'd like to be able to alter my session automatically on login. e.g.
alter session set query_tag='my-tag';
A two step approach would be:
Connect through SnowSQL
run !source <filename>
This will execute the SQL in the file without exiting SnowSQL.
I thought using the -q parameter would work. For me it didn't work and returned the following error after connecting:
001003 (42000): SQL compilation error:
syntax error line 1 at position 0 unexpected '/'.
I need to investigate further as I don't have a '/' in my file
Have you already tried to configure Snowsql to keep the client session active?
https://docs.snowflake.net/manuals/user-guide/snowsql-config.html#client-session-keep-alive

SQL cmdshell to execute putty command

I am trying to run a sh file in putty ssh from windows which was done successfully using putty and cmd prompt. When trying the same in xp_cmdshell or through sp_start_job in mssql the query is running for longtime without end.
SQL Query:
exec xp_cmdshell 'D:\path\run.bat'
batch file:
"C:\Program Files\Putty\putty.exe" -ssh root#10.10.10.1 -pw mypass -m D:\path\runcmd.txt
Is the way I am trying correct?
Could you please try this?
EXEC master..xp_CMDShell '"D:\path\run.bat"'
details referred from below link,
Executing a bat file inside a Stored Procedure using SQL server 2005

Resources