473,407 Members | 2,312 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,407 software developers and data experts.

problem with subselect

Hello,

I have a problem with a subselect I use in a stored procedure:

UPDATE #TEMP_TABLE
SET P_ID_1=(SELECT top 1 b.P_ID_1 from #TEMP_TABLE b where
b.ID=PARENT_ID),
P_ID_2=PARENT_ID,
P_ID_3=ID
WHERE PARENT_ID IN (SELECT P_ID_2
FROM #TEMP_TABLE b)

So the subselect is (SELECT top 1 b.P_ID_1 from #TEMP_TABLE b where
b.ID=PARENT_ID), and it returns NULL. The cause of that is most
probably the fact that I try to link ID from inner table b with
PARENT_ID from the outer table. I thought it had to be done this way,
but obviously not. Can somebody help me with this syntax problem?

Thx,

Bart

Aug 31 '07 #1
5 3750
Please post table structure. Also let us know what you want to update.

-
Shiju Samuel

On Aug 31, 6:58 pm, Bart op de grote markt <warn...@googlemail.com>
wrote:
Hello,

I have a problem with a subselect I use in a stored procedure:

UPDATE #TEMP_TABLE
SET P_ID_1=(SELECT top 1 b.P_ID_1 from #TEMP_TABLE b where
b.ID=PARENT_ID),
P_ID_2=PARENT_ID,
P_ID_3=ID
WHERE PARENT_ID IN (SELECT P_ID_2
FROM #TEMP_TABLE b)

So the subselect is (SELECT top 1 b.P_ID_1 from #TEMP_TABLE b where
b.ID=PARENT_ID), and it returns NULL. The cause of that is most
probably the fact that I try to link ID from inner table b with
PARENT_ID from the outer table. I thought it had to be done this way,
but obviously not. Can somebody help me with this syntax problem?

Thx,

Bart

Aug 31 '07 #2
On 31 aug, 18:20, Shiju Samuel <shiju.sam...@gmail.comwrote:
Please post table structure. Also let us know what you want to update.

-
Shiju Samuel
Since I am not at work now, I can't copy the whole stored procedure
that I had till now; but concerting the table structure, it is like
this
#TEMP_TABLE with columns ID, PARENT_ID, SEQUENCE, DESCRIPTION, P_ID_1,
P,_ID_2, P_ID_3,...,P_ID_10
The records in the #TEMP_TABLE are first imported from a databasetable
(only columns ID, PARENT_ID, SEQUENCE, DESCRIPTION) and they represent
a tree-like structure with the root-nodes having a PARENT_ID-value of
NULL. Now I want the stored procedure to return every node with all
of its parents until the root node.(which will be in P_ID_1) E.g. 1 >
1A 1A1 will be the result for record [1A1 | 1A | 3 | description of
1A1]

So the first step is importing the data into #TEMP_TABLE.
The 2nd step is to put the ID's of all records that have NULL as
PARENT_ID into their P_ID_1 column.
The 3rd step is to put ID's of all records that have a PARENT_ID that
is in (SELECT P_ID_2 FROM #TEMP_TABLE b) , which is a result op step
2, into P_ID_2. We put their parent_ID then into P_ID_1.
The 4th step is the SQL from my first post. It is easy to fill in
P_ID_2 and P_ID_3, but for P_ID_1 I need a subselect, and it doesn't
seem to work like I explained in my first post.
Grtz,

Bart

Aug 31 '07 #3
Bart op de grote markt (wa*****@googlemail.com) writes:
But well I learned from your post that you put a FROM-clause in your
update. I did not know that you could do that. Unfortunately I can
only test it on monday again... Thx for your input!
I should point out that this is syntax that is proprietary to SQL Server
and Sybase (and I think Informix has it too). In fact, you can also do:
UPDATE tbl
SET col = ...
FROM tbl
JOIN othertbl ...

just like in a regular SELECT. The one thing to watch out for is that if
your join conditions are such that they include rows from the target
table multiple times, it is unpredictable which value that will win.

While frowned at by purists, it's a very convenient extension, as it
makes your UPDATE (and DELETE) statements easier to write. Also, it is
my experience that an UPDATE with FROM JOIN performs better than using
subqueries in the SET clause. This is particularly evident if you need
to retrieve several columns from the same table. With the ANSI syntax
you need to repeat the subquery each time. (Since SQL Server does not
support set constrctors).

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Sep 1 '07 #4
Erland Sommarskog <es****@sommarskog.sewrote in
news:Xn**********************@127.0.0.1:
UPDATE tbl
SET col = ...
FROM tbl
JOIN othertbl ...
IMHO it is also worth noting that the tbl immediately following the UPDATE
may be an alias so that one may write

UPDATE a
SET col = ...
FROM tbl a
....

(but the form UPDATE tbl a SET col = ... isn't permitted)

and this can avoid some confusion (to the human reader/writer - of course
the computer executes what was written, even if it isn't what the SQL
writer intended)
Sep 1 '07 #5
Thx everybody for your help! It works the way I wanted it to and I
learned some new things :).

Regards,

Bart

Sep 3 '07 #6

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

Similar topics

0
by: krystoffff | last post by:
Hi I have the following database under MySQL 4.1 : //////////////////////////////////////////////////// # Table structure for table `affiliate_lockout` # CREATE TABLE `affiliate_lockout` (...
6
by: Greg Stark | last post by:
So I have a query in which some of the select values are subqueries. The subqueries are aggregates so I don't want to turn this into a join, it would become too complex and postgres would have...
3
by: Neil Zanella | last post by:
Hello, I would like to ask the about the following... PostgreSQL allows tables resulting from subselects to be renamed with an optional AS keyword whereas Oracle 9 will report an error...
7
by: Patrick Fisher | last post by:
Hi I have a table which Contains entries with RefCode field containing INVP or INVPD Common fields in each entry would be InvoiceNo, Total and PurTyp for example. You could have ...
4
by: dtwalter | last post by:
Is it possible to ORDER BY a SubSelect? I don't see why it wouldn't be, but I'm having some trouble. Hopefully it's just a simple error in syntax and somebody can tell me. Here's what I'm trying...
1
by: Marco Lazzeri | last post by:
I'd like to reference values returned by a subselect in the same SELECT query. Example: SELECT id, ( SELECT COUNT(*) FROM second ) AS value_to_reference, ( value_to_reference + 1 ) AS...
6
by: Joolz | last post by:
Hi everyone, When importing a bunch of data (> 85000 rows) I get an error I can't explain. The table into which I'm importing has a unique clause on (code, bedrijf). The rows in the source-table...
6
by: Sebastien | last post by:
I have the following statement which I run successfully in... 1 hour 10 minutes. SELECT a.tsgicd as ACCT_ID, a.tsa5cd as SEC_ID, CASE WHEN (SUBSTRING(a.tsgicd, 6, 1) = 'R' or...
10
by: Rudolf Bargholz | last post by:
Perhaps some kind soul could help me out with an SQL I have been trying all day to get to work, where one colum is just not summing up the way I want it to. I have the following data GRP_SEQ ...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...

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.