Bulk insert

The following is a proc to accept a data table as parameter and insert it to another table:
CREATE PROCEDURE [dbo].[InsertData]
    @TableType TableType readonly
AS
BEGIN
    insert into [dbo].DataList select * from @TableType; 
END

Open in new window


When I run this code, I get "Parameter or variable '@TableType' has an invalid data type."

Q1: What is correct syntax for line  @TableType TableType readonly?

Q2: Could you please code to create a sample table with two lines of data so I could test this proc?
fileID       filename        createdOn
1              File_1          2014-02-26 	
2              File_2          2014-03-28

Open in new window


Note that fileID is identity column.
LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
CREATE PROCEDURE [dbo].[InsertData]  (
@SourceTable as varchar(50)
)
AS
BEGIN
    insert into [dbo].DataList select * from @SourceTable ;
END
0
Robert SchuttSoftware EngineerCommented:
Using this MSDN example I suggest you try:
CREATE TYPE TableType AS TABLE 
( /*fileID INT IDENTITY(1, 1),*/ [filename] VARCHAR(100), createdOn DATE);
GO

CREATE PROCEDURE [dbo].[InsertData]
    @TableType TableType readonly
AS
BEGIN
    insert into [dbo].DataList select * from @TableType; 
END
GO

-- the calling part

DECLARE @myTableVar AS TableType;

INSERT INTO @myTableVar ([filename], createdOn) VALUES
 ('File_1', '2014-02-26')
,('File_2', '2014-03-28')

EXEC [dbo].[InsertData] @myTableVar;
GO

SELECT * FROM [dbo].DataList;
GO

Open in new window

Note that you don't need the id in the table variable but I left it in the code (commented out) should you need it anyway. But in that case you also need to set IDENTITY_INSERT ON for the DataList table (and OFF again after the insert) which would significantly complicate the situation because you would need to avoid duplicates yourself.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vitor MontalvãoMSSQL Senior EngineerCommented:
eghtebas, do you still need help with this question?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.