I have two tables Role and Role_Imp. I need to fetch Name column's value from the 1st row of the Role table. After that I need to update Name's column value in Role_Imp table for the rows which has Names same as the selected name from the Role table.
I am using the following query, which is not working as its wrong.
UPDATE Role_Imp
SET Role_Imp.Name = 'Role Test Change'
FROM Role_Imp
INNER JOIN
Role ON Role_Imp.Name = SELECT TOP 1 Name FROM Role
How should I do this?
Seems like this should do it:
UPDATE Role_Imp
SET Name = 'Role Test Change'
WHERE Name = (SELECT TOP 1 Name FROM Role)
Related
I have 2 tables: tblAccounts and tblOrders. How do I copy the data in column VENUE in tblOrders to the new column I've just set up in tblAccounts also called VENUE but where the account ID matches.
I was going to try something like this:
INSERT INTO tblaccounts (venue)
SELECT venue
FROM tblorders
WHERE tblorders.accountID = tblaccounts.accountID
If that makes sense?
The tables are different sizes so I can't just copy it all across.
Since you want to copy the value of column: venue from tblOrders to tblAccounts, you should update tblAccounts instead of inserting as following.
UPDATE tblAccounts
SET
[venue] =y.[venue]
FROM tblAccounts INNER JOIN tblOrders y
ON
tblAccounts.[accountID] = y.[accountID]
I am using SQL Management Studio 2012.
I would like to update a field from a table that is not within the Target/Source relationship in a Store Procedure using a TRIGGER to update records from a View ([dbo].ActivityView tv) to an editable table ([dbo].ActivityTracker t). Records fall in and out of the View based on a [COMPLETION] date field. If the [COMPLETION] date value is past the end of the Quarter, it drops out of the View but triggers a "Won't Be Complete" in the Target's [COMPLETION] column. However, when this happens, I can't see when that update timestamp to the [COMPLETION] field took place since it is no longer within the View. The value exists in "THISOTHERTABLE" the View is based off.
Therefore, I would like to update the field in the Target from "THISOTHERTABLE" if possible.
MERGE [dbo].[ActivityTracker] t --TARGET
USING [dbo].[ActivityView] tv --SOURCE
on tv.ID = t.ID
WHEN MATCHED THEN
UPDATE
SET t.ID = tv.ID,
Do Stuff...
WHEN NOT MATCHED BY TARGET THEN
Do Stuff...
WHEN NOT MATCHED BY SOURCE THEN
Set t.TIMESTAMP = (select distinct o.TIMESTAMP from [dbp].THISOTHERTABLE o where t.ID = o.ID) END
"THISOTHERTABLE" has multiple records for the ID field, so I would have to select distinct TIMESTAMP to get only a single value for the unique ID in the TARGET table. However, THISOTHERTABLE already has only the MAX(TIMESTAMP) for TIMESTAMP so no need to do the MAX. The TARGET table has only one unique record for each ID. Is it possible to write an UPDATE subquery like I have shown in the last line of example code?
how to check if value from external table is different than current table then update.
I am updating student firstname from firstname of extstudent. if value is different then only update else dont update
Update student
SET FirstName = FirstName
FROM ExtStudent
You need to join the two tables in your UPDATE (Transact-SQL) statement so that you can compare the two. Both tables have to have the same key so that you can match up the records in your table to your "external" table. In my example below, I'm going to assume that the primary key of both tables is a field called StudentId.
Update Student
SET Student.FirstName = ExtStudent.FirstName
FROM Student
JOIN ExtStudent
ON Student.StudentID = ExtStudent.StudentID
Where Student.FirstName <> ExtStudent.FirstName
This will update the Student table and set the FirstName field to the matching record with the same StudentID in the ExtStudent table where the FirstName fields do not match. Because this is an inner join, it will not update if no matching record is not found.
If you are updating multiple fields and want to update the record if any of the fields changed, then check for inequality for each field in the where clause separated by an OR.
You could technically leave off the where clause because if it doesn't change it would just be updating it to the same value. However keep in mind that this does register as an update, so any update triggers would be fired for all the records... even the ones that didn't change.
Another issue you might run into is if your name fields are nullable. The where clause will return false if either one of the fields are null, so it would not update. You could account for this with the isnull function.
Where isnull(Student.FirstName,'') <> isnull(ExtStudent.FirstName,'')
If changed to a left join with this modification, it would update FirstName to NULL if no match was found.
I have three table as below:
I have a dropdownlist and I want to display "Libelle" (Table 'Grade') which is connected with Table 'EvolutionGrade' ((main table)) According to "PPR" in 'Agent' Table.
I'm working with SQL Server: this is my query, I don't know if it's correct, I have to take Libelle in table Grade in display it in TextBox where Grade.codegrade = EvolutionGrade.codegrade (something like that) according to PPR in table Agent.
if exists ( select CodeGrade from Grade where CodeGrade = (select CodeGrade from Agent where PPR =#ppr))
begin
update Grade set Libelle=#lblgrade where CodeGrade = (select CodeGrade from Agent where PPR =#ppr)
end
else
insert into Grade (Libelle) values ( #lblgrade)
why do you have 2 the same table column? primary key must be different to other columns. Why not Eval_num?
I need to set a Lable on a contact record based on finding a duplicate email in the contact table. The duplicate label field is in the contacts_cstm field.
This SP updates all of the records, not just the one submitted.
#EMAIL1 NVARCHAR (100)
AS
BEGIN
SET NOCOUNT ON;
update CONTACTS_CSTM set DUPLICATE_CONTACT_C = 'DUPLICATE'
where (select count(EMAIL1) from CONTACTS as C
where C.EMAIL1 = #EMAIL1 ) >1
I want to this to update when the count of the contact's email is >1.
Your where clause does not constrain the table you are updating. You need to equate some column of C with some column of CONTACTS_CSTM,