Link to home
Start Free TrialLog in
Avatar of CipherIS
CipherISFlag for United States of America

asked on

.NET Pass DataTable to Stored Procedure

I want to pass a datatable to a stored procedures.  I found the below article and have been using this as my template.

https://visualstudiomagazine.com/articles/2015/06/01/table-valued-parameters.aspx

I created the below Type successfully but when trying to use it in the stored procedure I'm getting below error.

Msg 137, Level 16, State 1, Procedure POProcess, Line 31 [Batch Start Line 7]
Must declare the scalar variable "@POC".

How do I fix this?

CREATE TYPE POComponent AS TABLE (
	[id_PO] [int] IDENTITY(1,1) NOT NULL,
	[promisedDate] [datetime] NULL,
	[qty] [int] NULL
)

Open in new window

ALTER PROCEDURE [dbo].[POProcess] (
	@POC POComponentType READONLY
) AS

BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

		UPDATE POComponent
		SET promisedDate =	@POC.promisedDate,
		qty =				@POC.qty
		WHERE id_PO =		@POC.id_PO

END

Open in new window

SOLUTION
Avatar of OMC2000
OMC2000
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CipherIS

ASKER

Thanks.  I know that it looks like only 3 fields because that is all I provided in my code, but there are two tables and each have approximately 10 fields each.

There will only be one row of data passed.  That being the case how do I use it as scalar?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Then there will be 20 parameters that I'm passing which is why I would rather create a table.  There are two datatables I need to pass.

So, passing in @prosmiedDate is the scalar variable.  I have to use a cursor to get the data and assign to variables.

Right?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Do you know why the example says:

In the body of the stored procedure, you can use the parameter like any other table. This example joins the parameter to another table in the actual database to find some matching rows and return the result:

Create PROCEDURE dbo.GetJobs
  @JobCriteria JobSpecifications ReadOnly
As

  Select *
    From JobOpenings jo
      Inner Join @JobCriteria jc
        On  jo.JobName = jc.Name
        And jo.StartDate > jc.AvailableDate
Return

I'm finding this not to be true as I would have to use a cursor and put it in another table or variables.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
One more question.  Using the cursor, I will be using two of them.  I will be checking the @@rowcount to ensure that it was successful.

Can I have the Begin Transaction, Commit Transaction, Rollback Transaction outside of the cursor to reverse any changes?  

I am presuming yes.