Solved

TSQL Query

Posted on 2015-01-16
8
144 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

732 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