SQL in Excel: Cannot call declared variable - sql

I am trying to breakdown my SQL string in Excel VBA, storing my variables in the first part of the SQL, and calling them in the second part. It seems that declared variables is not my friend here?
First part(Declaring my variable)
Dim rst As ADODB.Recordset
Dim cnn As ADODB.Connection
Dim sSQL As String
Set rst = New ADODB.Recordset
rst.CursorLocation = adUseClient
rst.LockType = adLockReadOnly
Set cnn = New ADODB.Connection
cnn.CommandTimeout = GCL_dbTimeout
cnn.Open "XXX"
rst.ActiveConnection = cnn
sSQL = "DECLARE #Id AS INT = 1234 ; SELECT #Id AS [Id] INTO #cte_TEMP;"
cnn.Execute (sSQL)
Second part(Calling my variable)
Using a temp table works:
rst.Open "SELECT * FROM #cte_TEMP;"
But calling the variable doesn't:
rst.Open "SELECT #Id;"

In-memory #variables are only in-scope during the execution of a single command; once rst.Open returns, the variable is gone and no longer exists.
By storing it into a #temp table, you are persisting the value to physical disk storage, which leaves it available to other subsequent commands.
Don't forget to DROP TABLE #cte_TEMP; at one point! :)
Note: #temp tables are only accessible from the user that created it. If you need to access it from a different connection string, you need to use ##temp tables instead.

Related

VBA SQL pulled data coming back as empty

I'm pulling into Excel VBA from a SQL ADODB Connection and it seems that some fields are coming back as empty that have values in SQL. I'm very green in VBA (just diving back into a legacy application to try and migrate everything to SQL Database storage instead of CSVs)
Here's an example of the value return (just a " where we should have "Sample Data | QRSTE/ S179399")
The code to pull:
Sub GetDFInfoByDf(recordID As String, connectionString as String)
Dim connectionString As String
connectionString = connectionString
Dim command As String
command = "Select * FROM data_table WHERE id = '" & recordID & "'"
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
conn.Open connectionString
rs.CursorLocation = adUseClient
rs.CursorType = adOpenStatic
rs.LockType = adLockBatchOptimistic
Set rs = conn.Execute(command)
Dim rsMatrix As Variant
rsMatrix = rs.GetRows(1)
If IsNull(rs) Then
'rs is null
MsgBox "Pulled recordset is null"
Else
Call FillObjValuesFromRecordSet(rs)
End If
I see that we have a somewhat special character in there (|)
In terms of any SQL Encoding configurations: It's most likely UTF-8. I would think that I have to convert that to ANSI either in VBA or on the SQL side, but have been running in circles to try and figure that out.
Note that this field is NVARCHAR in SQL
Any ideas on how to handle this? Documentation is very sparse on the subject, from what I've seen.
Thanks!
Things I've Tried:
Adding Session Mode=ANSI; to the connection string
Expected Outcome:
-SQL stores the varchar "Sample Data | QRSTE/ S179399" (no quotes in the field)
-I'm expecting my Select to return that exact varchar/string value instead of the return in the image (")
Solution:
My SQL table columns with varchar(max) or nvarchar(max) were not able to translate back.
My initial table had larger-than-needed sizes, so altering those columns to varchar(8000) and nvarchar(4000) fixed the issue!
Thanks

How to pass SQL query from Excel/vba which contains another query as string

