473,383 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

nested queries, stored procedures, temporary table

Hi,
I'm adapting access queries to sql server and I have difficulties with
the following pattern :
query1 : SELECT * FROM Query2 WHERE A=@param1
query 2: SELECT * FROM Table2 WHERE B=@param2

The queries are nested, and they both use parameters.
In MS Acccess the management of nested queries with parameters is so
easy (implicit declaration of parameters, transmission of parameters
from main query to nested query)
that I don't know what the syntax should be for stored procedures.

The corresponding stored procedure would be something like

CREATE TABLE #TempTable (...table definition...)
INSERT INTO #TempTable ExecProc spQuery2 @Param2
SELECT * FROM #TempTable WHERE A=@Param1

And spQuery2 would be : SELECT * FROM Table2 WHERE B=@Param

I was wondering if this syntax would work and if I can skip the
explicit declaration of #TempTable definition.

Thanks for your suggestions.

Jul 23 '05 #1
5 11099
I don't understand why you would want to use a temp table at all. In
SQL Server you can nest queries in the FROM clause as derived tables
but in your case it looks like you could just combine the two WHERE
clauses.

--
David Portas
SQL Server MVP
--

Jul 23 '05 #2
I don't understand why you would want to use a temp table at all. In
SQL Server you can nest queries in the FROM clause as derived tables
but in your case it looks like you could just combine the two WHERE
clauses.

--
David Portas
SQL Server MVP
--

Jul 23 '05 #3
Stu
You can use nested subqueries in SQL Server; you can even have multiple
result set returned from a single stored procedure (if you wish).
Based on your example, I'm assuming you want the results from query1 to
be your final result; in that case, you can do something like:

SELECT colList
FROM (SELECT colList FROM Table2 WHERE B=@Param) alias
WHERE A=@Param1

If you have to have a stored procedure for query2, you are correct that
you will need to insert the results from query2 into a temporary table
or table variable and then query those results.

HTH,
Stu

Jul 23 '05 #4
I do agree with both of you that there are other ways to perform the
queries.
But for readability and commonalization purposes I do need to have a
stored procedure for query 2.
I need some pointers for the syntax :
How do I declare the temp table or table variable ?
How do I load the table with the result of the stored procedure ?

Thanks a lot,

Jul 23 '05 #5
Actually I found what I was looking for at
http://www.sommarskog.se/share_data.html#UDF

Syntax would be
CREATE FUNCTION spQuery2(@Param varchar(30))
RETURNS TABLE
AS
RETURN (SELECT * FROM Table2 WHERE B=@Param)
then spQuery1 would be
SELECT * FROM spQuery2(@Param2) WHERE A=@Param1

I'm sure that they're ways to make it more efficient, etc. but that one
is a good start.

Thanks

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: WGW | last post by:
Though I am a novice to MS SQL server (2000 I believe), I can do almost! everything I need. Maybe not efficiently, but usefully. However, I have a problem -- a complex query problem... I can...
7
by: pkruti | last post by:
Below is a stored procedure i am working with and i am trying to drop the yesno_holding table if it exists but how do i add it back? Meaning i want it to drop if it exists but i want to add it back...
2
by: Keith Watson | last post by:
Hi, we are currently implementing an application running on DB2 V7 on Z/OS using largely COBOL stored procedures, managed using WLM. Some of these stored procedures declared global temporary...
7
by: Anthony Robinson | last post by:
Have been encountering an odd issue. Every now and again, certain packages of stored procedures just become invalid. I'm aware that dropping or altering an underlying table would render a package...
8
by: Thomasb | last post by:
With a background in MS SQL Server programming I'm used to temporary tables. Have just started to work with DB2 ver 7 on z/OS and stumbled into the concept of GLOBAL TEMPORARY TABLE. I have...
1
by: Roman Prigozhin | last post by:
Hi all, I have one stored procedure where I defined 3 temporary tables, which i return back to java. I want to have 3 separate sub procedures which would go after the data and fill out these...
2
by: Twan Kennis | last post by:
Question: How do I pass a returning resultset from a nested Stored Procedure (which opens a cursor including option "WITH RETURN TO CALLER") as a returning resultset from it's own? When I...
2
by: Andy B | last post by:
Is there an easy way to convert tableAdaptor queries into stored procs without messing up the dataTables in the dataSet or losing the queries themselves?
3
by: codefragment | last post by:
Hi I have a chunky bit of sql that I will want to call from a number of places. It will return a few thousand rows. Whats the best way of structuring this? 1) I initially thought of using...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.