Running a T-SQL script under different credentials using SQLCMD.exe utility - sql

I created a batch file and placed the following line into it:
runas /user:internal\c39293 "C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE" -E -S WLDZ9454 -d ChadDb -Q "usp_Test"
I was trying to run the stored procedure usp_Test on the server WLDZ9454 against the ChadDb database.
When I execute it, I just get info regarding param options, no error mesage, so it isn't clear to me what I am doing wrong.
Note that when I run the code minus the code that attempts to run under different credentials, it works:
"C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE" -E -S WLDZ9454 -d ChadDb -Q "usp_Test"

I suspect it's because you have not enclosed the program you are running and its parameters in quotes.
SqlCmd is therefore executed without any parameters.
runas /user:internal\c39293 """C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE"" -E -S WLDZ9454 -d ChadDb -Q ""usp_Test"""

Related

SQL BCP within Powershell hangs

I am new to BCP. I am tying to launch bcp.exe utility from Powershell but it hangs. Same command works fine command prompt. I am using Invoke-Expression to launch bcp.exe.
I am able to launch SQLCMD.exe without any problems.
This is my powershell.
Set-Location -Path "C:\Program Files\Microsoft SQL Server\110\Tools\Binn"
SQLCMD.EXE -b -E -S CORPSYSSQLDEV -d CORPSYSDM -Q "select top 10 * from t_test"
$psCommand = "bcp.exe ""testDB.dbo.t_test"" in ""C:\temp\test\testFile20180919.txt"" -c -t""\t"" -T -S ""TESTSQLDEV"" -e c:\temp\NoahFolder\error.csv"
Write-Host $psCommand
Invoke-Expression $psCommand
This is the result of $psCommand.
bcp.exe "testDB.dbo.t_test" in "C:\temp\test\testFile20180919.txt" -c -t"\t" -T -S "TESTSQLDEV" -e c:\temp\test\error.csv
Which works fine from command prompt but when I run the powershell script its stuck.
I am able to launch SQLCMD.exe from powershell fine.
What am I doing wrong? Any ideas or pointers.
I should have searched more before asking here. This post helped me. I was able to run BCP from powershell using Start-Process command.
$bcp = 'c:\Program Files\Microsoft SQL Server\110\Tools\Binn\bcp.exe'
$arglist = #(
#add all arguments.
)
Start-Process -FilePath $bcp -ArgumentList $arglist

Select after login via Batch (Microsoft SQL Server Management Studio)

I am trying to select data from table and save them info .csv file via .bat file.
My batch runs Microsoft SQL Server Management Studio and log me into database, but I am not able to execute sql scripts.
Here is what I have:
#echo off
cls
:begin
set /p SName=Server Name :
set /p DbName=Database Name :
set /p UName=User Name :
set /p DbPWD=Password :
pause
echo Running Microsoft SQL Server Management Studio, please wait
call ssms -S %SName% -d %DbName% -U %UName% -P %DbPWD% "select * from MYTABLE" -s "," -o " Export.csv"
echo Done
pause
:end
It does not work after line: ssms -S %SName% -d %DbName% -U %UName% -P %DbPWD%
UPDATE:
Error picture
Thank you
SSMS does not accept an in-line script from the command line, as you are trying to do. Please use SQLCMD for this work. that's why you got the error message.
See SSMS Command Line
Straight from Microsoft
sqlcmd -S <ComputerName>\<InstanceName> -i <MyScript.sql> -o <MyOutput.rpt>

Run .sql file from batch file

I'm new to batch files and would like to execute sql script from the batch file. Please can someone assist.Thanks in advance.
You can use sqlcmd.exe from the DOS prompt. If you run sqlcmd -? from the DOS prompt the program will tell you the command line parameters. Here's an example
sqlcmd -S MyDbServer -d DatabaseName -E -i "MyScript.sql"
The -E tells sqlcmd to use "trusted authentication" meaning your Windows login. If you're not using Windows Authentication then you'll want to use the -U and -P parameters.

Need help to make sqlcmd run as per requirements

I wanted to run SQLCMD.EXE as mentioned here - http://blog.daringa.com/archives/tag/error-hresult-e_fail-has-been-returned-from-a-call-to-a-com-component
I used the line in link with proper path to sqlcmd, user and password I use to login to my SQL server - sqlcmd -S .\MYSQLSERVER2008 -U MyUsername -P MyPassword -i C:\Database\hugescript.sql
Problem - I see a window and some message, but it vanishes so quickly that I cannot even see what it is. How do I see this window and how do I then execute an SQL file via SQLCMD.EXE
Why am I executing a script via SQLCMD and not SQL SERVER (ie SS) MGMT STUDIO ?
SS throws an error when sql files are big, ie about 100mb or more.
You can redirect the output from sqlcmd
sqlcmd -S .\MYSQLSERVER2008 -U MyUsername -P MyPassword
-i C:\Database\hugescript.sql > log.txt 2> error.txt
It will write the output to log.txt file and errors to error.txt file (you can specify the full path if you want). You can then see what's happening.

Executing series of SQL commands on the command line

I want to run a series of SQL statements against a SQL Server 2005 database from the command line.
When I launch 1st statement
osql -E -S <Server_Name>\<Instance_Name> -d <Server_Name>
it is going to prompt window 1> from there after I am unable to proceed further through script.
How to give input to 1> prompt I mean giving next SQL statement
BACKUP DATABASE TO DISK = 'c:\test.bak' WITH INIT,SKIP
and finally exit to that prompt
I tried with && but I guess that is for only commandline commands.
You ae looking for the -Q switch on the sqlcmd tool (don't use osql on sqlserver 2005 or higher) (type sqlcmd /? to see all options) or lookit up on msdn
sqlcmd -E -S <Server_Name>\<Instance_Name> -d <Server_Name> -Q "BACKUP DATABASE TO DISK = 'c:\test.bak' WITH INIT,SKIP"
Alternatively you can create a sqlscript file where you put all the sql statements in you want to execute. Assuming you name your file myscript.sql the osql command would go like this:
sqlcmd -E -S <Server_Name>\<Instance_Name> -d <Server_Name> -i myscript.sql
Perhaps you may want to try a small trick that emerged from other question in this forum (that was deleted unfortunately).
You may insert the input for a command directly in the lines below the command and then execute the file NOT as Batch file, but as input por cmd.exe (this is similar to a here document in Linux). For example:
script.TXT:
#echo off
osql -E -S <Server_Name>\<Instance_Name> -d <Server_Name>
BACKUP DATABASE TO DISK = 'c:\test.bak' WITH INIT,SKIP
exit
Execute previous "script" this way:
cmd < script.TXT
If you perform this test, please report the result...
Antonio