Solved

TSQL Query

Posted on 2015-01-16
8
141 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 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSMS Opening Mode 9 18
SQL R 21 24
sql 2016 data tools breakdown.. 1 11
too many installs coming along with SQL 2016? 1 14
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

809 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