Simple Update Query..... (1 Viewer)

ajb_1976

Registered User.
Local time
Today, 02:30
Joined
Feb 25, 2005
Messages
34
Hi,

I am new to SQL server but have had a bit of exposure to MS Access.

I am trying to run a simple update query on single table (dbo.tblClient). I ran the code as a SELECT script and it produces the correct results, but when I change this to an UPDATE i get a message informing me that the 'subquery returned more than 1 value....'.

Therefore my question is - How do I update multiple records within the table using an UPDATE query?

The code I am using is this -

UPDATE dbo.tblClient
SET ClientCategoryID = 23
WHERE EnquiryNotes like '%Category=UK%'
AND ClientCategoryID = 0
AND EntryDate >= DATEADD(day,-7, GETDATE())

and the successful SELECT query is this -

SELECT ClientID, ClientCategoryID, EntryDate
FROM dbo.tblClient
WHERE EnquiryNotes like '%Category=UK Removals%'
AND ClientCategoryID = 0
AND EntryDate >= DATEADD(day,-7, GETDATE())

Many Thanks
Alan
 

Ranman256

Well-known member
Local time
Yesterday, 21:30
Joined
Apr 9, 2015
Messages
4,337
1st: DATEADD("D",-7, GETDATE())
(unless day is a constant = "D")

2. save the select query to say Q1
then Q2 is the update query, "update Q1 set ClientCatID=23 "
 

Minty

AWF VIP
Local time
Today, 02:30
Joined
Jul 26, 2013
Messages
10,371
Bit late to the party with this one.
I can't see anything wrong with that as T-SQL Update query.

The only thing I'm slightly suspicious of in the Criteria for Enquiry Notes
EnquiryNotes like '%Category=UK%'

I'm wondering if it's trying to evaluate that somehow.
Try hard coding a specific value in there instead as an experiment.
 

WayneRyan

AWF VIP
Local time
Today, 02:30
Joined
Nov 19, 2002
Messages
7,122
I'm also late to the party.

The error message seems invalid because ... there is no subquery!
The SQL shown can't produce that message.
The SQL as shown should result in a message of some number of rows affected.
Any message involving "subquery" can only result from a trigger on the table.

The functional SELECT statement shows that all of the table/column names are correct.
Where was the query run? (SSMS, Access)
Was it possibly run in SSMS with other queries UNINTENTIONALLY run? (highlight only your query)

We need more info.

Wayne
 

Isaac

Lifelong Learner
Local time
Yesterday, 18:30
Joined
Mar 14, 2017
Messages
8,777
Was it possibly run in SSMS with other queries UNINTENTIONALLY run? (highlight only your query)
That's kind of what I was thinking too! The query looks fine to me?

But the question of where it was run is good too. Access is horrible at returning actual database server errors as a calling app. I'd be curious to see a screenshot of the ssms window...
 

WayneRyan

AWF VIP
Local time
Today, 02:30
Joined
Nov 19, 2002
Messages
7,122
Isaac,

We're just gonna have to wait on the OP.

That message could very well come from a trigger.
A trigger that was written for single-row updates !!!
It might do a nested select from INSERTED that would do return this type of error.

But, that would be VERY evident in the error message shown by SSMS.
It would explicitly say trigger.

Guess we'll just wait and see.

Wayne
 

isladogs

MVP / VIP
Local time
Today, 02:30
Joined
Jan 14, 2017
Messages
18,216
1. Shouldn't the WHERE clause be the same as the SELECT query ...UK Removals%
2. Possibly unnecessary but try setting the query to show unique records. UPDATE DISTINCTROW ...
 

WayneRyan

AWF VIP
Local time
Today, 02:30
Joined
Nov 19, 2002
Messages
7,122
Good catch isladogs.
My brain wanted to see them as the same.

But that would only affect the total number of rows affected.
Error messages would be PK or unique indexes ... not sub query returns too many rows.

Still a mystery.

Wayne
 

Users who are viewing this thread

Top Bottom