Register
: :
Login
Home
Forums
Blogs
Podcast
Directories
Scripts
Downloads
Community
User Group Support
Learning Resources
We have a new sponsor! Introducting
Pragma Systems
. See the home page for details.
Unanswered
Active Topics
Forums
Search
Members
Forums
>
Using PowerShell
>
SQL Server
How to get return value from a SQL store procedure
Last Post 21 Jun 2009 01:33 PM by
gtan
. 2 Replies.
Sort:
Oldest First
Most Recent First
Prev
Next
You are not authorized to post a reply.
Author
Messages
gtan
New Member
Posts:2
18 Jun 2009 03:44 AM
Hi, all
I am new to powershell, and practicing to use .net object in Powershell.
Now I can select/update/insert into SQL tables, but cannot get the return value, and output parameter from a store procedure.
Could someone please give me a sample script for these?
Thanks a lot,
Gary
cmille19
New Member
Posts:51
18 Jun 2009 09:13 PM
Getting output and return parameters is a slightly more difficult, but here's an example:
I created procedure call InsertCategory in the Northwind database as follows:
CREATE PROCEDURE dbo.InsertCategory
@CategoryName nvarchar(15)
,@Identity int OUT
AS
SET NOCOUNT ON
INSERT INTO Categories (CategoryName) VALUES(@CategoryName)
SET @Identity = SCOPE_IDENTITY()
RETURN @@ROWCOUNT
And this is the Powershell script to call the procedure and obtain the output parameter and return values (change the serverName variable for your environment):
$serverName='Z002\SQL2K8'
$databaseName='Northwind'
$query='InsertCategory'
$catName='Test'
$connString = "Server=$serverName;Database=$databaseName;Integrated Security=SSPI;"
$conn = new-object System.Data.SqlClient.SqlConnection $connString
$conn.Open()
$cmd = new-object System.Data.SqlClient.SqlCommand("$query", $conn)
$cmd.CommandType = [System.Data.CommandType]"StoredProcedure"
$cmd.Parameters.Add("@RowCount", [System.Data.SqlDbType]"Int")
$cmd.Parameters["@RowCount"].Direction = [System.Data.ParameterDirection]"ReturnValue"
$cmd.Parameters.Add("@CategoryName", [System.Data.SqlDbType]"NChar", 15)
$cmd.Parameters["@CategoryName"].Value = $catName
$cmd.Parameters.Add("@Identity", [System.Data.SqlDbType]"Int")
$cmd.Parameters["@Identity"].Direction = [System.Data.ParameterDirection]"Output"
$cmd.ExecuteNonQuery()
$conn.Close()
$cmd.Parameters["@RowCount"].Value
$cmd.Parameters["@Identity"].Value
gtan
New Member
Posts:2
21 Jun 2009 01:33 PM
Thank you so much, it works like a charm.
You are not authorized to post a reply.
PowerShellCommunity.org
--Community Announcements and Assistance
--Completely Unrelated
--User Groups
--Community Business
----Suggestion Box
Using PowerShell
--General PowerShell
--Books, Tools, and Videos
--Exchange Server
--Active Directory
--System Center Family
--Non-Microsoft Products
--SharePoint
--SQL Server
--Working with .NET
--Peer Review
--Testing, Testing...
PowerShell Development
--Cmdlet Development
--PSDrive Provider Development
--Hosting the Shell
Looking Ahead
--Using PowerShell v2.0
--Developing for PowerShell v2.0
Forums
>
Using PowerShell
>
SQL Server
Active Forums 4.1
Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008