Inserting one table's complete column data to a particular column in another table in Microsoft SQL - sql

I m having two tables COMPONENTS(sno,comp_name,quantity,costperunit,totalcost) and COST(sno,comp_name,costperunit).. in Microsfot SQL Server
I m prompting user to enter costperunit value and updating immediately in COST table.
I want to import all the values of costperunit column to COMPONENTS from COST Table.
I have tried this :
insert into COMPONENTS(costperunit)
select costperunit from COST where COST.sno=COMPONENTS.sno
but I m unable to achieve the required functionality.
Somebody please suggest me better query to do this..
Thanks in Advance..

If you want to update (!) all values, why are you trying to insert ? Try to use "update from select" way:
UPDATE components
SET components.costperunit=cost.costperunit
FROM cost
INNER JOIN components ON cost.sno=components.sno
Even better to add WHERE clause to update only changed values:

Related

How do aggregate function on two cells of a record in a database table and show its result in another table in MS SQL Server

I have two tables Product and SellingIncome as shown in the screenshot below, and I need in the SellingIncome table to set for the SellingIncome column to insert automatically the multiplication of columns ProductPrice and SellingCount in the Product table (I mean ==>
SellingIncome.SellingIncome = Product.ProductPrice * Product.SellingCount
How can solve this problem? Any help will be appreciated!
Forget about the "sellingincome" table. You can just use a computed column in product:
alter table product add column sellingincome as
(productprice * sellingcount);
Using a computed column means that sellingincome is always up-to-date.

Change column value after INSERT if the value fits criteria?

I have never really worked with Triggers before in MSSQL but I think it'll be what I need for this task.
The structure of the table is as such:
ID|****|****|****|****|****|****|****|TOUROPERATOR
The Tour Operator Code is the code that tells us what company owned the flight we carried out for them. Two of those codes (there are 24 in total) are outdated. Our users requested that those two be changed but the tour operator code is pulled from a database we don't control. The FlightData table however, we do control. So I was thinking a trigger could change the tour operator code if it was one of the two outdated ones, to the correct ones instead respectively when they were inserted.
So I went into good ol' SQL Management Studio and asked to make a trigger. It gave me some sample code and here is my Pseudo Code below:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER ChangeProvider
ON FlightData
AFTER INSERT
AS
BEGIN
IF(TheInsertedValue == Criteria)
UPDATE FlightData
SET TheInsertedValue = NewValue
ENDIF
END
GO
I am not that good with this type of Database Programming so excuse my mistakes.
How would I go about doing this?
You could add a computed column to your table instead of adding a trigger.
Then the new column could just use a case statement to either show
the original TourOperator column value or the new value you wanted.
You'd add a new column to your table like this
TourOperatorCorrect = CASE WHEN TourOperator = 'Whatever value' THEN 'ChangedValue'
--I just want to use what I have already in the TourOperator column
ELSE TourOperator
END AS VARCHAR(50)
Basics of computed columns are here - https://msdn.microsoft.com/en-ie/library/ms188300.aspx
Your misconception here is that the trigger runs once per inserted value - it is in fact run once per insert statement, so you can and will find more than one row inserted at once.
You'll find that your inserted values are in the pseudo table inserted, which has the same structure as your FlightData table in this case. You write a select statement against that, specifying any criteria you wish.
However, it's not immediately clear what your logic is - does the FlightData table you are updating in your trigger only have one row? Do you update every row in the table with the newest inserted value? It is hard to understand what you are trying to now, and what the purpose of the table and this trigger are - let alone what you would want to do if you inserted more than one row at once.
When inserted table contains mutiple rows,your code will fail,so change code to work with inserted table as whole
UPDATE F
SET f.TheInsertedValue = i.value
from inserted i
join
Flighttable F
on f.matchingcolumn=i.matchingcolumn
and i.somevalue='criteria'

Updating columns values from another table SQL

I want to update my table from another table in another database.I have two table that has two same columns.There are ID and iexp column.What i want is update every row from k_monster table to my database's k_monster table but there are other columns such as iHP iMP so i want to just update iExp column.what do you suggest?
Assuming Target_Database is the database where the table is that you want to update and Source_Database is the database where the table is you are using to update from.
Your query should look something like this.....
USE [Target_Database]
GO
UPDATE t
SET t.iexp = S.iexp
FROM K_monster t
INNER JOIN [Source_Database].[Schema].[K_monster] S
ON t.ID = S.ID
GO
Please check this link similar to this type of question:
Additionally I would suggest you to search before you ask any questions.
UPDATE record in one database with values from another in SQL Server 2008?
This link has similar answer to your question.
More links:
Update database table from one SQL Server database table to another?
https://dba.stackexchange.com/questions/30228/how-to-update-one-database-from-another
https://dba.stackexchange.com/questions/58371/sql-update-column-with-data-from-another-table
With Regards

Access Query need to pick parameters from another Table at run time

I need to delete and append the data from a Access Table ULAE. I want to create a parameter query where parameter needs to be picked from another table LAE.
This table has 3 columns(Group,le,qtr) that will be an input and will be part of where condition.
I am trying to run the query:
DELETE FROM t_00_unearned_unincepted_alloc_basis_table as ULAE
INNER JOIN [Table Valued Parameter] as LAE
ON LAE.le=ULAE.le;
It shows the error that specify the input table you want to delete.
Thanks in advance.
Try like this
DELETE ULAE.*
FROM t_00_unearned_unincepted_alloc_basis_table as ULAE
WHERE ULAE.le IN (SELECT le FROM LAE where le= parametervalue)

Update trigger with GROUP BY

I'm using insert-/update triggers to update a second table's column Price.
The insert trigger seems to work perfectly, but when I try to change a single record in SSMS, I get an error:
The row value(s) updated or deleted either do not make the row
unique or they alter multiple rows(2 rows).
This is my update-trigger:
CREATE TRIGGER [dbo].[trgUpdateMasterData] ON [dbo].[tabSparePartMasterData_Temp]
AFTER UPDATE
AS
UPDATE tabSparePart
SET Price = MD.Price
FROM tabSparePart INNER JOIN
(
SELECT inserted.[Material Number (SAP)] AS MaterialNumber, inserted.Price
FROM inserted
GROUP BY [Material Number (SAP)], inserted.Price
) MD
ON tabSparePart.SparePartName = MD.MaterialNumber
I need to group by Material-Number because there are redundant rows inserted into table tabSparePartMasterData_Temp which i'm only using to update the Sparepart-Price in tabSparePart. But i assumed that the group by would sort out the duplicates(Price is same for any duplicate).
It's possible that the inserted/updated records' MaterialNumber is not available in tabSparepart. In this case this record should be "skipped". Does the INNER JOIN takes that into account?
Try adding SET NOCOUNT ON to the trigger
This error doesn't look like a SQL error and I'm guessing the client code is getting confused by the 2nd update in the trigger.
Edit: this error can be caused by the data grid view in SSMS being silly.
This isn't a SQL message as I thought: it is an SSMS being stupid message
See these which all says "learn to write SQL"
http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/dealing-with-the-row-value-s-updated-or (Less than dot blog)
Trigger that modifies multiple rows on diffrent table then it was invoked on in SQL Server 2005
SSMS permits duplicate records in a table, but not subsequent updates
Saying that, there is a KB article about a bug in SQL Server 2005...