Solved

Powershell ExecuteNonQuery Returns -1

Posted on 2013-10-31
5
1,191 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
  • 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

820 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