473,661 Members | 2,429 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11110
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(@Param 2) 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
16940
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 create a parameter query in a stored procedure, but how do I use the result set of a parameter query in a select query (in the same or another sp)? In short, if a select query contains a result table that is generated as a parameter query, how do I...
7
3983
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 if it doesnt exist. the reason why i have to drop it is because when i run the stored procedure it creates mulitple rows with the values in them but in my report it takes the top row and insert those values into the report and i dont want that i...
2
6968
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 tables, which are declared with ON COMMIT DELETE ROWS to perform work on and then return these temporary tables to the client (which is a message driven EJB connecting via DB2 connect). The client reads the data in the result sets, creates some XML...
7
5671
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 invalid, but we are doing no such thing... After banging my head on the wall for a bit I noticed that the two stored procedures that are experiencing this behavior are procedures that are called from within another procedure (they're not both...
8
7933
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 created a temporary database with a tables space. Verified that DECLARE GLOBAL TEMPORARY TABLE TEMP (A INTEGER); INSERT INTO SESSION.TEMP VALUES(10); SELECT A FROM SESSION.TEMP; works from a query tool.
1
5289
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 temporary tables. Question: Is it possible to a) Pass a pointer to the temporary table from one procedure and have it filled out in the other procedure, then return controll to the caller ?
2
10611
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 execute the top-level Stored Procedure, it executes succesfully, but without any resultset. ========================================================== CREATE PROCEDURE sp_executesql(sqltxt CLOB(2M))
2
1781
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
4373
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 nested stored procedures and returning the result in a temporary table. However the scope of the temporary table seems to be limited to the stored procedure its created in so unless I create the temporary table in every stored
0
8341
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8542
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8630
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7362
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4343
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2760
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1740
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.