Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this table

user | column1 | column2
-----------------------------
peter | a |
peter | b |
peter | c |
josue | a |
josue | b |

I want to update the column2 with the records of column1 as parameter in a function, which return a value for column2, but i need filter by user, in this case only the records with the user peter

SQL
update table1
set column2 = dbo.fn_function(t.column1)
from table1 t
where t.user='peter'


The update execute shows the next error: "subquery return more than one value"

What I'm doing wrong? if the fn_function return one value?

Not works without "from" shows the same error

SQL
update table1
set column2 = dbo.fn_function(column1)
where user='peter'
Posted
Updated 19-Sep-13 9:34am
v5

You don't need the "from" line

SQL
update table1
set column2 = dbo.fn_function(t.column1)

get rid of this line
from table1 t

where t.user='peter'


since you are updating the same table you are checking the requirement "=peter" on.
 
Share this answer
 
Comments
Ejrr1085 19-Sep-13 15:38pm    
Not works without "from" shows the same error

update table1
set column2 = dbo.fn_function(column1)
where user='peter'
Ejrr1085 19-Sep-13 16:28pm    
Sorry I found the error, the function has a select with a "beetween", and "the beetween" makes return 2 values. In my case is better use > and < or >= or <= thanks
Are you sure that your function call doesn't return more than one response for "column1"? It looks like the function is only using the parameter "column1", and getting multiple results back, so the query doesn't know which function result to use.
 
Share this answer
 
Comments
Ejrr1085 19-Sep-13 15:43pm    
the function return an only value no query or table. The column1 is a hour value and the function returns a varchar with the value if is in a range, therange value go to column2, column2 too is a varchar.
Ejrr1085 19-Sep-13 16:27pm    
Sorry I found the error, the function has a select with a "beetween", and "the beetween" makes return 2 values. In my case is better use > and < or >= or <= thanks
Hmmm...

I've recreated this on my server as so

SQL
update dbo.SalesmanList
set lengthofRepCode = dbo.tester(RepCode)
where Name = 'Alan Lese'


the dbo.tester function doesn't reference any tables, just takes the varchar data parameter given and returns the length of the string, and then updates the table column oldaocpersno to whatever that is. I set up two records for the Alan Lese name, but with different length data in the RepCode column, and it worked perfect for me.

I'd really need to see your function i think to help you further, sorry.
 
Share this answer
 
Comments
Ejrr1085 19-Sep-13 16:26pm    
Sorry I found the error, the function has a select with a "beetween", and "the beetween" makes return 2 values. In my case is better use > and < or >= or <= thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900