Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Pass table valued parameter to Stored Proc from Powershell

Posted on 2013-01-18
4
1,735 Views
Last Modified: 2013-01-22
Is it possible to pass in a table valued parameter to a stored procedure from Powershell using the Invoke-SqlCmd?  

This is what I've tried so far from my googling but I'm not having any luck.  

$query = "EXEC TestProc @DbStats = `$(DBStats)"

Invoke-Sqlcmd -ServerInstance LOCALHOST\DEFAULT -Database MyDb -Query $query -Variable "DBSTATS='$DBStats'"

Open in new window


The error I'm getting is "Invoke-Sqlcmd : Operand type clash: varchar is incompatible with DBStatsTable"

DBStatsTable is my user defined table type.  Whenever I do a get-member on $DBStats it's coming back as an array object so I'm not sure why it's being passed in as a varchar.

Edit: I could also pass in an XML data type but I'm not having any luck that way either.  

Invoke-Sqlcmd -ServerInstance LOCALHOST\DEFAULT -Database MyDB -Query $query -Variable "DBStats='$TestXML'"

Open in new window


This doesn't cause any errors but all I'm getting from the proc is "System.Xml.XmlDocument"
0
Comment
Question by:tim_cs
4 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 38796424
It is telling you the value you are passing does not match with the datatype expected on the db side:


you need to look at the ddl for your table and verify the datatype you are passing into dbstats then simply use powershell to convert the string to that datatype before passing into your invoke-sql


Not much else I can offer without knowing the design of the table, but that seems to be a likely culprit.
0
 
LVL 42

Expert Comment

by:EugeneZ
ID: 38796755
as per many posts regarding this error
you need to check what datatype your are passing vs. what it is expected to be


http://forums.asp.net/t/1167056.aspx

you need to make sure the data types are correct

<to pass in a table valued parameter to a stored procedure from >

try to test in the sql server without PS and with later- adjust as needed
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 38796773
It looks like you need to define the TVP as [System.Data.SqlDbType]::Structured.  Here are a couple of examples using Powershell with TVPs:
http://technet.microsoft.com/en-us/library/bb510489.aspx  (see the example at the end)
http://sev17.com/2012/04/13/table-valued-parameter-example/
0
 
LVL 15

Author Closing Comment

by:tim_cs
ID: 38805884
Thanks!
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Synchronize a new Active Directory domain with an existing Office 365 tenant
A brief introduction to what I consider to be the best editor for PowerShell.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

860 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