Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sub-query to scalar

Status
Not open for further replies.

Luzian

Programmer
Nov 27, 2005
103
US
how do I:
Code:
INSERT INTO Modifications
(
	type
)
VALUES
(
	(
		SELECT [red]FirstRow(id)[/red]
		FROM ModificationTypes_ENUM
		WHERE name = 'create'
	)
)

I want to select the "id" column from the first row I get.
 
INSERT INTO Modifications
(
type
)
SELECT MIN(id)
FROM ModificationTypes_ENUM
WHERE name = 'create'
 
Do you want:

a) the ID from the first row returned in the sub query
or
b) the minimum ("first") id from the ModificationTypes_ENUM
table ?

min() will give you b) however to get a) I think you will have to resort to whatever functions come with your DB because off hand I can't think of a set-based function in ANSI SQL that will do this (unless you can uniquely identify the required row in some way).

Example for SQL Server 2005 AdventureWorks database to illustrate the point:

select top 1 CreditCardID from Sales.ContactCreditCard
- gives you the CreditCardID value from the first row in the result set

select min(CreditCardID) from Sales.ContactCreditCard
- gives you the first CreditCardID in the table

Any further thoughts anyone ?

ujb

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top