Solved

TSQL Query

Posted on 2015-01-16
8
146 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 24

Expert Comment

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

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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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
 

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 13

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

717 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