Domanda

I am using the Select and Right Join SQL functions to call all the data into one table. Data being called will only populate if ans_primary = '1' on the Assignment table. If asn_primary = '1' then it will join all the below columns on att_id = att_id for both tables.

The two tables and the columns used are below:

Assignment (att_id, itm_id, asn_primary)

Attachment (att_id, att_name, att_type, att_path, att_path2, att_path3)

select assignment.itm_id, attachment.att_name, attachment.att_type, attachment.att_path,    attachment.att_path2, attachment.att_path3
    from assignment
    right join attachment
    on assignment.att_id=attachment.att_id
    where assignment.asn_primary = '1'

I need to be able to update all the fields in the att_name column after the call has been made. I am not sure how to update columns after a Join call is used.

The SQL I need to run after the info has been called/joined is:

Update attachment
set att_name = att_name + '.tif'
where att_path3 like '%.tif%'
È stato utile?

Soluzione

The syntax for join in an update in SQL Server is:

Update att
    set att_name = att_name + '.tif'
    from assignment a join
         attachment att
         on a.att_id = att.att_id
    where a.asn_primary = '1' and
          att.att_path3 like '%.tif%';

The right outer join doesn't seem appropriate, because you are filtering on a field from assignment.

Altri suggerimenti

What you're looking for is referred to as a side-effecting query. You'll want to follow the basic structure as below. Be careful of the results. If a row shows up three times, it will be updated three times. This can significantly affect server performance and the resulting logs.

I usually imbed a 'SELECT *' to allow me to view the results prior to executing any updates.

UPDATE T1
SET T1.Column1 = 'New Data'
--SELECT *
FROM dbo.Table1 AS T1
INNER JOIN dbo.Table2 AS T2 ON T2.Id = T1.Id
WHERE T1.Column1 <> 'New Data';
UPDATE assignment
right join attachment
on assignment.att_id=attachment.att_id
SET att_name = att_name + '.tif'
where assignment.asn_primary = '1'
AND att_path3 like '%.tif%'
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top