轉發先前用作參考的相同示例代碼。
USE <Database>
GO
/* This is a template table */
CREATE TYPE Emp_Details AS TABLE
([Name] VARCHAR(100)
, [Description] VARCHAR(100)
, [Address] VARCHAR(100));
GO
/* The following is your Table Emp_Details which you must be having already*/
CREATE TABLE Emp_Details
([Name] VARCHAR(100)
, [Description] VARCHAR(100)
, [Address] VARCHAR(100));
GO
/* Consider this as your Input Data i.e CSV file or Excel (Note: I have created a table for sample)*/
CREATE TABLE Emp_Details1
([Name] VARCHAR(100)
, [Description] VARCHAR(100)
, [Address] VARCHAR(100));
GO
INSERT INTO Emp_Details1 VALUES ('John','Test','123')
INSERT INTO Emp_Details1 VALUES ('John1','Test1','1234')
INSERT INTO Emp_Details1 VALUES ('John2','Test2','1235')
GO
SELECT * FROM Emp_Details
/* Declare a variable that references the type. So when you reference a `TYPE` it takes the table template which we created previously*/
DECLARE @Emp AS Emp_Details;
/* Add data to the table variable. In your case push the data that you get into the @Emp */
INSERT INTO @Emp ([Name], [Description], [Address])
SELECT [Name], [Description], [Address]
FROM Emp_Details1;
/* Pass the table variable data to a stored procedure. */
EXEC [dbo].[Insert_Deatils] @Emp;
GO
SELECT * FROM Emp_Details
,您聲明此表型@InsertAircraft
爲什麼你會因爲如果存在條件 – 2017-06-14 07:14:19
Edited..now檢查代碼 – Superr
在第一條語句的截斷Emp_Details表然後 – 2017-06-14 07:15:49