Solved

Powershell ExecuteNonQuery Returns -1

Posted on 2013-10-31
5
1,302 Views
Last Modified: 2013-11-01
Hi,
I have a powershell script that kicks off a store procedure.
The ExecuteNonQuery does not execute the stored procedure and returns a -1
I am using a domain account and I can launch the sp successfully from sql mgmt studio
Need assistance on what -1 means

Note:
The script works fine referencing same database but on another dev sql server.
 
I'll add the powershell script shortly.
0
Comment
Question by:Mauro Cazabonnet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 4

Author Comment

by:Mauro Cazabonnet
ID: 39614563
Here's the script
$OutputEncoding = [Text.Encoding]::ASCII
$SQLServer = "server1,2431"
$SQLDBName = "RAS"
 
Write-EventLog "Windows PowerShell" -source PowerShell -eventid 1616 -message "ROusage.ps1 has started"
 
$SQLConn = New-Object System.Data.SqlClient.SqlConnection
$SQLConn.ConnectionString = "Server=$SQLServer;Database=$SQLDBName;Integrated Security=TRUE;"
$SQLConn.Open()
 
Write-Host "SQL Connection Opened..."
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $SQLConn
 
#region Variables
 
#Inputs/Archive Directory
$errlog = "d:\inetpub\ftproot\daily\rousage.error"
#endregion
 
Write-Host "Gathering ROusage data..."
Write-Host "Execute RO Usage Collection"
                                                $CommandText = "RPT_ROUsageNew"
                                               $Command.CommandText = $CommandText
                                               $Command.CommandTimeOut = 0
                                               try
                                {
                                                $Return = $Command.ExecuteNonQuery()
                                }
                                catch [System.Data.SqlClient.SqlException]
                                {
                                               
                                                $Return = 0
                                }
                                Finally
                                {
                                                If ($Return -eq "1") {} else
                                                {
                                                                "Error collecting ROusage data :: " + $CommandText | Out-File $errlog -Append -Encoding ASCII
                                                }
                                }
0
 
LVL 40

Expert Comment

by:lcohan
ID: 39616900
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery(v=vs.110).aspx


"For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1."

I think you must double check what happens if you run the SQL SP using the EXACT NT login via SSMS and check the underlying tables for triggers, FKeys, etc...

Also make sure to put a SET NOCOUNT ON on the top of the code inside your SQL SP
0
 
LVL 4

Author Comment

by:Mauro Cazabonnet
ID: 39616952
Yes I do have SET NOCOUNT ON in the sp
This is what I'm observing
When I kick off sp from SSMS it takes about 14 minutes to complete
Running the same sp from powershell on a remote machine it run's in less than 10 seconds and the data collected from the sp is populated correctly in the tables.

It looks like it working ok, the time differences between SSMS and the powershell are a bit wierd
0
 
LVL 40

Accepted Solution

by:
lcohan earned 500 total points
ID: 39617173
"It looks like it working ok, the time differences between SSMS and the powershell are a bit wierd"

how must data does the SP returns?
is the powershell allways executed AFTER the SP call via SSMS - if yes, cold vs "warm" code and data cache could be your answer.

Either way you should use SET EXECUTION TIME ON in SSMS prior to exec the SP then you will see the actual execution time of that SP.
0
 
LVL 4

Author Closing Comment

by:Mauro Cazabonnet
ID: 39617916
Thx,
I'm going to keep an eye on it and run a few more test

Thx Again!
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question