Search
 
SCRIPT & CODE EXAMPLE
 

SQL

excel vba import data to sql server

Sub UpdateTable()

    Dim cnn As Object
    Dim wbkOpen As Workbook
    Dim objfl As Variant
    Dim rngName As Range
    Workbooks.Open "C:your_path_hereExcel_to_SQL_Server.xls"
    Set wbkOpen = ActiveWorkbook
    Sheets("Sheet1").Select
    Set rngName = Range(Range("A1"), Range("A1").End(xlToLeft).End(xlDown))
    rngName.Name = "TempRange"
    strFileName = wbkOpen.FullName
    Set cnn = CreateObject("ADODB.Connection")
    cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFileName & ";Extended Properties=""Excel 12.0 Xml;HDR=Yes"";"
    nSQL = "INSERT INTO [odbc;Driver={SQL Server};Server=Server_Name;Database=[Your_Database].[dbo].[TBL]]"
    nJOIN = " SELECT * from [TempRange]"
    cnn.Execute nSQL & nJOIN
    MsgBox "Uploaded Successfully"
    wbkOpen.Close
    Set wbkOpen = Nothing

End Sub

Sub InsertInto()

'Declare some variables
Dim cnn As adodb.Connection
Dim cmd As adodb.Command
Dim strSQL As String

'Create a new Connection object
Set cnn = New adodb.Connection

'Set the connection string
cnn.ConnectionString = "Server_Name;Database=Your_Database;Trusted_Connection=True;"

'Create a new Command object
Set cmd = New adodb.Command

'Open the connection
cnn.Open
'Associate the command with the connection
cmd.ActiveConnection = cnn

'Tell the Command we are giving it a bit of SQL to run, not a stored procedure
cmd.CommandType = adCmdText

'Create the SQL
strSQL = "UPDATE TBL SET JOIN_DT = 2013-01-13 WHERE EMPID = 2"

'Pass the SQL to the Command object
cmd.CommandText = strSQL

'Open the Connection to the database
cnn.Open

'Execute the bit of SQL to update the database
cmd.Execute

'Close the connection again
cnn.Close

'Remove the objects
Set cmd = Nothing
Set cnn = Nothing

End Sub
Comment

PREVIOUS NEXT
Code Example
Sql :: SELECT ALL TABLE INFO 
Sql :: mysql dependency for spring boot 
Sql :: SQL:RANK function to delete duplicate rows 
Sql :: oracle create index if not exists 
Sql :: offset in postgresql example 
Sql :: oracle all columns 
Sql :: sql alias 
Sql :: sql alternative to max statement 
Sql :: sql max value in column 
Sql :: sql select where id not exists in another table 
Sql :: postrgesql concat 2 columns divided by ; 
Sql :: FIND ABOVE AVERAGE SALARY EARNER IN SQL 
Sql :: db count rows 
Sql :: sql average 
Sql :: sql creating tables 
Sql :: example database query 
Sql :: sql date between one week 
Sql :: sql: extract day text from datetime value 
Sql :: what is primary key 
Sql :: mariadb check constraint example? 
Sql :: sqlite trim 
Sql :: not null sql 
Sql :: oracle select 
Sql :: min and max salary and name in sql 
Sql :: column with prefix in sql 
Sql :: postgres where 
Sql :: migrations.RunSQL 
Sql :: postgres isnull 
Sql :: in sqlalchemy 
Sql :: mssql xml 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =