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
Related
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
Update details
set details.a=a, details.b=b
where Id=10 select a,b from #userdefinedtabletype
Here id is unique for all the records
Since you have not provided much detail as to what either table has in it I am going to assume that your user defined table only has one row in it. Otherwise you will get the last values that are returned from the query.
So I would use a CROSS APPLY to do the update such as:
Update details
set details.a=t2.a, details.b=t2.b
from details t1
CROSS APPLY (select a,b from #userdefinedtabletype) t2
where t1.Id=10
If in fact these tables are related by an ID of some sort then you would probably want to do an INNER JOIN:
Update details
set details.a=t2.a, details.b=t2.b
from details t1
INNER JOIN #userdefinedtabletype t2 on t1.id = t2.id
where t1.Id=10
I have two tables which as same column name called id (stores record id). I'm using an inner join on these 2 tables and taking the id column of 2 tables. I'm using this select query in the angularjs http.get rest API. When the rest API throws the resultant table which contain 2 column of same name-id, I'm storing this in scope variable.
The problem is how can i use the id of one particular table.
consider query is,
select t1.id,t2.id
from table1 AS t1
INNER JOIN table2 AS t2 ON t2.product=t1.product_id
I am storing the result of rest API in scope variable,
$scope.details=response.data.platform.record;
I used these below syntax inside alert box in controller but not working,
$scope.details.id, $scope.details.t1.id.
Using column alias name, will solve your problem:
SELECT t1.id AS T1Id,
t2.id AS T2Id
FROM table1 AS t1
INNER JOIN table2 AS t2 ON t2.product = t1.product_id
and in your controller, you can use as:
$scope.details.T1Id
$scope.details.T2Id
I am new to Microsoft SQL Server. I am trying to join two tables that has common key named CampaignID using LEFT OUTER JOIN. I need to reuse the result in a different query, so I decided to capture the result set using CTE_Results. For example,
-- This is my CTE script
WITH CTE_Results AS
(
SELECT t1.CampaignID, t2.CampaignID, t1.Name, t2.Vendor
FROM CampaignDetails AS t1
LEFT OUTER JOIN CampaignOnlineDetails AS t2
ON t1.CampaignID = t2.CampaignID
)
-- This is the script I want to use to compare the resulting table. For example,
SELECT Vendor
FROM CTE_Results
However, when I ran above, I get:
The column `CampaignID` was specified multiple times for `CTE_Results`.
From reading through old StackOverflow questions and answers, it seems like since CampaignID is in both tables that are being joined, I must use table aliases to specify whose (which table's) CampaignID I want to SELECT. But I think I did that and even that it seems like the error still occurs.
Is there a way for me to select and keep BOTH CampaignID's in my CTE? If so, what should be changed? Thank you for the answers!
You have CampaignID selected twice in CTE, use different alias name to fix the problem
WITH CTE_Results
AS (SELECT t1.CampaignID AS cd_CampaignID,
t2.CampaignID AS cod_CampaignID,
t1.NAME,
t2.Vendor
FROM CampaignDetails AS t1
LEFT OUTER JOIN CampaignOnlineDetails AS t2
ON t1.CampaignID = t2.CampaignID)
-- This is the script I want to use to compare the resulting table. For example,
SELECT Vendor
FROM CTE_Results
or use this
WITH CTE_Results(cd_CampaignID, cod_CampaignID, NAME, Vendor)
AS (SELECT t1.CampaignID,
t2.CampaignID,
t1.NAME,
t2.Vendor
FROM CampaignDetails AS t1
LEFT OUTER JOIN CampaignOnlineDetails AS t2
ON t1.CampaignID = t2.CampaignID)
-- This is the script I want to use to compare the resulting table. For example,
SELECT Vendor
FROM CTE_Results
You need to Alias the CampaignID Columns in your CTE or define the returned column names in the CTE declaration. Otherwise it would be like creating a table with two columns with the same name.
Example Column Alias:
WITH CTE_Results AS
(
SELECT t1.CampaignID as 'CampaignID1', t2.CampaignID as 'CampaignID2', t1.Name, t2.Vendor
FROM CampaignDetails AS t1
LEFT OUTER JOIN CampaignOnlineDetails AS t2
ON t1.CampaignID = t2.CampaignID
)
Or In CTE declaration:
WITH CTE_Results (CampaignID1, CampaignID2, [Name], Vendor) AS
(
SELECT t1.CampaignID, t2.CampaignID , t1.Name, t2.Vendor
FROM CampaignDetails AS t1
LEFT OUTER JOIN CampaignOnlineDetails AS t2
ON t1.CampaignID = t2.CampaignID
)
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.