Wednesday, December 5, 2018

insert Table range to SQL server Database

Dim rng as Range
Dim defaultDate As string
Dim sql as string, bulkSql as string

Set rng = Range("A1:XX") -- Range of the table. 
defaultDate = Format(Range("A2").Value, "yyyy/mm/dd") 
bulkSql = ""  

'generated sample: insert tRate(ID, Rate, Date) SELECT s.ObjectId, '0.15', '2015/08/24' FROM tSecurity s where s.Number = '007'

For Each row In rng.Rows

    sql= "insert tRate(ID, Rate, Date) SELECT s.ObjectId " & "','" & row.Cells(2).Value & "','" & defaultDate & "' FROM tSecurity s where s.number = '" & row.Cells(1).Value & "'; "

    bulkSql = bulkSql & sql  

Next row

adoCn.Execute bulkSql, , adCmdText

https://stackoverflow.com/questions/32184673/inserting-values-into-sql-server-table-via-vba

No comments: