1

I've been struggling to get the defined data table to be accepted as a variable for one of the parameters of the stored procedure.

1st time using user defined tables so I may have made syntax errors

This will be so I can send a data table from a c# program instead of individually inserting each row.

Stored Procedure

CREATE PROCEDURE [dbo].[Upload_AddBulkProducts]
@uploadedTable CSV_ADDProducts readonly
AS
    INSERT INTO [dbo].[Products]
     SELECT * FROM @uploadedTable
GO

Error = undefined variable The error i get when trying to create the stored procedure

User Defined Table

CREATE TYPE [dbo].[CSV_ADDProducts] AS TABLE(
[Product Item] [nvarchar](50) NULL,
[Product SKU] [bigint] NULL,
[Product Name] [nvarchar](max) NULL,
[Product Active] [nchar](10) NULL,
[Product Selling Price] [money] NULL,
[Product Description] [nvarchar](max) NULL,
[Product Purchase Description] [nvarchar](max) NULL,
[Product VAT Code ID] [bigint] NOT NULL,
[Product Last Update] [datetime] NULL
)
GO
2
  • you cannot pass table name as parameter at all , unless you want to use dynamic sql Commented Nov 12, 2020 at 18:28
  • @eshirvana thanks ill have a read up on it:) Commented Nov 12, 2020 at 21:21

1 Answer 1

1

In the context of T-SQL your code is valid. The type can be created and the procedure, too.

Then, in the C# code use the following:

DataTable dt = new DataTable();
dt.Columns.Add("Product Item", typeof (string));
//add other columns here

SqlParameter param = new SqlParameter("@uploadedTable", SqlDbType.Structured)
{
    TypeName = "dbo.userdefinedtabletype",
    Value = dt
};
sqlComm.Parameters.Add(param);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @gotqn , i just got it to work but not too sure how. would you know how to change the sp to T-SQL query?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.