Solved

Powershell ExecuteNonQuery Returns -1

Posted on 2013-10-31
5
1,037 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:mcazabonnet
  • 3
  • 2
5 Comments
 
LVL 4

Author Comment

by:mcazabonnet
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 39

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:mcazabonnet
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 39

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:mcazabonnet
ID: 39617916
Thx,
I'm going to keep an eye on it and run a few more test

Thx Again!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now