Suppose I have a table in Excel with two columns (Name, Rate) (say, this table's name tExcel
). Table starts at cell (2,1), and Date is static (in cell (1,1))
I want to insert those values into SQL Server 2008 tRate
table with following logic
insert tRate(ID, Rate, Date)
select
s.ObjectID, e.Rate, Date -- date comes from cell(1,1). DateType is smalldatetime
from
tExcel e, tSecurity s
where
e.Name = s.Number
I've created a connection
Sub disp_cust()
Dim adoCN As ADODB.Connection
Dim sConnString As String
Dim sSQL As String
Dim lRow As Long, lCol As Long
Set cn = New ADODB.Connection
sConnString = "Provider=sqloledb;Server=xxx;Database=xxx;User Id=xxx;Password=xxx"
Set adoCN = CreateObject("ADODB.Connection")
adoCN.Open sConnString
adoCN.Close
Set adoCN = Nothing
End Sub
Thanks for help.
EDIT to @jaimetotal answer
sql= "insert tRate(ID, Rate, Date) SELECT s.ObjectId ," & Replace(Row.Cells(2).Value, ",", ".") & ",'" & defaultDate & "' FROM tSecurity s where s.number = '" & row.Cells(1).Value & "'; "