 |
|
 |
|
Next: SP Command Doesn't Execute
|
| Author |
Message |
External

Since: Jan 05, 2007 Posts: 8
|
(Msg. 1) Posted: Thu Jan 25, 2007 10:32 am
Post subject: Problem with nested Select Statement Archived from groups: microsoft>public>sqlserver>programming (more info?)
|
|
|
I am trying to get a list of rows from 1 view and 1 table (that is not
included in the View). The View is called "vAAWO" and the table is
called "PullSaveData". The current stored procedure I have is:
Select [WO Number], [Body], [Part Number], [Complete Date], [WO
Quantity], --Select some fields from the vAAWO view
(
Select [SaveString] from [PullSaveData] --Select the
SaveString field from the PullSaveData table
where ([vAAWO].[WO NUMBER] = [PullSaveData].[WONumber])
--"Join" the two tables by the WO Number field
AND (CharIndex ([vAAWO].[Part Number],
[PullSaveData].[SaveString]) <> 0) --Search for the Part number in the
SaveString Field
) as "PulledString"
>From [vAAWO]
Where [Work Center] Like '34f' -- Show only the items for the passed
Work Center
I appoligize for the "messiness" of this code. This procedure actually
works somewhat. My problem is the nested Select statement, which does
two things.
The first line ([vAAWO].[WO NUMBER] = [PullSaveData].[WONumber]) gets
the records that have the same Work Order number in the two objects (my
view & separate table).
The second line (CharIndex ([vAAWO].[Part Number],
[PullSaveData].[SaveString]) <> 0) looks to see if the "Part Number"
exists in the "SaveString" field.
The problem is that this is returning a recordset as if I typed "OR"
instead of "AND" in my nested Select statement. I get all the records
that match EITHER of the criteria of my first and second lines shown
above.
Can anyone tell me why this is occurring? Obviously, if there is a
better way to do this, I am all ears. Just know that I have tried a
couple of other methods, like joining the separate table in my view
(which got several other extra records).
I would GREATLY appreciate any suggestions that anyone might have.
Thank you! >> Stay informed about: Problem with nested Select Statement |
|
| Back to top |
|
 |  |
External

