How to route SQL print output to log file? - sql

Anyone who knows how to route print ' ' in an sql script to a logfile when using Invoke-Sqlcmd?
I tried using sqlcmd -o someoutfile.txt, but it overwrites, it does not append to existing file. And if an SQL error occurs, only the error message is sent to file, not the print ' '.
When using Invoke-Sqlcmd | out-file someoutfile.txt -Append, it appends only Write-Output and eventually SQL errors, but not the print ' ' in the sql script excuted.
Has anyone found a solution for this?

Invoke-SqlCmd implements T-SQL PRINT statements and RAISERROR using the verbose parameter. To capture verbose output, first you'll need to include the parameter in your call to invoke-sqlcmd i.e. invoke-sqlcmd -verbose and next you can do one of two things:
If you're using Powershell V3 or higher you can redirect verbose output:
invoke-sqlcmd -verbose 4>&1 | outfile someoutfile.txt
If you're using Powershell V2 you can't redirect verbose output to a file, however you can use start-transcript to send all screen output to a file. One gotcha with this approach--it will not work with SQL Agent Powershell job step. It will however work with a cmdexec job step which calls powershell.exe.

And one moment...
The command "Invoke-Sqlcmd" has a parameter -SeverityLevel.
SeverityLevel specifies the lower limit for the error message severity level Invoke-Sqlcmd returns to the ERRORLEVEL PowerShell.
Invoke-Sqlcmd does not report severities for informational messages that have a severity of 10!
Severity Level 10: Status Information
This is an informational message that indicates a problem caused by mistakes in the information the user has entered. Severity level 0 is not visible in SQL Server.

Related

SP2-0734: unknown command beginning select

I have continuous automated application deployment building on Azure DevOps server 2019(ex TFS). Part of deploy is checking the Oracle DB status, before running scripts, the script below works for a year, and ones (probably after latest Azure DevOps server update 2019.1) it stops working with an error:
SQL> SP2-0734: unknown command beginning " select..." - rest of line ignored.
SQL> SP2-0734: unknown command beginning " select..." - rest of line ignored.
$chekdbsql = 'select status from v$instance;'
$i = 1
$chkdb = ""
while ($chkdb.Contains("OPEN") -ne 'True') {
Clear-Variable -Name chkdb
$chkdb = ($chekdbsql | cmd /c "sqlplus -s user/password#localhost/ora as sysdba")
if ($chkdb.Contains("OPEN") -eq 'True'){
break
}
echo "Trying to connect to database. Attempt $i"
sleep 10
$i++ }
write-host "Connected! Database's status is 'open'." -ForegroundColor green
If I tried to execute command locally on the machine where the application is built - it's work well.
The space before select makes me think it's a character encoding issue. See e.g. this, this
beginning " select..."
I'm not familiar enough with powershell to know what the problem is. I can think of a workaround, but it's a bit of a hack.
$chekdbsql = "`nselect status from v`$instance;"
This makes sure that whatever garbage characters are getting inserted at the beginning of the string will be on their own line in SQL*Plus. So if you get a SP2-0734, your select command will still run after that. Since it's now a double-quoted string, I escaped the $.

Powershell script as a step in sql job giving error

I am trying to create a sql job which syncs users from a csv file to ad group.
My powershell script is one of the steps of this job. Issue is that my script is supposed to run on another server which has Active Directory but i keep on getting error when i run this step.
My script is following:
invoke-Command -Session Server-Name
Import-Module activedirectory
$ADUsers = Import-csv \\Server-Name\folder\file.csv
foreach ($User in $ADUsers)
{
$Username = $User.sAMAccountName
$group=$user.adgroup
if (Get-ADUser -F {SamAccountName -eq $Username})
{
foreach($group in $groups){Add-ADGroupMember -identity $group -Members $Username}
Write-Output "$username has beeen added to group $group"
}
}
Error i am getting is
Executed as user: Username. A job step received an error at line 2 in a PowerShell script. The corresponding line is 'Invoke-Command -Session Server-Name. Correct the script and reschedule the job. The error information returned by PowerShell is: 'Cannot bind parameter 'Session'. Cannot convert the "Server-Name" value of type "System.String" to type "System.Management.Automation.Runspaces.PSSession". '. Process Exit Code -1. The step failed.
server name has '-' in between so need to know if that is causing the issue
or i am using wrong way to run this script on a different server from a sql job
Any help would be appreciated!
Jaspreet I am not expert on powershell but seems like you are passing the wrong parameters.Just referring to Microsoft docs seems like you need to pass the computer name rather than -Session
Try with this line of code at starting
invoke-Command -ComputerName Server-Name.
For more please refer Microsoft docs
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/invoke-command?view=powershell-6#examples

Capture the Error returned within sql result data set using power shell script

