Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 28th, 2003, 12:20 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
Default

 
Quote:
quote:
Quote:
 You can create a Stored Procedure that takes a parameter like this

CREATE PROCEDURE Procedure1 @Parameter int
AS

SELECT * from Table1
WHERE NumberField1 = @Parameter

RETURN

This will return a row (or set of rows) from the stored procedure filtered by the @parameter value that you give it.
Yes, I have done this a lot (lately) but how do I get that result into another SP?
Example:
SP 1:
Lets say I have a Customer table and I pass the parameter to filter for just City critera = @Enter_City (I'll enter Chicago when I run it)

SP 2: I want to see all orders by customers in Chicago who have ordered part number 1234. So I make a SP that uses the Orders table and uses SP1 to which will give me Chicago (or whatever I enter)customers. Then I filter the Orders table for part = @Enter_Part (and I type 1234)

This is overly simplictic, but it is the clearest way I can think of right now to explain what it is I commonly do in Access and want to now do in SQL.
 
Old October 28th, 2003, 12:20 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:Originally posted by Mitch
 This is "foggy" in my mind: What I need to understand is how to do something in SQL that I used to do in MS Access, namely use a query that takes a parameter(s) that is used within another query that itself takes a different parameter(s).
You can't do the same thing in SQL Server. There is no such thing as a "...query that takes a parameter...". There are lots of things (almost all bad :D) you can do in Access that you can't do exactly the same way in SQL Server.

There is no such thing as a "Query" in SQL Server. We refer to queries (lowercase) as being the resultset of a SELECT statement, or the act that we do when we execute such a statement in a tool like Query Analyzer. Instead of Queries which are stored in the database as in Access, SQL Server has Views and Stored Procedures.

Stored procedures may take parameters and may return resultsets, but they may just as well have no parameters and return nothing at all.

Views are virtual tables - they act just like a table which contains rows made up of columns. You select the rows and columns from the view in the same way you would select from a table. You JOIN a View to other Views and/or tables.

The rough equivalent of what you are looking for is that you would pass parameters to a Stored procedure which would execute a SELECT statement returning a resultset. This resultset would be constructed by JOINing your tables and views together as appropriate and then using your parameters as expressions in the WHERE clause to select the appropriate rows from the JOINs of your various tables and views. You might also use your parameters to direct the flow of execution in the Stored Procedure, depending on your requirements.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old October 28th, 2003, 12:34 PM
sal sal is offline
Friend of Wrox
 
Join Date: Oct 2003
Posts: 702
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Jeff is right. Access makes us lazy.
Create the two stored procedures with IN parameters.
Create two combobox controls. Set one to show all cities, set the other to show all part numbers
Have the user select values from both comboboxes and have them vlick a button and run your stored procedure that returns the resultwet that you are looking for.

I think you are trying to do this thinking too much like Access. Do the joins as Jeff suggested and create two IN parameters and use them in the WHERE clause.





Sal
 
Old October 28th, 2003, 12:46 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:Originally posted by sal
 I think you are trying to do this thinking too much like Access. Do the joins as Jeff suggested and create two IN parameters and use them in the WHERE clause.
That is, create one stored procedure that takes two parameters and use them both in the SELECT statement.

So, in your earlier post where you wanted to select orders from Chicago for part number 1234, you'd construct a single stored procedure with those two values as parameters, and which contained a SELECT statement which JOINed your Order_Detail to your Order_Header and then JOINed to your Customer table. The WHERE clause in this SELECT statement selects from the Customer table where the Customer.city column equals the City parameter and the Order_Detail.Partnumber column equals the Part Number parameter...


Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old October 28th, 2003, 12:49 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:Originally posted by sal
 Jeff is right. Access makes us lazy.
Access makes you wrong. (at least from a relational point of view).

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old October 28th, 2003, 12:58 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

If you post a little more info on what you are trying to do, and the tables you are working with, perhaps we can help you get to where you want to go, without using any canine assistance. :)

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old October 28th, 2003, 12:59 PM
sal sal is offline
Friend of Wrox
 
Join Date: Oct 2003
Posts: 702
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Access makes you AutoNumber happy! Way wrong!




Sal





Similar Threads
Thread Thread Starter Forum Replies Last Post
Oracle SP itvenky Oracle 2 November 23rd, 2006 03:13 AM
Please check my SP mat41 SQL Server 2000 1 April 19th, 2006 05:36 AM
exec sp within another sp collie SQL Server 2000 1 December 22nd, 2004 05:46 AM
sp problem lucian Classic ASP Basics 0 July 9th, 2004 01:52 AM
Can a SP run another SP as sa? dbradley SQL Server 2000 0 July 17th, 2003 08:35 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.