INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Powershell SQL sp with Query Results

Powershell SQL sp with Query Results

(OP)
I am trying to do something like the example below.  I want to run a sql query and then execute a sp using the query result.  


QUERY:
    SELECT ID from zSYSUSER WHERE HRID = XXXXX
----->  This returns the result of a zSYSUSERID of YYYYY

SP:
    EXEX spUpdTerminatedStatus 'T','HRID','03/20/2012','YYYYY'
 

RE: Powershell SQL sp with Query Results

(OP)
Where I've gotten so far.  I am using the following to try and save the query results as a variable and then pass to the stored procedure.  

$x = invoke-sqlcmd -query "SELECT ID from zSysUser WHERE HRID = 1111" -database DATABASENAME -serverinstance SERVERNAME

This will save the variable fine, but I believe it is saving the row now the ID

Then to run the SP

invoke-sqlcmd "EXEC spUpdTerminatedStatus 'T','HRID','DATE','$x'" -database DATABASENAME -serverinstance SERVERNAME

This will not run because it is saving $x as system.data.datarow and not system.int32 which the SP is looking for an int for $x

Thoughts?

RE: Powershell SQL sp with Query Results

How are you casting that variable?
Try

CODE --> PowerShell

[int]$x = invoke-sqlcmd -query "SELECT ID from zSysUser WHERE HRID = 1111" -database DATABASENAME -serverinstance SERVERNAME

and if that doesn't work

CODE --> PowerShell

$x = invoke-sqlcmd -query "SELECT ID from zSysUser WHERE HRID = 1111" -database DATABASENAME -serverinstance SERVERNAME

invoke-sqlcmd "EXEC spUpdTerminatedStatus 'T','HRID','DATE','[int]$x'" -database DATABASENAME -serverinstance SERVERNAME

 

Do you have your Tek-Tips.com Swag? I've got mine!

Stop by the new Tek-Tips group at LinkedIn.

RE: Powershell SQL sp with Query Results

(OP)
It was giving me issues with converting datarow to int32 with both your methods.  My workaround, although probably not optimal was

[int]$y = $x[0]
then calling the SP with $y

RE: Powershell SQL sp with Query Results

Okay. Glad you got it to work.  

Do you have your Tek-Tips.com Swag? I've got mine!

Stop by the new Tek-Tips group at LinkedIn.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close