Since: Jan 22, 2007 Posts: 56
|
(Msg. 2) Posted: Thu Jan 25, 2007 6:34 pm
Post subject: Re: Problem with nested Select Statement [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
It's occurring because you aren't actually doing a JOIN on the table,
and excluding the rows you don't want. What about something like this:
SELECT
wo.[WO Number],
wo.[Body],
wo.[Part Number],
wo.[Complete Date],
wo.[WO Quantity],
psd.SaveString AS [PulledString]
FROM [vAAWO] wo
INNER JOIN [PullSaveData] psd
ON wo.[WO NUMBER] = psd.[WONumber]
AND CHARINDEX (wo.[Part Number], psd.[SaveString]) > 0
WHERE wo.[Work Center] = '34f'
-Dave
Kirk wrote:
> I am trying to get a list of rows from 1 view and 1 table (that is not
> included in the View). The View is called "vAAWO" and the table is
> called "PullSaveData". The current stored procedure I have is:
>
> Select [WO Number], [Body], [Part Number], [Complete Date], [WO
> Quantity], --Select some fields from the vAAWO view
> (
> Select [SaveString] from [PullSaveData] --Select the
> SaveString field from the PullSaveData table
> where ([vAAWO].[WO NUMBER] = [PullSaveData].[WONumber])
> --"Join" the two tables by the WO Number field
> AND (CharIndex ([vAAWO].[Part Number],
> [PullSaveData].[SaveString]) <> 0) --Search for the Part number in the
> SaveString Field
> ) as "PulledString"
>>From [vAAWO]
> Where [Work Center] Like '34f' -- Show only the items for the passed
> Work Center
>
> I appoligize for the "messiness" of this code. This procedure actually
> works somewhat. My problem is the nested Select statement, which does
> two things.
>
> The first line ([vAAWO].[WO NUMBER] = [PullSaveData].[WONumber]) gets
> the records that have the same Work Order number in the two objects (my
> view & separate table).
> The second line (CharIndex ([vAAWO].[Part Number],
> [PullSaveData].[SaveString]) <> 0) looks to see if the "Part Number"
> exists in the "SaveString" field.
>
> The problem is that this is returning a recordset as if I typed "OR"
> instead of "AND" in my nested Select statement. I get all the records
> that match EITHER of the criteria of my first and second lines shown
> above.
>
> Can anyone tell me why this is occurring? Obviously, if there is a
> better way to do this, I am all ears. Just know that I have tried a
> couple of other methods, like joining the separate table in my view
> (which got several other extra records).
>
> I would GREATLY appreciate any suggestions that anyone might have.
> Thank you!
>
--
-Dave Markle
http://www.markleconsulting.com/blog >> Stay informed about: Problem with nested Select Statement |
|
| Back to top |
|
 |  |
External

Since: Jan 05, 2007 Posts: 8
|
(Msg. 3) Posted: Fri Jan 26, 2007 4:58 am
Post subject: Re: Problem with nested Select Statement [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Holy cow - that was awesome! I have been struggling with this for some
time and that was an incredibly simple & concise solution to my
problem. Consider this an outstanding recomendation for your
consulting firm!
THANK YOU very much Dave.
On Jan 25, 6:34 pm, Dave Markle
wrote:
> It's occurring because you aren't actually doing a JOIN on the table,
> and excluding the rows you don't want. What about something like this:
>
> SELECT
> wo.[WO Number],
> wo.[Body],
> wo.[Part Number],
> wo.[Complete Date],
> wo.[WO Quantity],
> psd.SaveString AS [PulledString]
> FROM [vAAWO] wo
> INNER JOIN [PullSaveData] psd
> ON wo.[WO NUMBER] = psd.[WONumber]
> AND CHARINDEX (wo.[Part Number], psd.[SaveString]) > 0
> WHERE wo.[Work Center] = '34f'
>
> -Dave
>
>
>
>
>
> Kirk wrote:
> > I am trying to get a list of rows from 1 view and 1 table (that is not
> > included in the View). The View is called "vAAWO" and the table is
> > called "PullSaveData". The current stored procedure I have is:
>
> > Select [WO Number], [Body], [Part Number], [Complete Date], [WO
> > Quantity], --Select some fields from the vAAWO view
> > (
> > Select [SaveString] from [PullSaveData] --Select the
> > SaveString field from the PullSaveData table
> > where ([vAAWO].[WO NUMBER] = [PullSaveData].[WONumber])
> > --"Join" the two tables by the WO Number field
> > AND (CharIndex ([vAAWO].[Part Number],
> > [PullSaveData].[SaveString]) <> 0) --Search for the Part number in the
> > SaveString Field
> > ) as "PulledString"
> >>From [vAAWO]
> > Where [Work Center] Like '34f' -- Show only the items for the passed
> > Work Center
>
> > I appoligize for the "messiness" of this code. This procedure actually
> > works somewhat. My problem is the nested Select statement, which does
> > two things.
>
> > The first line ([vAAWO].[WO NUMBER] = [PullSaveData].[WONumber]) gets
> > the records that have the same Work Order number in the two objects (my
> > view & separate table).
> > The second line (CharIndex ([vAAWO].[Part Number],
> > [PullSaveData].[SaveString]) <> 0) looks to see if the "Part Number"
> > exists in the "SaveString" field.
>
> > The problem is that this is returning a recordset as if I typed "OR"
> > instead of "AND" in my nested Select statement. I get all the records
> > that match EITHER of the criteria of my first and second lines shown
> > above.
>
> > Can anyone tell me why this is occurring? Obviously, if there is a
> > better way to do this, I am all ears. Just know that I have tried a
> > couple of other methods, like joining the separate table in my view
> > (which got several other extra records).
>
> > I would GREATLY appreciate any suggestions that anyone might have.
> > Thank you!--
> -Dave Markle
>
> http://www.markleconsulting.com/blog- Hide quoted text -- Show quoted text - >> Stay informed about: Problem with nested Select Statement |
|
| Back to top |
|
 |  |
| Related Topics: | Building a dynamic Select Statement Problem - Ok now i know doing this in a stored procedure is slow etc but my boss wants it done this way so i have to somehow find how to make it work (even tho its probably slower :() Anyway... I have a table which defines what columns i require and the format....
adding another row in the select? join? nested select - working in asp.net 2.0. I have a dropdownlist with a sqldatasource selectcommand. Currently just doing a select distinct * from table order by code (the second field). I'm trying to stay only in the markup, and I'd like to add a temporary (for the sake...
An INSERT EXEC statement cannot be nested. - Using sql 2005 I have a stored procedure (SPB) that calls a stored procedure (SPA) and uses it's resultset for further processing. Both stored procedures make use of temp tables. Create Table #tmp(bla bla bla) When I run SPB I get an error msg: "...
Performance benefits between a JOIN statement vs a nested .. - Is there a performance bonus between these 2 sql statements? Using JOIN: SELECT * FROM A, B WHERE A.ID = B.ID; Using nested SELECT: SELECT * FROM A WHERE A.ID = (SELECT ID FROM B); Thanks
Nested Select?? - Hi all, I have the following table id (autonumber) category1 (int) category2 (int) booking_month (int) booking_year (int) I have records in the table for booking_year = 2004 and booking_year = 2005, for example id, category1, category2, booking_month,... |
|
You can post new topics in this forum You can reply to topics in this forum You can edit your posts in this forum You can delete your posts in this forum You can vote in polls in this forum
|
|
|
|
 |
|
|