When i want to populate my grid I use this Select command :
SELECT t1.Id, t1.emertimi, kredite, pershkrim, Viti, t2.Emertimi as Expr1, emri, mbiemri
FROM Kurse as t1 INNER JOIN
Deget as t2 ON t1.degID = t2.Id INNER JOIN
Pedgoge ON t1.Id = Pedgoge.kurs_id
Now when I try to Update the grid with an Update command that is fired after an EditCommand finished ( you pres Update as a link) I use it this way :
UPDATE Kurse
SET t1.emertimi=#emertimi,
kredite=#kredite,
pershkrim=#pershkrim,
Viti=#Viti,
t2.Emertimi as Expr1 = #Expr1, emri=#emri, mbiemri=#mbiemri
FROM Kurse as t1 INNER JOIN
Deget as t2 ON t1.degID = t2.Id INNER JOIN
Pedgoge ON t1.Id = Pedgoge.kurs_id
But this doesn't work. Please as I am a student and non english native is a little hard to understand what should I do in this case. Any help is much appreciated
UPDATE statement changes only one table or view
It fails because there are two different aliases (t1 and t2 ) in the SET clause
As Vadim already said, UPDATE command can only change one table. So you'll have to write 3 different Update statements.
UPDATE Kurse
SET emertimi=#emertimi,
kredite=#kredite,
pershkrim=#pershkrim,
Viti=#Viti
WHERE.......
UPDATE Deget
SET Emertimi = #Expr1,
emri=#emri,
mbiemri=#mbiemri
WHERE ......
From your query it is not clear what exactly you would like to update and where, so you'll have to sort out where conditions yourself.
Related
I have the following update statement I use in trigger that have same behavior and both of it is correct but 2nd one have problem if I update both tableA_ID and ValueB at same times. But this not big issue as there is work around but if possible I want to avoid it. So what I want to know is there a big performance between this 2 and it will make me decide which one to go. If 2nd much better in performance than I will use 2nd one. But if the difference not that big I will use 1st one. I not expert in sql server. Anyone can help?
UPDATE tableA
SET tableA.Value = tableA.Value - old.ValueB
FROM tableA
INNER JOIN (
SELECT tableA_ID ,ValueB
FROM deleted
) old ON old.tableA_ID = tableA.ID
UPDATE tableA
SET tableA.Value = tableA.Value + new.ValueB
FROM tableA
INNER JOIN (
SELECT tableA_ID ,ValueB
FROM inserted
) new ON new.tableA_ID = tableA.ID
UPDATE tableA
SET tableA.Value = tableA.Value - old.ValueB + new.ValueB
FROM tableA
INNER JOIN (
SELECT tableA_ID ,ValueB
FROM deleted
) old ON old.tableA_ID = tableA.ID
INNER JOIN (
SELECT tableA_ID ,ValueB
FROM inserted
) new ON new.tableA_ID = tableA.ID
As with anything, test it to be sure.
Whether or not you should be doing this in a trigger is a long discussion.
Performance-wise, you're going to see better performance, in most circumstances, with the second query. The reason I can simply state that is that you're only looking at a single lookup for the UPDATE in the query where you update everything all at once. Whereas, the first query has to find the data to UPDATE, two times.
However, again, test this.
And yes, as #SMor says in the comments, why you need this as a trigger should absolutely be discussed. Triggers can be seriously misused and abused.
I am trying to update column from one table to another table of different database.
I searched on stack but found answers for PostgreSQL and SQLite. Those code ain't worked for me properly in SQL Server.
Say,
D1,D2 are the two different databases
T1 is the table
C1,C2,C3 are the columns in T1
I want to update like
UPDATE T1
SET D2.T1.C1 = D1.T1.C1
WHERE D2.T1.C2 = D1.T1.C2
Everything except the where clause works fine.
Here is some code I tried:
use DB2
GO
UPDATE TABLE1
SET COL1 = (SELECT COL1 FROM DB1.dbo.TABLE1)
WHERE COL2 = DB1.dbo.TABLE1.COL2
How shall I write this query?
Note: D1 and D2 are identical. Both have the exact same schema. Just the name and data are different. Both databases are on the same server.
SQL Server supports an update join syntax:
UPDATE t1
SET COL1 = t2.COL1
FROM TABLE1 D1.t1
INNER JOIN TABLE2 D2.t2
ON t1.COL2 = t2.COL2;
Actually, your current approach might work, but you should try changing it to this:
UPDATE TABLE1 D1.t1
SET COL1 = (SELECT t2.COL1 FROM TABLE2 D2.t2 WHERE T1.COL2 = T2.COL2);
You can simply use an INNER JOIN cause sql-server support Cross-Database Queries, so you can do as:
UPDATE T1
SET T1.Col1 = T2.Col1
FROM DataBase1.Schema.TableName T1 INNER JOIN DataBase2.Schema.TableName T2
ON T1.ID = T2.ID;
Please, visit and read Cross-Database Queries.
Or update tableName set column Name = (select columnName from second table where 1=1) i.e condition is either true or false optional though
I have a table variable named #MatchTemp.I have written an insert query using select statement as shown below
Insert into #MatchTemp(StoreName,CompanyCode,StoreCode,Address,CityState)
Select s.StoreName,co.CompanyCode,s.StoreCode,s.BuildingName as Address,s.City+','+st.StateName as CityState
from Cashsafes c
inner join Stores s on s.StoreId=c.StoreId
inner join Company co on co.CompanyId=c.CompanyId
inner join State st on st.StateId=s.StateId
and c.CashsafeId=#cashsafeid.
How can i write the update query instead of Insert as shown above? Im stuck here
Update #MatchTemp set StoreName=s.StoreName,CompanyCode=co.CompanyCode,..
from Cashsafes c
Any help will be appreciated.
the better is to use the temporary table(using # for scope or ## for global) rather than temporary variable as temporary variable gonna create in primary memory (RAM) whereas temporary table gonna create in secondary memory (DISK). However the syntax for your update query, you can append the where condion in last if required.
UPDATE #Table1 SET t1.name = t2.name
FROM Table2 t2
INNER JOIN #Table1 t1 ON t1.id = t2.id
I'm trying to update a column while joining it to another table. I've used below query, but it gave me error.
UPDATE TABLE_2
INNER JOIN
TABLE_1
ON (T2.ID=T1.ID)
SET TABLE_2.DEPT='HUMAN RESOURCE'
WHERE TABLE_2.DEPT='HR'
AND T1.COMPANY =1
Can anyone help me on this?
Thanks.
For one thing, you're using table aliases that aren't defined anywhere (T2, T1 etc) and that may very well solve your problem. If not, the correct syntax very much depends on SQL flavor.
For example, in SQL Server the syntax is
UPDATE T2
SET T2.dept = 'HUMAN RESOURCE'
FROM Table2 T2
INNER JOIN Table1 T1
ON T1.[ID] = T2.[ID]
Although you don't even need a join here really, you just want
UPDATE Table2 T2
SET T2.dept = 'HUMAN RESOURCE'
WHERE EXISTS(SELECT * FROM Table1 T1
ON T1.[ID] = T2.[ID])
In MySQL the syntax is
UPDATE FROM TABLE2 AS T2
INNER JOIN TABLE1 as T1
ON T2.id = T1.id
SET T2.Dept = 'Human Resources'
Of Course, the WHERE EXISTS approach also works for MySQL
UPDATE FROM Table2 AS T2
SET Dept="Human Resources"
WHERE EXISTS (SELECT * FROM Table1 T1
ON T1.[ID] = T2.[ID]);
If it is MSSQL, then the query should be
UPDATE TABLE_2 SET DEPT='Human Resource'
FROM TABLE_1
WHERE TABLE_2.ID = TABLE_1.ID
AND TABLE_2.DEPT = 'HR'
AND TABLE_1.COMPANY = 1
UPDATE TABLE_2 SET DEPT='Human Resource'
FROM TABLE_1,Table2
WHERE TABLE_2.ID = TABLE_1.ID
AND TABLE_2.DEPT = 'HR'
AND TABLE_1.COMPANY = 1
Because when we update a table in joining then we use both tables in from close
I have two tabels with a relation and I want to update a field in table A. Is it possible to combine update and join in the same query? I googled it but didnt find any working solution?
UPDATE md SET md.status = '3'
FROM pd_mounting_details AS md
LEFT OUTER JOIN pd_order_ecolid AS oe ON md.order_data = oe.id
I'm using MS SQL
UPDATE
t
SET
t.Column1=100
FROM
myTableA t
LEFT JOIN
myTableB t2
ON
t2.ID=t.ID
Replace myTableA with your table name and replace Column1 with your column name.
After this simply LEFT JOIN to tableB. t in this case is just an alias for myTableA. t2 is an alias for your joined table, in my example that is myTableB. If you don't like using t or t2 use any alias name you prefer - it doesn't matter - I just happen to like using those.
If what you need is UPDATE from SELECT statement you can do something like this:
UPDATE suppliers
SET city = (SELECT customers.city FROM customers
WHERE customers.customer_name = suppliers.supplier_name)
Just another example where the value of a column from table 1 is inserted into a column in table 2:
UPDATE Address
SET Phone1 = sp.Phone
FROM Address ad LEFT JOIN Speaker sp
ON sp.AddressID = ad.ID
WHERE sp.Phone <> ''
In mysql the SET clause needs to come after the JOIN. Example:
UPDATE e
LEFT JOIN a ON a.id = e.aid
SET e.id = 2
WHERE
e.type = 'user' AND
a.country = 'US';
The Left join in this query is pointless:
UPDATE md SET md.status = '3'
FROM pd_mounting_details AS md
LEFT OUTER JOIN pd_order_ecolid AS oe ON md.order_data = oe.id
It would update all rows of pd_mounting_details, whether or not a matching row exists in pd_order_ecolid. If you wanted to only update matching rows, it should be an inner join.
If you want to apply some condition based on the join occurring or not, you need to add a WHERE clause and/or a CASE expression in your SET clause.