Update rows from one table to another table based on Id [duplicate] - sql-server

This question already has answers here:
How do I UPDATE from a SELECT in SQL Server?
(38 answers)
Closed 4 years ago.
Both tables have a client_id column. Need to insert data from #LocalDashboardtable into T004_Dashboard when the client_id column are equal. i have tried this but it does't help shows an error "Incorrect syntax near ','. "
update T004_Dashboard set T004_Dashboard.[GrossCharge],T004_Dashboard.[NetCharge]
= (select #LocalDashboardtable.[GrossCharge] , #LocalDashboardtable.[NetCharge]
from #LocalDashboardtable where
#LocalDashboardtable.client_id =T004_Dashboard.client_id and
#LocalDashboardtable.[month] =T004_Dashboard.[month]
and #LocalDashboardtable.[year] =T004_Dashboard.[year] )
pls help me

This is your query (which looks a lot like SQL Server):
update T004_Dashboard
set T004_Dashboard.[GrossCharge],
T004_Dashboard.[NetCharge] = (select #LocalDashboardtable.[GrossCharge], #LocalDashboardtable.[NetCharge]
from #LocalDashboardtable
where #LocalDashboardtable.client_id = T004_Dashboard.client_id and
#LocalDashboardtable.[month] = T004_Dashboard.[month] and
#LocalDashboardtable.[year] = T004_Dashboard.[year]
);
You cannot set a pair of columns to a pair of columns in a subquery. Instead, use a join:
update T004_Dashboard
set GrossCharge = ld.GrossCharge,
NetCharge = ld.NetCharge
from T004_Dashboard d join
#LocalDashboardtable ld
on ld.[month] = d.[month] and ld.[year] = d.[year] and
ld.client_id = d.client_id;
Also, SQL Server does not allow qualified column names in update/set statements

Related

How to query values when a column has N value

So I have the following table:
And I'm trying to write a Query where I can send the code BR_BN as a variable in my WHERE clause
and if I get BR_BN then I want to retrieve the records with this code AND the records with the Code_FS RB02. On the other side when I get the value AB_CP, I want to include the recordes with the Code_FS RB01.
Here's the Query I've tried so far:
DECLARE #Code_OB VARCHAR(20) = 'BR_BN'
SELECT * FROM Dummy_AV
WHERE FK = 2
OR
(#Code_OB = 'BR_BN' AND Code_FS = 'RB02' AND Code_FS = #Code_OB)
But it doesn't work, it retrieves all the records regardless of the FK, and/or the #Code_FS.
How can I achieve this?
Thanks for the help.
You don't note the FK = 2 being needed, yet you have it in front of an OR in the WHERE clause. I think this is what you're after, if it isn't exactly what you're aiming for hopefully it gets you on the right track. For future questions, always helpful to paste your sample data as data instead of an image.
DECLARE #Code_OB VARCHAR(20) = 'BR_BN'
SELECT * FROM Dummy_AV
WHERE FK = 2 -- you will get all rows where this is true
OR
((#Code_OB = Code_OB AND Code_FS = 'RB02') OR (Code_OB = 'AB_CP' AND Code_FS = 'RB01')) -- you will get all rows where one of these is true

Issues while using <> (not equal to) in sql Query [duplicate]

This question already has answers here:
Why does NULL = NULL evaluate to false in SQL server
(21 answers)
Closed 4 years ago.
I am trying get a result for the following query.
Select * from FACHL where Comp_Year = '2018-2019' and
Trans_date between '04/01/2018' And '03/31/2019' And Order_By <> 'SMAN'
Order By Account_Code,Trans_Date,Document_No,S_no
This gives me no result.
If I remove Order_By <> 'SMAN' from Query , it shows me result.
The data in Order_By column is NULL for all rows.
So I tried Order_By = NULL but no result.
The image shows my data. RESULT IMAGE
Where am I committing mistake here???
Thanx..
SQL NULL means I don't know.
If you want to get NULL row need to use Order_By IS NULL rather than Order_By = NULL
Select *
from FACHL
where Comp_Year = '2018-2019' AND
Trans_date between '04/01/2018' And '03/31/2019' And Order_By IS NULL
Order By Account_Code,Trans_Date,Document_No,S_no
NOTE
NULL can't use <> or = to get the value when the ANSI_NULLS set ON

What is the query for table 3?

I have extracted these two tables from a SQL Database:
query for table 1:
SELECT POM_DOCNO,
POM_DATE,
SUP_CODE,
POM_CREATEDBY
FROM SI_PURORDERMASTER
WHERE POM_YEAR = 2012
AND POM_PERIOD = 6
query for table 2:
SELECT POM_DOCNO,
ITM_ITEMCODE,
ITM_ITEMDESC,
POD_QTY,
POD_RATE
FROM SI_PURORDERDETAIL
WHERE POM_YEAR = 2012
AND POM_PERIOD = 6
query to get table 3 ?
I have tried using joins but always end up with wrong result :/
Is there anyway to get table 3 with just table 1 & 2 ?
Both table 1 and 2 have "POM_DOCNO" column in common.
Try something like that;
SELECT TOP 5
SIR.POM_DOCNO,
SIR.POM_DATE,
SIR.SUP_CODE,
SIL.ITM_ITEMCODE,
SIL.POD_QTY,
SIL.POD_RATE,
SIR.POM_CREATEDBY
FROM SI_PURORDERMASTER SIR inner join SI_PURORDERDETAIL SIL ON SIR.POM_DOCNO = SIL.POM_DOCNO
WHERE SIR.POM_YEAR = 2012
AND SIR.POM_PERIOD = 6
Also, just use TOP to limit results. If you want to specift order you should use order by

Updating one table's column in SQL Server from another

I have a table of measurements from weather stations, with station names (in Hebrew):
I also have created a table of those weather stations with their latitudes and longitudes:
I've written a query that should update the first table with the lat/longs from the second, but it's not working:
update t1
set t1.MeasurementLat = t2.Latitude,
t1.MeasurementLong = t2.Longitude
from [dbo].[Measurements] as t1
inner join [dbo].[StationCoords] as t2 on t1.StationName like t2.Station
I think there is a problem with the way the station name is being read, and perhaps something to do with encoding, because this query brings back an empty result, too:
SELECT TOP (5) *
FROM [dbo].[Measurements]
WHERE [StationName] = 'אריאל מכללה';
Any ideas?
Your example names are not the same. Perhaps this will work:
update m
set MeasurementLat = sc.Latitude,
MeasurementLong = sc.Longitude
from dbo.[Measurements] m join
dbo.[StationCoords] sc
on m.StationName like sc.Station + '%';

Update Weight in magento 1.9 on mass

I am trying to create a MySQL statement that I can put in a php script to update the weight on a few thousand products in magento 1.9.
This is the statement I currently have:
UPDATE dp_catalog_product_entity_decimal AS ped JOIN dp_eav_attribute AS ea ON ea.entity_type_id = 10 AND ea.attribute_code = 'weight' AND ped.attribute_id = ea.attribute_id SET ped.value = 8 WHERE ped.entity_id = P1000X3;
It was partially taken from another post so I am not sure if it will work at all but I currently have the error "#1054 - Unknown column 'P1000X3' in 'where clause'"
I am not that great with sql joins and I dont really know the magento databse at all so any help to get this statement to work is much apreciated.
Thanks.
Matt
The correct SQL to update the weight of a product with a specific SKU in Magento is this (replace YOUR-MAGENTO-SKU with the sku of the item you want to update, and the 8 with the weight value).
UPDATE catalog_product_entity_decimal AS cped
JOIN eav_attribute AS ea ON ea.entity_type_id = (SELECT entity_type_id FROM eav_entity_type WHERE entity_type_code = 'catalog_product')
LEFT JOIN catalog_product_entity AS cpe ON cpe.entity_id = cped.entity_id
AND ea.attribute_code = 'weight'
AND cped.attribute_id = ea.attribute_id
SET cped.value = 8
WHERE cpe.sku = 'YOUR-MAGENTO-SKU';
In your case, you have the table prefix dp_, and the product sku P1000X3, so the correct SQL for you would be:
UPDATE dp_catalog_product_entity_decimal AS cped
JOIN dp_eav_attribute AS ea ON ea.entity_type_id = (SELECT entity_type_id FROM dp_eav_entity_type WHERE entity_type_code = 'catalog_product')
LEFT JOIN dp_catalog_product_entity AS cpe ON cpe.entity_id = cped.entity_id
AND ea.attribute_code = 'weight'
AND cped.attribute_id = ea.attribute_id
SET cped.value = 8
WHERE cpe.sku = 'P1000X3';
After running this, be sure to reindex your Magento Indexes.
Do your Magento tables have the prefix dp_? Make sure of this.
Also on this part:
WHERE ped.entity_id = P1000X3;
ped.entity_id will be an integer value (number).
I'm not sure where you got P1000X3, but using that won't work (it's a string value). Even so, strings should be wrapped with a single quotes ', like this:
'P1000X3';

Resources