Solved

TSQL Query

Posted on 2015-01-16
8
137 Views
Last Modified: 2015-01-20
I have a long query with the in Excel VBA

Rs.Open stSQL, conn, adOpenKeyset, adLockReadOnly

The stSQL contains two parts
1. select [some fields] from TableA into #TMP1
2. select [some fields from #TMP1 INNER JOIN TableB on ....

The first part retrieved a temporary solution into a temp table and the second part select fields based on joining the temp table to another table.

If I paste the whole SQL and run on SQL  Management Studio. It works fine. However, no recordset can be retrieved through the VBA. Can VBA SQL allows directing 1st part of the result to a temporary table in order to subsequent query to use. Is there any workaround way to make it work ?

Tks
0
Comment
Question by:AXISHK
  • 3
  • 3
  • 2
8 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40553453
How about using a Stored Procedure?
0
 
LVL 12

Expert Comment

by:Nakul Vachhrajani
ID: 40553482
Are you changing connections in between? The single hash (#) indicates a local temporary table that is not accessible across connections.

It's similar to the following:
1. Open SSMS and connect 2 query editor windows to a SQL server instance
2. Paste your 1st step (SELECT .... INTO #temp1) in the 1st window
3. Paste the 2nd step (SELECT ... FROM #temp1) in the 2nd window
4. Run the query in window #1 followed by a query in window #2 - it will be unable to find the object - #temp1
5. Now, switch back to the 1st window and run (SELECT .... FROM #temp1) - it will succeed and return results

The workaround that immeidately comes to mind is to have a permanent transaction table that that the SPID of the process as the key. So, query #1 inserts the result sets with SPID as the key. Query #2 simply queries all records matching that SPID.

Or, use a stored procedure instead (much better from a performance perspective). You can also evaluate using in-line table valued functions.
0
 

Author Comment

by:AXISHK
ID: 40553753
Tks.  Any example of calling the procedure on the SQL server from VBA, or create a stored procedure in VBA and use it ?


Great Tks.
0
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 300 total points
ID: 40553765
Here's how to do it in Excel (i.e. not in VBA): http://blogs.office.com/2010/06/07/running-a-sql-stored-procedure-from-excel-no-vba/

Here's an example of how to do in Excel VBA: http://www.experts-exchange.com/Database/MS-SQL-Server/Q_28388090.html

There are other examples, but I'm not allowed to post them. Google

call a sql server stored procedure from vba

for more examples.
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

Author Comment

by:AXISHK
ID: 40555132
With sql stored procedure, what is max. size of a single input parameter ? For @InvoiceNo nvarchar(3000), does it mean it only allow 3000 chars to pass ?

Tks
0
 
LVL 12

Assisted Solution

by:Nakul Vachhrajani
Nakul Vachhrajani earned 200 total points
ID: 40556440
You are correct. NVARCHAR(3000) means that it will accept 3000 Unicode characters. Only exceptions are the datatypes like NVARCHAR(MAX) or VARCHAR(MAX) that support upto 2 GB of data.
0
 

Author Comment

by:AXISHK
ID: 40557068
Tks. Will there be any issue if I define my input parameter as NVARCHAR(MAX) to ensure it can handle the maximum size input ?
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40557250
Not too many issues. Can't be indexed, but that isn't relevant to you.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now