I am new to power shell and trying to execute multiple SQL scripts in a single PS script file. Below is my code,
Function ExecuteQuery($scriptname, $variableExe, $Databasename, $folder, $Optional)
{
invoke-sqlcmd -inputfile $ScriptLocation\$folder\$scriptname -Variable $variableExe -database $Databasename
-username $UsernameExecuteDBScript -password $PasswordExecuteDBScript -serverinstance $ServerName -querytimeout 65535
}
Then I am checking for any errors that may occur using the following code,
if($Error[0].Exception.Message -Match "Error")
{
Write-Host '------------------------------------------------'
Write-Host 'An Error has occured'
Write-Host
Write-Host $scriptname "execution Failed"
I am logging this SQL result, as well as the error messages generated in the command prompt using Start-Transcript
start-transcript $file
ExecuteQuery script1.SQL $null $dbname xy 0
ExecuteQuery script2.SQL $xyz $dbname xy 0
ExecuteQuery script3.SQL $null $dbname xy 0
I execute my PS script using a .bat file.
But I have an issue with this, if any SQL query completed it's execution successfully , but the table returned has columns saying "Error 547, FK violation" or some other similar error messages, my powershell script or Start Transcript do not capture it. It just goes on and runs the script and next scripts in the list.
Below is a sample data returned by the sql execution. The query completes it's execution and goes on to the next script. But the actual execution of the delete did not happen due to the FK violation. Data is returned in the table with 3 columns
ERR_NUMBER : 547
ERR_MESSAGE : The DELETE statement conflicted with the REFERENCE constraint "FK
_abc_sample". The conflict occurred in database "sample", table "dbo.sample_LOOKUP", column 'CONFIG_ID'.
ERR_LINE : 201
I want the Powershell script execution to be stopped/terminated and log the information into my log file when this happens.
Can anyone please help me out with this

Calling SQL*PLUS from UNIX Shell script and print the status message about the query

I want to run a SQL code using shell script and return the message whether the SQL query executed successfully or not. For this I have used unix script given below.
#!/bin/sh
sqlplus -S hr/hr#xe<<EOF
#emp.sql
EOF
var1=$(cat /cygdrive/d/scripts/output.txt | grep -c 'COUNT')
if [ $var1 -ge 1 ];
then
echo "success"
else
echo "failure"
fi
exit;
and emp.sql(called sql file) as
SET ECHO OFF
SPOOL D:\scripts\output.txt
SET LINESIZE 100
SET PAGESIZE 50
SELECT count(*) FROM employees;
SPOOL OFF;
EXIT 0;
When I execute the script I am getting output as
COUNT(*)
----------
107
./script1.sh: line 13: syntax error: unexpected end of file.
I don't know where I should put EOF statement exactly. Also I am not getting the status message whether it is success or failure which I want as output. Please help. Thanks in advance
SPOOL D:\scripts\output.txt Isnt this windows way of referring to a file where as in the shell script you referred to the file as /cygdrive/d/scripts/output.txt. I assume you are using linux shell to execute so I executed your script changing the spool line in sql file. It worked fine.
Edit: Also the \ that you used, for the spooled output.txt path, will cause the sqlplus to terminate. Hence the error line 13: syntax error: unexpected end of file. Perhaps add quotes to the path or use the same file path as you used in shell

Error testing and control from DOS

I'm running DOS 6.0.6002 on a windows server enterprise system, SP2.
SQL Server 2008 R2 (10.50.4000)
I have a main control program in DOS.
I'm invoking an sql program through sqlcmd.
A simplified version looks like this:
set sqlsvr=myServer
set logfile=logfile.txt
sqlcmd -S %sqlsvr% -d myDB -i import_some_stuff.sql > "%logfile%" 2>&1
echo error level = %ERRORLEVEL%
I need this program to be pretty robust. It has to run every day against a lot of files and tables. If it fails, I need to catch it and notify sysadmin. For now, just catch it.
So to test this, I've tried the following tests:
1) Renaming the file to one that does not exist.
Result: it returns and errorlevel of 1 (that is it caught the error!) bravo!
2) typing in some syntactical rubbish at the front of the sql program.
Result: it prints the error message in the log file, BUT it DOES NOT return an error (so the return value in %ERRORLEVEL% is zero. This seems incredible to me. What am I missing?
Try the -b option to sqlcmd:
-b
Specifies that sqlcmd exits and returns a DOS ERRORLEVEL value when an
error occurs.
The value that is returned to the DOS ERRORLEVEL
variable is 1 when the SQL Server error message has a severity level
greater than 10; otherwise, the value returned is 0. If the -V option
has been set in addition to -b, sqlcmd will not report an error if the
severity level is lower than the values set using -V. Command prompt
batch files can test the value of ERRORLEVEL and handle the error
appropriately. sqlcmd does not report errors for severity level 10
(informational messages).
If the sqlcmd script contains an incorrect comment, syntax error,
or is missing a scripting variable, ERRORLEVEL returned is 1.
Here is the documentation