Solved

Pass table valued parameter to Stored Proc from Powershell

Posted on 2013-01-18
4
1,706 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

773 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