David Megnin
asked on
Update a table with a calculated value. "Subquery returned more than 1 value..."
I'm trying to update a table with a calculated value. I have two tables, Applicants and JobTitles.
I want to update the PositionsAvailable field in JobTitles (current values may be NULL or some value)
This Select query gives me the numbers I need:
SELECT JobTitles_1.txtWorksiteJob TitlePosit ionsNumber - COUNT(Applicants.keyApplic antID) AS Expr1
FROM Applicants INNER JOIN
JobTitles AS JobTitles_1 ON JobTitles_1.keyJobTitleID = Applicants.keyJobTitleID
GROUP BY JobTitles_1.txtWorksiteJob TitlePosit ionsNumber , JobTitles_1.keyJobTitleID
I tried this query and although it pased the "Check SQL" check I got an error when I tried to execute it:
UPDATE JobTitles
SET PositionsAvailable =
(SELECT JobTitles_1.txtWorksiteJob TitlePosit ionsNumber - COUNT(Applicants.keyApplic antID) AS Expr1
FROM Applicants INNER JOIN
JobTitles AS JobTitles_1 ON JobTitles_1.keyJobTitleID = Applicants.keyJobTitleID
GROUP BY JobTitles_1.txtWorksiteJob TitlePosit ionsNumber , JobTitles_1.keyJobTitleID)
The error said "...Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <=, >, >= or when the subquery is used as an expression..."
Can someone help me fix it? I'm using VB.NET, VS2005, SQL Server 2000
thanks
I want to update the PositionsAvailable field in JobTitles (current values may be NULL or some value)
This Select query gives me the numbers I need:
SELECT JobTitles_1.txtWorksiteJob
FROM Applicants INNER JOIN
JobTitles AS JobTitles_1 ON JobTitles_1.keyJobTitleID = Applicants.keyJobTitleID
GROUP BY JobTitles_1.txtWorksiteJob
I tried this query and although it pased the "Check SQL" check I got an error when I tried to execute it:
UPDATE JobTitles
SET PositionsAvailable =
(SELECT JobTitles_1.txtWorksiteJob
FROM Applicants INNER JOIN
JobTitles AS JobTitles_1 ON JobTitles_1.keyJobTitleID = Applicants.keyJobTitleID
GROUP BY JobTitles_1.txtWorksiteJob
The error said "...Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <=, >, >= or when the subquery is used as an expression..."
Can someone help me fix it? I'm using VB.NET, VS2005, SQL Server 2000
thanks
ASKER
Ahhh! Thank you! I'll give it a try. I bumped the points up for the "after hours" quick response. I appreciate it.
ASKER
It gives me a "The column prefix JobTitles_1 does not match a table or alias used in the query" error.
That's odd because JobTitles_1.keyJobTitleID is used all over the place in the query.
I'm just learning SQL and working on my production database so I don't want to "experiment" too much.
Would this work if I used:
WHERE JobTitles.keyJobTitleID=Ap plicants.k eyJobTitle ID instead of
WHERE JobTitles.keyJobTitleID=Jo bTitles_1. keyJobTitl eID ?
The two tables I'm working with are Applicants and JobTitles.
Thanks again.
That's odd because JobTitles_1.keyJobTitleID is used all over the place in the query.
I'm just learning SQL and working on my production database so I don't want to "experiment" too much.
Would this work if I used:
WHERE JobTitles.keyJobTitleID=Ap
WHERE JobTitles.keyJobTitleID=Jo
The two tables I'm working with are Applicants and JobTitles.
Thanks again.
ASKER
Okay, I tried: WHERE JobTitles.keyJobTitleID=Ap plicants.k eyJobTitle ID and got the same error... "Applicants doesn't match a table used in the query..."
So I moved the parentheses to the end putting: "WHERE JobTitles.keyJobTitleID=Ap plicants.k eyJobTitle ID" inside the subquery and got an "Error near 'Where' ".
So I moved the parentheses to the end putting: "WHERE JobTitles.keyJobTitleID=Ap
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
In regards to the where clause error; you have to do Where Clauses prior to Group By.
SELECT ...
FROM ...
WHERE ...
GROUP BY ...
HAVING ...
ORDER BY ...
SELECT ...
FROM ...
WHERE ...
GROUP BY ...
HAVING ...
ORDER BY ...
ASKER
I was wondering if the order mattered. I had tried putting it in the subquery, but only by moving the parentheses, not moving the clause. Thanks for that tip!
I did back up my database last night before I tried it the first time. I'll back it up again before I try this one too. ;-)
I did back up my database last night before I tried it the first time. I'll back it up again before I try this one too. ;-)
>>The column prefix JobTitles_1 does not match a table
The WHERE clause must be within the parens, not outside, like this
UPDATE JobTitles
SET PositionsAvailable =
(SELECT JobTitles_1.txtWorksiteJob TitlePosit ionsNumber -COUNT(Applicants.keyAppli cantID)
FROM Applicants INNER JOIN
JobTitles AS JobTitles_1 ON JobTitles_1.keyJobTitleID = Applicants.keyJobTitleID
GROUP BY JobTitles_1.txtWorksiteJob TitlePosit ionsNumber , JobTitles_1.keyJobTitleID
WHERE JobTitles.keyJobTitleID=Jo bTitles_1. keyJobTitl eID)
The WHERE clause must be within the parens, not outside, like this
UPDATE JobTitles
SET PositionsAvailable =
(SELECT JobTitles_1.txtWorksiteJob
FROM Applicants INNER JOIN
JobTitles AS JobTitles_1 ON JobTitles_1.keyJobTitleID = Applicants.keyJobTitleID
GROUP BY JobTitles_1.txtWorksiteJob
WHERE JobTitles.keyJobTitleID=Jo
ASKER
That worked like a charm. Thank you very much!!
I saved it as a StoredProcedure. Could you give me a quick instruction on what's required to execute the StoredProcedure from a Button_Click event.
...
Dim con As New SqlConnection
Dim cmd As New SqlCommand
con.ConnectionString = " my connection string "
cmd = StoredProcedure1
con.Open()
cmd.ExecuteScalar
con.Close()
...
Is that close to being correct? " ...'Scalar, 'NonScalar"?
Are there "standard" error handling bits that I should include as a general rule?
Thanks again.
I saved it as a StoredProcedure. Could you give me a quick instruction on what's required to execute the StoredProcedure from a Button_Click event.
...
Dim con As New SqlConnection
Dim cmd As New SqlCommand
con.ConnectionString = " my connection string "
cmd = StoredProcedure1
con.Open()
cmd.ExecuteScalar
con.Close()
...
Is that close to being correct? " ...'Scalar, 'NonScalar"?
Are there "standard" error handling bits that I should include as a general rule?
Thanks again.
See http://support.microsoft.com/kb/308049/en-us. It has ExecuteScalar vs. ExecuteNonQuery as well as examples of calling each with parameters.
ASKER
Is this correctly written?
Dim con As New SqlConnection
Dim cmd As SqlCommand = New SqlCommand & _
("spUpdate_JobTitles_Posit ionsAvaila ble", con)
con.ConnectionString = "myConnectionString"
cmd.Connection = con
cmd.CommandType = CommandType.StoredProcedur e
con.Open()
cmd.ExecuteNonQuery
con.Close()
Dim con As New SqlConnection
Dim cmd As SqlCommand = New SqlCommand & _
("spUpdate_JobTitles_Posit
con.ConnectionString = "myConnectionString"
cmd.Connection = con
cmd.CommandType = CommandType.StoredProcedur
con.Open()
cmd.ExecuteNonQuery
con.Close()
ASKER
Oh, spUpdate_JobTitles_Positio nsAvailabl e is an Update query:
UPDATE JobTitles
SET PositionsAvailable =
(SELECT JobTitles_1.txtWorksiteJob TitlePosit ionsNumber - COUNT(Applicants.keyApplic antID) AS Expr1
FROM Applicants INNER JOIN
JobTitles AS JobTitles_1 ON JobTitles_1.keyJobTitleID = Applicants.keyJobTitleID
WHERE (JobTitles.keyJobTitleID = JobTitles_1.keyJobTitleID)
GROUP BY JobTitles_1.txtWorksiteJob TitlePosit ionsNumber , JobTitles_1.keyJobTitleID)
It's attached to a button_click event.
When I tested it, it did not update
UPDATE JobTitles
SET PositionsAvailable =
(SELECT JobTitles_1.txtWorksiteJob
FROM Applicants INNER JOIN
JobTitles AS JobTitles_1 ON JobTitles_1.keyJobTitleID = Applicants.keyJobTitleID
WHERE (JobTitles.keyJobTitleID = JobTitles_1.keyJobTitleID)
GROUP BY JobTitles_1.txtWorksiteJob
It's attached to a button_click event.
When I tested it, it did not update
UPDATE JobTitles
SET PositionsAvailable =
(SELECT JobTitles_1.txtWorksiteJob
FROM Applicants INNER JOIN
JobTitles AS JobTitles_1 ON JobTitles_1.keyJobTitleID = Applicants.keyJobTitleID
GROUP BY JobTitles_1.txtWorksiteJob
WHERE JobTitles.keyJobTitleID=Jo