I need to make an update in a table based on other information.
How to proceed?
My case is:
I have a column iniciantes table MEMB_INFO
I need to make an update MEMB_INFO SET iniciantes = 0
But I need to make a query on WHERE table column resets the character table
Example:
UPDATE MEMB_INFO
SET iniciantes = 0
FROM MEMB_INFO CROSS JOIN
Character
WHERE (Character.Resets >= 100)
necessary that the update the memb_info only occur in cases where the reference character is greater than or equal to 100
Simply, join MEMB_INFO with your Character table and specify their relations:
update m
set iniciantes = 0
from MEMB_INFO m
inner join Character c on
c.Resets >= 100
AND m.CharacterId = c.CharacterId --Specify your tables' relations.
you have to specify columns on which you're joining this two tables. If you have characterId column in both tables, your query will be:
update MEMB_INFO set
iniciantes = 0
from MEMB_INFO as m
where
m.CharacterId in (
select c.CharacterId from Character as c where c.Reset >= 100
)
Related
I have two tables. My first table A contains
Tran_Particular | Dr_Tran_Amt | BeneficiaryName | PRNNo
columns.
My second table B contains
BeneficiaryName | Dr_Tran_Amt | PRNNo
columns.
I want to update table A.PRNNo but When I am updating this on BeneficiaryName and Dr_Tran_Amt that time it updates only first value. But In table B there are two PRNNO's are exist and In table A also two different Tran_Particular are exist. I want to update unique PRNNO wrt Tran_Particular, BeneficiaryName and Dr_Tran_Amt.
Query.
update A
set a.PRNNo = b.PRNNo
from A a
inner join B b
on a.Dr_Tran_Amt= b.Amount
and a.BeneficiaryName = b.BeneficiaryName;
HoW to update this in SQL server.
The code below should get you only the first matching row (the lowest PRNNo value):
UPDATE A
SET a.PRNNo = b.PRNNo
FROM A a
INNER join B b
ON a.Dr_Tran_Amt = b.Amount
AND a.BeneficiaryName = b.BeneficiaryName
AND b.PRNNo = (SELECT MIN(c.PRNNo) FROM B c WHERE c.BeneficiaryName = B.BeneficiaryName AND c.Dr_Tran_Amt = b.Dr_Tran_Amt);
I have two tables jhead and jjops. I am updating jhead with the following statement:
UPDATE jhead h
SET h.jhpcmp = 1
WHERE h.jhpcmp = '0' AND h.jhpqty <= h.jhqtys
But now I want to update jjops based upon what I updated in jhead. The statement would be like:
UPDATE jjops j
SET j.jascmp = 1, japcmp = 1
WHERE (This is where I am stuck)
What links the two tables is the following: h.jhjob = j.jajob
Is there some way to update both of the tables together? Should I first update table jjops using a join and then update table jhead and if so what Join should I use?
The way to update two tables "at the same time" is to use a transaction. You can use the output clause as one way to pass stuff from one statement to the next. SQL Server also has a special syntax for joining in an update statement (see the second update)
Declare #ids table (jhjob int not null) -- replace with correct data type
Begin Transaction
Update
jhead
Set
jhpcmp = 1
output
inserted.jhjob into #ids
Where
jhpcmp = '0' And
jhpqty <= jhqtys
Update
jjops
Set
jascmp = 1,
japcmp = 1
From
jjops j
inner join
#ids h
on j.jajob = h.jhjob
Commit Transaction
SELECT `pro`.`St`, `s`.`Quantity`
FROM `s`
LEFT JOIN `web`.`pro` ON `s`.`Pro_id` = `pro`.`ProdID`
the above query results in a table like
st quantity
132 1
11 1
st is from one table and quantity is from another
using this select statement I want to add the resulting quantity to the st row in the other table.
Basically updating the second table by adding the quantity i get from this select statement.
UPDATE `web`.`pro`
SET `pro`.`st` = `pro`.`st` + `s`.`Quantity`
FROM `web`.`pro`
JOIN `shopping cart` `s`
ON `s`.`Pro_id` = `pro`.`ProdID`;
Something like that ?
I'm searching for check a columns value in all the rows and update with a where clause as a condition. My case is as follows:
SubscriptionID ChannelURI StudentID
1 XXXX 4
2 yyyy 4
3 XXXX 3
4 XXXX 4
5 XXXX 2
I want to check the column channel uri value for a specfic student and for all matched results to set it to null.
So in this case row 3 and 5 should be set to null.
I've tried this, but it set all channeluri of other rows than studnetid = 4 to null
UPDATE SubscriptionCourse
Set ChannelURI = 1
, DeviceId = null
FROM SubscriptionCourse as t1
INNER JOIN SubscriptionCourse as t2
on t1.ChannelURI = t2.ChannelURI
WHERE StudentId! = 4
Reference the table to be updated by it's alias given in the FROM clause, rather than by name (since the same table name is referenced twice. Also qualify the reference on StudentId in the WHERE clause with the table alias as well.
UPDATE t1
SET t1.ChannelURI = 1
, t1.DeviceId = NULL
FROM SubscriptionCourse t1
JOIN SubscriptionCourse t2
ON t1.ChannelURI = t2.ChannelURI
WHERE t1.StudentId != 4
You say you want to set ChannelURI to NULL, but your statement is setting to a literal value of 1. I've left the assignment as you specified in your statement, but qualified the columns with the table alias.
I don't think this is your problem, but I never include a space in the "not equals" comparison operator symbol (!=). I've just never seen that before. I prefer to use the <> symbol for the "not equals" comparison operator.
From your description of the problem and your example, it's not at all clear why you need to join the table to itself.
I recommend you FIRST write a SELECT statement that returns the rows you want to update, by replacing the UPDATE and SET clauses with a SELECT <expression_list> clause, with the expression_list including the value of the primary key column(s), the column you want to update, and any other columns you want to check. Once that SELECT is returning the rows you want to update, then convert it into an UPDATE statement.
update [table_name] set channelURI = "null" where SubscriptionID = 3 or SubscriptionID =5
in this case,SubscriptionID must be a primary key.
Update table set channeluri = null where studentid <> 4
That what you want? Or you want to find all the ones that have the same uri as student 4 and null those?
Update table set channeluri = null from table inner join table t2 on table.channeluri =t2.channeluri where table.studentid <> 4 and t2.studentid =4
Something like that, im on my phone, the wife has stolen the pc
I need to update a field on a table to be true only if a matching row exists in another table, for all the rows where the column is currently null in the main table.
This is a description of what I want to achieve:
UPDATE [LenqReloaded].[dbo].[Enquiry] A
SET [ResponseLetterSent] = 1
WHERE [ResponseLetterSent] IS NULL
AND EXISTS
(
SELECT * FROM [LenqReloaded].[dbo].[Attachment] B
WHERE A.[EnquiryID] = B.[EnquiryID]
)
This isn't syntactically correct.
I can't code it via an IF EXISTS... statement because I don't have the [EnquiryID] without reading the data from the table.
How should I format my UPDATE statement?
You weren't far off...
UPDATE A
SET A.[ResponseLetterSent] = 1
FROM [LenqReloaded].[dbo].[Enquiry] A
WHERE A.[ResponseLetterSent] IS NULL
AND EXISTS ( SELECT * FROM [LenqReloaded].[dbo].[Attachment] B WHERE A.[EnquiryID] = B.[EnquiryID] )
You need to use a join in your update:
UPDATE [LenqReloaded].[dbo].[Enquiry] SET [ResponseLetterSent] = 1
FROM [LenqReloaded].[dbo].[Enquiry] A
join [LenqReloaded].[dbo].[Attachment] B on A.[EnquiryID] = B.[EnquiryID]
WHERE A.[ResponseLetterSent] IS NULL
This seems counterintuitive, but you need to establish a table alias in a From clause but use that alias in the Update Clause...
Update E Set
ResponseLetterSent = 1
From LenqReloaded.dbo.Enquiry E
Where ResponseLetterSent Is Null
And Exists (Select * From LenqReloaded.dbo.Attachment
Where EnquiryID = E.EnquiryID)
The thing you are missing is the 'from' clause, which is a t-sql extension - it is the only way to assign an alias to the updated table
update [lenqreloaded].[dbo].[enquiry]
set [responselettersent] = 1
from [lenqreloaded].[dbo].[enquiry] a
where [responselettersent] is null
and exists (
select *
from [lenqreloaded].[dbo].[attachment] b
where a.[enquiryid] = b.[enquiryid]
)