Passing output parameter from stored procedure
hi,
i have problem passing value output parameter sql stored procedure powershell variable, , value variable send via mail:
script sql stored procedure:
use [master] go alter procedure dbo.test @value nvarchar(4000) output begin set nocount on; set @value = 'error' end go
script powershell:
$sqlconnection = new-object system.data.sqlclient.sqlconnection $sqlconnection.connectionstring = "integrated security=sspi; data source=.\sqlexpress; initial catalog=master;" $sqlcmd = new-object system.data.sqlclient.sqlcommand $sqlcmd.commandtext = "[test]" $sqlcmd.connection = $sqlconnection $sqlcmd.commandtype = [system.data.commandtype]::storedprocedure $sqlcmd.parameters.add("@value", "") | out-null $sqlcmd.parameters["@value"].direction = [system.data.parameterdirection]::output $sqlconnection.open() $sqlcmd.executenonquery() $returnvalue = $sqlcmd.parameters["@value"].value $sqlconnection.close() $smtp = new-object net.mail.smtpclient("test@test.com") $objmailmessage = new-object system.net.mail.mailmessage $objmailmessage.from = "test@test.com" $objmailmessage.to.add("test@test.com") $objmailmessage.subject = "testmail" $objmailmessage.body = $returnvalue $smtp.send($objmailmessage)
the powershell script runs without errors , send me mail, in mail`s body instead of word: "error" apears single character: "e".
can guide me how pass output value , send via mail?
thanks
it works me:
ps c:\scripts> $sqlconnection = new-object system.data.sqlclient.sqlconnection ps c:\scripts> $sqlconnection.connectionstring = "integrated security=sspi; data source=.\sqlexpress; initial catalog=northwind;" ps c:\scripts> $sqlcmd = new-object system.data.sqlclient.sqlcommand ps c:\scripts> $sqlcmd.commandtext = "dbo.test" ps c:\scripts> $sqlcmd.connection = $sqlconnection ps c:\scripts> $sqlcmd.commandtype = [system.data.commandtype]::storedprocedure ps c:\scripts> $p1=$sqlcmd.parameters.add("@value", "") ps c:\scripts> $p1.direction = [system.data.parameterdirection]::output ps c:\scripts> $p1.size=50 ps c:\scripts> $sqlconnection.open() ps c:\scripts> $sqlcmd.executenonquery() -1 ps c:\scripts> $p1.sqlvalue.value hello ps c:\scripts>
i used 'hello' instead of 'error' , northwind db instead of 'master'. add size' , reference correct properties , should work.
\_(ツ)_/
Windows Server > Windows PowerShell
Comments
Post a Comment