I'm using Excel/vba to send query to SQL Server. The query is something like:
using database
declare #queryString as nvarchar(max)
create table #tempTable (col1 int, col2 int, col3 int)
insert into #tempTable
select top 10 col1, col2, col3
from sometable
set #queryString='select * from #tempTable where col1>10'
exec(#queryString);
If I run this query in SQL Server Management Studio, I get the desired results, but when I run it from Excel/vba, I get an error
Operation is not allowed when the object is closed.
I tried to pass it to SQL Server Management Studio like this (where queryExample is my query):
Dim rs As New ADODB.Recordset
Set Connection = New ADODB.Connection
Connection.ConnectionString = "... connection string ..."
Connection.Open
With rs
.ActiveConnection=Connection
.Open queryExample
End With
and like this
Dim rs As New ADODB.Recordset
Dim cmd As New ADODB.Command
Set Connection = New ADODB.Connection
cmd.CommandText = queryExample
cmd.CommandType = adCmdText
cmd.ActiveConnection = Connection
Set rsSR = cmd.Execute
but nothing is working, and I always get the same error. I think that problem is in exec() function, because if I pass query like this:
select top 10 col1, col2, col3 from sometable
I don't get any error and recordset is populated with data.
What am I doing wrong? Any help would be appreciated.
Thanks.
EDIT:
I made a mistake when copying vba code for the last example to pass the query to SQL Server Management Studio, the actual code was:
Dim rs As New ADODB.Recordset
Dim cmd As New ADODB.Command
Set Connection = New ADODB.Connection
Connection.ConnectionString = "... connection string ..."
Connection.Open
cmd.CommandText = queryExample
cmd.CommandType = adCmdText
cmd.ActiveConnection = Connection
Set rsSR = cmd.Execute
Conection was opened in both cases - value of .State of Connection object was 1.

How do I import data from a Teradata table to MS access in an automated way?

I am new to MS Access. I have been trying to import data from a Teradata table to MS Access database. I could establish the connection between the two using VBA. However, I am not being able to write the contents to the access database.
For Excel, we generally use objects like sheets and range to populate the values. What are Access counterparts of these objects?
Given below is the code that I have been using:
Sub TBEN_PR_DSM_SEAS()
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Dim cmdSQLData As ADODB.Command
Set cmdSQLData = New ADODB.Command
Dim query As String
cn.Open "DRIVER={Teradata}; DBCNAME=ABC2; Persist Security Info=True; User ID= ******; Password=******; Session Mode=ANSI;"
Set cmdSQLData.ActiveConnection = cn
query = "SELECT * FROM PRODBBYCIADHOCWRK.TBEN_PR_DSM_SEAS;"
cmdSQLData.CommandText = query
cmdSQLData.CommandType = adCmdText
cmdSQLData.CommandTimeout = 0
Set rs = cmdSQLData.Execute()
End Sub
Can anyone please help me out with the rest of the part? I am using Access 2007-2010.
Thanks and regards,
Nirvik
MS Access is an interesting piece of software as it can serve as both a RDMS database and GUI console to a database. By default, it connects to the Jet/ACE SQL Engine (Windows .dll files) which would compare to SQLite another file-level RDMS. However, with MSAccess.exe Office program, this default can be switched or supplemented with any other ODBC/OLEDB compliant database including the server-level RDMS (Oracle, SQL Server, MySQL, Sybase, even Teradata) using linked tables. And in connecting to external backends it would compare to MySQL's phpmyadmin, SQL Server's Management Studio, PostgreSQL's pgAdmin, and other consoles.
Therefore, consider creating a linked table to Teradata using DoCmd.TransferDatabase where changes reflect on both ends without constant import and export of table data:
DoCmd.TransferDatabase acLink, "ODBC Database", _
"ODBC;DRIVER={Teradata}; DBCNAME=ABC2; Persist Security Info=True; User ID= ******;" _
& "Password=******; Session Mode=ANSI;", acTable, "TBEN_PR_DSM_SEAS", "NewAccessTable"
And for a static, local copy (which would add redundancy to your application needs) you can run an append or make-table query in Query Window or VBA's DoCmd.RunSQL or CurrentDb.Execute to a local Access table using above linked table.
INSERT INTO NewAccessTable SELECT * FROM [TBEN_PR_DSM_SEAS]
SELECT * INTO NewAccessTable FROM [TBEN_PR_DSM_SEAS]
Insert the data in the recordset into the Access table, using new recordset, Connection and Command objects.
Sub TBEN_PR_DSM_SEAS()
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Dim cmdSQLData As ADODB.Command
Set cmdSQLData = New ADODB.Command
Dim query As String
cn.Open "DRIVER={Teradata}; DBCNAME=ABC2; Persist Security Info=True; User ID= ******; Password=******; Session Mode=ANSI;"
Set cmdSQLData.ActiveConnection = cn
query = "SELECT * FROM PRODBBYCIADHOCWRK.TBEN_PR_DSM_SEAS;"
cmdSQLData.CommandText = query
cmdSQLData.CommandType = adCmdText
cmdSQLData.CommandTimeout = 0
Set rs = cmdSQLData.Execute()
'Up to here is your code.
'Asuming you have a table in Access with identical number of fields, and field names:
dim dRst as dao.Recordset, fld as Variant
set dRst = CurrentDb.("AccessTable")
Do While Not Rs.EOF
dRst.AddNew
For Each fld in dRst.Fields
dRst.Fields(fld.Name) = rs.Fields(fld.Name)
Next
'Update an entire record:
dRst.Update
Rs.MoveNext: Loop
End Sub
Tables are Sheets and Queries select Ranges.
Fields are Columns and Records are Rows.
'Loop through Records
Do Until rs.EOF
'rs.Fields(0) is the first field returned from the Teradata query
'executed in your initial question.
Debug.Print rs.Fields(0)
rs.MoveNext
Loop
'Append to Table
'Have to create Table1 in Access database.
'Table1
'Field Name: Column1
'Data Type: Text
If rs.BOF = False Then rs.MoveFirst 'BOF = Beginning of file
DoCmd.SetWarnings False
Do Until rs.EOF 'EOF = End of file
DoCmd.RunSQL ("INSERT INTO Table1 (Column1) SELECT '" & rs.Fields(0) & "'")
rs.MoveNext
Loop
DoCmd.SetWarnings True
cn.Close
Set cn = Nothing

SQL Variables not working in Excel Query

Hi I have a query that can not be represented graphically in Excel External SQL Server Query, because the query is not simple I can not set Excel Variables. Instead I try to set variables in the SQL using declare/set however when doing this it fails when trying to import the data.
Is there a way to get around this? I need a level of flexibility for running the query and piping the data into Excel..
Okay so here is an example as requested.
declare #enddate as datetime
set #enddate = (select max(rpt_week) from [results].dbo.t_Data)
Select * from results.dbo.t_Data
where rpt_week = #enddate
This will not run in excel, obviously my query is more complex but the use of variables is essential and I am looking for a way to have this work.
Thanks
When using declare values in Excel, you need to SET NOCOUNT ON
your sample would be as follows:
SET NOCOUNT ON;
declare #enddate as datetime
set #enddate = (select max(rpt_week) from [results].dbo.t_Data)
Select * from results.dbo.t_Data
where rpt_week = #enddate
There is another question on this subject which has a correct answer:
Use SET NOCCOUNT ON in your query.
I encountered the same problem as the OP when I tried to run an SP from Excel, which was supposed to return data from an embedded SELECT. It was caused by a ... row(s) affected message being returned from the server. Apparently, Excel can't deal with this message correctly, and the returned data is ignored. Once I used SET NOCOUNT ON in my SP, the data was displayed correctly in Excel.
I am not quite sure what you wish, but here are some notes:
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim cmd As New ADODB.Command
cn.Open ServerCon ''Connection string
''Straight sql
sSQL = "SELECT * FROM table_1 WHERE adate = (SELECT Max(adate) FROM table_1)"
rs.Open sSQL, cn
''Not a good idea, see procedure parameter below for a better way
sSQL = "SELECT * FROM table_1 WHERE adate = '" _
& Sheets("Sheet7").Range("B2") & "'"
rs.Open sSQL, cn
''Stored procedure
rs.Open "ExcelTest", cn
''Stored procedure with parameter
cmd.ActiveConnection = cn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "ExcelTest"
cmd.Parameters("#enddate") = Sheets("Sheet7").Range("B2")
Set rs = cmd.Execute
''Write to Excel
ActiveWorkbook.Sheets("Sheet12").Cells(1, 1).CopyFromRecordset rs
You can also use Query Tables.

How to get id of newly inserted record using Excel VBA?

Seems a common enough problem this, but most solutions refer to concatenating multiple SQL commands, something which I believe can't be done with ADO/VBA (I'll be glad to be shown wrong in this regard however).
I currently insert my new record then run a select query using (I hope) enough fields to guarantee that only the newly inserted record can be returned. My databases are rarely accessed by more than one person at a time (negligible risk of another insert happening between queries) and due to the structure of the tables, identifying the new record is normally pretty easy.
I'm now trying to update a table that does not have much scope for uniqueness, other than in the artificial primary key. This means there is a risk that the new record may not be unique, and I'm loathe to add a field just to force uniqueness.
What's the best way to insert a record into an Access table then query the new primary key from Excel in this situation?
Thanks for the replies. I have tried to get ##IDENTITY working, but this always returns 0 using the code below.
Private Sub getIdentityTest()
Dim myRecordset As New ADODB.Recordset
Dim SQL As String, SQL2 As String
SQL = "INSERT INTO tblTasks (discipline,task,owner,unit,minutes) VALUES (""testDisc3-3"",""testTask"",""testOwner"",""testUnit"",1);"
SQL2 = "SELECT ##identity AS NewID FROM tblTasks;"
If databaseConnection Is Nothing Then
createDBConnection
End If
With databaseConnection
.Open dbConnectionString
.Execute (SQL)
.Close
End With
myRecordset.Open SQL2, dbConnectionString, adOpenStatic, adLockReadOnly
Debug.Print myRecordset.Fields("NewID")
myRecordset.Close
Set myRecordset = Nothing
End Sub
Anything stand out being responsible?
However, given the caveats helpfully supplied by Renaud (below) there seems nearly as much risk with using ##IDENTITY as with any other method, so I've resorted to using SELECT MAX for now. For future reference though I would be interested to see what is wrong with my attempt above.
About your question:
I'm now trying to update a table that
does not have much scope for
uniqueness, other than in the
artificial primary key. This means
there is a risk that the new record
may not be unique, and I'm loathe to
add a field just to force uniqueness.
If you are using an AutoIncrement for your primary key, then you have uniqueness and you could use SELECT ##Identity; to get the value of the last autogenerated ID (see caveats below).
If you are not using autoincrement, and you are inserting the records from Access but you want to retrieve the last one from Excel:
make sure your primary key is sortable, so you can get the last one using a query like either of these:
SELECT MAX(MyPrimaryField) FROM MyTable;
SELECT TOP 1 MyPrimaryField FROM MyTable ORDER BY MyPrimaryField DESC;
or, if sorting your primary field wouldn't give you the last one, you would need to add a DateTime field (say InsertedDate) and save the current date and time every time you create a new record in that table so you could get the last one like this:
SELECT TOP 1 MyPrimaryField FROM MyTable ORDER BY InsertedDate DESC;
In either of these cases, I think you would find adding an AutoIncrement primary key as being a lot easier to deal with:
It's not going to cost you much
It's going to guarantee you uniqueness of your records without having to think about it
It's going to make it easier for you to pick the most recent record, either using ##Identity or through sorting by the primary key or getting the Max().
From Excel
To get the data into Excel, you have a couple of choices:
create a data link using a query, so you can use the result directly in a Cell or a range.
query from VBA:
Sub GetLastPrimaryKey(PrimaryField as string, Table as string) as variant
Dim con As String
Dim rs As ADODB.Recordset
Dim sql As String
con = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source= ; C:\myDatabase.accdb"
sql = "SELECT MAX([" & PrimaryField & "]) FROM [" & MyTable & "];"
Set rs = New ADODB.Recordset
rs.Open sql, con, adOpenStatic, adLockReadOnly
GetLastPrimaryKey = rs.Fields(0).Value
rs.Close
Set rs = Nothing
End Sub
Note about ##Identity
You have to be careful of the caveats when using ##Identity in standard Access databases(*):
It only works with AutoIncrement Identity fields.
It's only available if you use ADO and run SELECT ##IDENTITY;
It returns the latest used counter, but that's for all tables. You can't use it to return the counter for a specific table in MS Access (as far as I know, if you specify a table using FROM mytable, it just gets ignored).
In short, the value returned may not be at all the one you expect.
You must query it straight after an INSERT to minimize the risk of getting a wrong answer.
That means that if you are inserting your data at one time and need to get the last ID at another time (or another place), it won't work.
Last but not least, the variable is set only when records are inserted through programming code.
This means that is the record was added through the user interface, ##IDENTITY will not be set.
(*): just to be clear, ##IDENTITY behaves differently, and in a more predictive way, if you use ANSI-92 SQL mode for your database.
The issue though is that ANSI 92 has a slightly different syntax than
the ANSI 89 flavour supported by Access and is meant to increase compatibility with SQL Server when Access is used as a front end.
If the artificial key is an autonumber, you can use ##identity.
Note that with both these examples, the transaction is isolated from other events, so the identity returned is the one just inserted. You can test this by pausing the code at Debug.Print db.RecordsAffected or Debug.Print lngRecs and inserting a record manually into Table1, continue the code and note that the identity returned is not that of the record inserted manually, but of the previous record inserted by code.
DAO Example
'Reference: Microsoft DAO 3.6 Object Library '
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
db.Execute ("INSERT INTO table1 (field1, Crdate ) " _
& "VALUES ( 46, #" & Format(Date, "yyyy/mm/dd") & "#)")
Debug.Print db.RecordsAffected
Set rs = db.OpenRecordset("SELECT ##identity AS NewID FROM table1")
Debug.Print rs.Fields("NewID")
ADO Example
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Set cn = CurrentProject.Connection
cn.Execute ("INSERT INTO table1 (field1, Crdate ) " _
& "VALUES ( 46, #" & Format(Date, "yyyy/mm/dd") & "#)"), lngRecs
Debug.Print lngRecs
rs.Open "SELECT ##identity AS NewID FROM table1", cn
Debug.Print rs.Fields("NewID")
Re: "I have tried to get ##IDENTITY working, but this always returns 0 using the code below."
Your code sends SQL and SQL2 through different connection objects. I don't think ##identity will return anything other than zero unless you ask from the same connection where you executed your INSERT statement.
Try changing this:
myRecordset.Open SQL2, dbConnectionString, adOpenStatic, adLockReadOnly
to:
myRecordset.Open SQL2, databaseConnection, adOpenStatic, adLockReadOnly
Here's my solution that does not use ##index or MAX.
Const connectionString = "Provider=SQLOLEDB; Data Source=SomeSource; Initial Catalog=SomeDB; User Id=YouIDHere; Password=YourPassword"
Const RecordsSQL = "SELECT * FROM ThatOneTable"
Private Sub InsertRecordAndGetID()
Set connection = New ADODB.connection
connection.connectionString = connectionString
connection.Open
Set recordset = New ADODB.recordset
recordset.Open SQL, connection, adOpenKeyset, adLockOptimistic
With recordset
.AddNew
!Field1 = Value1
!Field2 = Value2
End With
recordset.MoveLast
ID = recordset.Fields("id")
End Sub
Enjoy!
Try following macro code.First add a command button to the sheet from the control box and paste following codes in the code window
Private Sub CommandButton1_Click()
MsgBox GetLastPrimaryKey
End Sub
Private Function GetLastPrimaryKey() As String
Dim con As String
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sql As String
con = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\myaccess.mdb;Persist Security Info=False"
sql = "SELECT MAX(id) FROM tblMyTable"
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
cn.Open con
rs.Open sql, cn, 3, 3, 1
If rs.RecordCount <> 0 Then
GetLastPrimaryKey = rs.Fields(0).Value
End If
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing
End Function
8 years late to the party... The problem you are having is that you are using dbConnectionString to create a new connection. ##identity is specific to the connection you are using.
First, don't close the original connection
'.Close
replace
myRecordset.Open SQL2, dbConnectionString, adOpenStatic, adLockReadOnly
with the connection you previously used for the insert
myRecordset.Open SQL2, databaseConnection, adOpenStatic, adLockReadOnly
and you'd have been all set. In fact, you don't even need to specify the table:
SQL2 = "SELECT ##identity AS NewID"