Subject: Returning the 1st row of a SELECT Sub Query
Posted By: rit01 Post Date: 2/21/2006 9:41:37 AM
Hi All

I wonder if someone knows the answer... I have a stored proc that looks similar to the following:

Update TABLE1
Set
FIELD2 = (SELECT FIELDB FROM TABLE2 WHERE (FIELDA= @PARAM1))
WHERE (FIELD1 = @PARAM1)


The SELECT sub query runs fine when selecting a value from TABLE2 that only appears once but TABLE2 also consists of multiple instance.. it then throws an error.

I think the answer would be to be able to select the FIRST row of the SELECT query result... is this possible in SQL 2K?

Many thanks

Rit
Reply By: SqlMenace Reply Date: 2/21/2006 9:46:46 AM
How about

Update TABLE1
Set
FIELD2 = (SELECT TOP 1 FIELDB FROM TABLE2 WHERE (FIELDA= @PARAM1))
WHERE (FIELD1 = @PARAM1)



How do you know that that is the value that you want? do you have duplicates or do you have different values and you don't care what you are updating it with?



“I sense many useless updates in you... Useless updates lead to fragmentation...  Fragmentation leads to downtime...Downtime leads to suffering..Fragmentation is the path to the darkside.. DBCC INDEXDEFRAG and DBCC DBREINDEX are the force...May the force be with you" -- http://sqlservercode.blogspot.com/
Reply By: rit01 Reply Date: 2/21/2006 11:20:12 AM
that works great... I have checked whether the first record is sufficient... I know what you mean though.

Many thanks

Rit

Go to topic 40328

Return to index page 363
Return to index page 362
Return to index page 361
Return to index page 360
Return to index page 359
Return to index page 358
Return to index page 357
Return to index page 356
Return to index page 355
Return to index page 354