First of all you need to know how to create
OleDb.Connection[
^] for SQL Server OLE DB Provider.
The second one is to know how to create query:
INSERT INTO [A_TEST].[dbo].[Regs] (RegistrationNo, Column_2, Column_3, Column_4, Column_5, Column_6, Column_7, Column_8)
SELECT PT.RegistrationNo, PT.[Column 2] AS Column_2, PT.[Column 3] AS Column_3, PT.[Column 4] AS Column_4,
PT.[Column 5] AS Column_5, PT.[Column 6] AS Column_6, PT.[Column 7] AS Column_7, PT.[Column 8] AS Column_8
FROM (
SELECT *
FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=E:\Regs.xls', 'SELECT * FROM [Sheet1$]') AS DT
WHERE [RegistrationNo] NOT IN (SELECT RegistrationNo FROM A_TEST.[dbo].[Regs])
) AS PT
In my example:
- database: A_TEST
- table: Regs
- columns: RegistrationNo, Column_1, Column_2,..., Column_8
As you see i use 4 times
SELECT
and 1 times
INSERT
command. First
SELECT
command is used to get data from PT (alias) and to set names for column-headers (if in the destination table they are different from source table). The second
SELECT
command is used to get data from MS Excel sheet and the third (in
WHERE
clause) is used to exclude existing RegistrationNo.
Before you start you need to read more about
OPENROWSET[
^].
Your question is not enough clear, so i can't help you more.