Mass Updates in SQL Server - sql-server

I am trying to do a mass update on short codes in SQL Server, but it doesn't return any change:
SELECT DISTCINT
a.site_area_sc, a.site_area_n, a.site_area_rmk, d.site_n 'NAME', d.site_sc,
UPDATE DB.DBO.bg
SET bg_sc = 'XXX-' + bg.bg_sc
FROM DB.DBO.bg
INNER JOIN DB.DBO.site ON s.site_id = bg.site_id
WHERE site.site_sc like 'XXX-%'
AND bg.stat_flag = 'n'
AND s.stat_flag = 'n'
AND bg.bg_sc not like 'XXX-%'
I am in need of help.

Related

Update multiple tables with one query

Record to update
select *
from Event_Measurable em
join Observation_Measurable om on em.Event_GUID = om.Event_GUID
where observation_guid in (8786975, 285886, 85976, 786976)
Update these records as follows:
set observation_value_text = '.',
observation_value_numeric = NULL,
om.status = 'D',
em.status = 'D',
I need help to update and set this values from the results of the query above. Can any help me. I have tried to update the table but it failed.
Try this:
update X set
observation_value_text = '.'
, observation_value_numeric = NULL
, om.status = 'D'
, em.status = 'D'
from
(
select
*
from Event_Measurable em
join Observation_Measurable om on em.Event_GUID = om.Event_GUID
where observation_guid in (8786975, 285886, 85976, 786976)
) X
Let me know if it works?
Your set list references multiple tables: that is invalid as only one table can be updated with one statement. You need to run 2 update statements so you either repeat the join in the second update or first put the result in a temp table and join that to the actual to-be-updated table. You might want to put the whole thing to a transaction to be able to roll back on error. See this reference.

How to implement incremental load using insert/update instead of merge in stored procedure?

I have implemented incremental load using merge, but I want using insert/update. Please help.
This is the stored procedure for the merge method. The stored procedure below loads data from source only when there are new records inserted, else it will discard. Updates only when there are updates in the record.
ALTER PROCEDURE [dbo].[LOAD_DIM_ADDRESS]
AS
BEGIN
MERGE [VINCE_RETAIL_TEST].[dbo].[DIM_ADDRESS] AS T
USING (SELECT
'1' as [COMPANY_KEY],
C.[CUSTOMER_KEY],
A.[ADDRESS_ID], A.[ADDRESS_TYPE],
'0' as [REGION_KEY],
'-1' as [COUNTRY_KEY],
'-1' as [STATE_KEY],
A.[CITY], A.[POSTAL_CODE]
FROM
[AX_STAGING].[dbo].[DIM_ADDRESS] A
INNER JOIN
[VINCE_RETAIL_TEST].[dbo].[DIM_CUSTOMER] C ON C.[CUSTOMER_ID] = A.[CUSTOMER_ID]
AND C.[ADDRESS_ID] = A.[ADDRESS_ID]) AS S
ON (T.[ADDRESS_ID] = S.[ADDRESS_ID] AND T.[ADDRESS_TYPE]=S.[ADDRESS_TYPE])
WHEN MATCHED
THEN
UPDATE
SET
T.[COMPANY_KEY] = S.[COMPANY_KEY],
T.[CUSTOMER_KEY] = S.[CUSTOMER_KEY],
T.[ADDRESS_ID] = S.[ADDRESS_ID],
T.[ADDRESS_TYPE] = S.[ADDRESS_TYPE],
T.[REGION_KEY] = S.[REGION_KEY],
T.[COUNTRY_KEY] = S.[COUNTRY_KEY],
T.[STATE_KEY] = S.[STATE_KEY],
T.[CITY] = S.[CITY],
T.[POSTAL_CODE] = S.[POSTAL_CODE]
WHEN NOT MATCHED BY TARGET
THEN
INSERT ([COMPANY_KEY], [CUSTOMER_KEY], [ADDRESS_ID], [ADDRESS_TYPE],
[REGION_KEY], [COUNTRY_KEY], [STATE_KEY], [CITY],
[POSTAL_CODE])
VALUES (S.[COMPANY_KEY], S.[CUSTOMER_KEY], S.[ADDRESS_ID], S.[ADDRESS_TYPE],
S.[REGION_KEY], S.[COUNTRY_KEY], S.[STATE_KEY], S.[CITY],
S.[POSTAL_CODE]);
END
Thanks in advance
You need to differentiate when to use INSERT, when to use UPDATE. So for example, only when (S.[ADDRESS_ID], S.[ADDRESS_TYPE]) does not exist in the target table, then do the INSERT. The code is listed. Similarly you can write the UPDATE part as a practice. :)
INSERT [VINCE_RETAIL_TEST].[dbo].[DIM_ADDRESS]
([COMPANY_KEY], [CUSTOMER_KEY], [ADDRESS_ID], [ADDRESS_TYPE],
[REGION_KEY], [COUNTRY_KEY], [STATE_KEY], [CITY],
[POSTAL_CODE])
SELECT S.[COMPANY_KEY], S.[CUSTOMER_KEY], S.[ADDRESS_ID], S.[ADDRESS_TYPE],
S.[REGION_KEY], S.[COUNTRY_KEY], S.[STATE_KEY], S.[CITY],
S.[POSTAL_CODE]
FROM (SELECT
'1' as [COMPANY_KEY],
C.[CUSTOMER_KEY],
A.[ADDRESS_ID], A.[ADDRESS_TYPE],
'0' as [REGION_KEY],
'-1' as [COUNTRY_KEY],
'-1' as [STATE_KEY],
A.[CITY], A.[POSTAL_CODE]
FROM
[AX_STAGING].[dbo].[DIM_ADDRESS] A
INNER JOIN
[VINCE_RETAIL_TEST].[dbo].[DIM_CUSTOMER] C ON C.[CUSTOMER_ID] = A.[CUSTOMER_ID]
AND C.[ADDRESS_ID] = A.[ADDRESS_ID]) AS S
WHERE (S.[ADDRESS_ID], S.[ADDRESS_TYPE]) NOT IN (SELECT [ADDRESS_ID],[ADDRESS_TYPE] FROM [VINCE_RETAIL_TEST].[dbo].[DIM_ADDRESS])

SQL Query Help - searching on multiple 'pairs' or data

I'm struggling to work out how to do a SQL query on a database that I have.
I have a view (which can be changed) which shows the relationships between the tables.
This creates a view as follows:
What I need to be able to do is search on one or more 'Attribute Pairs'
for example
I want to search for records with:
(
(AttributeName='FileExtension' AND AttributeValue='.pdf')
AND (AttributeName='AccountNumber' AND AttributeValue='ABB001'
)
As you can tell, this is not working as AttributeName cant be two things at once. I have this working with an OR filter, but I want it to find records that have all attribute pairs
SELECT
dbo.SiconDMSDocument.SiconDMSDocumentID,
dbo.SiconDMSAttribute.SiconDMSAttributeID,
dbo.SiconDMSAttribute.AttributeFriendlyName,
dbo.SiconDMSAttribute.AttributeName,
dbo.SiconDMSDocumentAttribute.AttributeValue,
dbo.SiconDMSAttribute.DataType,
dbo.SiconDMSDocumentType.SiconDMSDocumentTypeID,
dbo.SiconDMSDocumentType.DocumentTypeName,
dbo.SiconDMSDocumentType.DocumentTypeFriendlyName,
dbo.SiconDMSModule.SiconDMSModuleID,
dbo.SiconDMSModule.ModuleName,
dbo.SiconDMSModule.ModuleFriendlyName,
dbo.SiconDMSDocument.SiconDMSDocumentTypeModuleID
FROM dbo.SiconDMSDocument
INNER JOIN dbo.SiconDMSDocumentAttribute ON dbo.SiconDMSDocument.SiconDMSDocumentID = dbo.SiconDMSDocumentAttribute.SiconDMSDocumentID
INNER JOIN dbo.SiconDMSAttribute ON dbo.SiconDMSDocumentAttribute.SiconDMSAttributeID = dbo.SiconDMSAttribute.SiconDMSAttributeID
AND
(
(dbo.SiconDMSAttribute.AttributeName = 'Reference' AND dbo.SiconDMSDocumentAttribute.AttributeValue='12345')
OR (dbo.SiconDMSAttribute.AttributeName = 'AccountNumber' AND dbo.SiconDMSDocumentAttribute.AttributeValue='ABB001')
)
INNER JOIN dbo.SiconDMSDocumentTypeModule ON dbo.SiconDMSDocument.SiconDMSDocumentTypeModuleID = dbo.SiconDMSDocumentTypeModule.SiconDMSDocumentTypeModuleID
INNER JOIN dbo.SiconDMSDocumentType ON dbo.SiconDMSDocumentTypeModule.SiconDMSDocumentTypeID = dbo.SiconDMSDocumentType.SiconDMSDocumentTypeID
INNER JOIN dbo.SiconDMSModule ON dbo.SiconDMSDocumentTypeModule.SiconDMSModuleID = dbo.SiconDMSModule.SiconDMSModuleID
WHERE
(dbo.SiconDMSDocument.Deleted = 0)
AND (dbo.SiconDMSDocumentAttribute.Deleted = 0)
AND (dbo.SiconDMSAttribute.Deleted = 0)
AND (dbo.SiconDMSDocumentType.Deleted = 0)
AND (dbo.SiconDMSDocumentTypeModule.Deleted = 0)
AND (dbo.SiconDMSModule.Deleted = 0)
Are there any SQL functions that will allow me to do something like this?
I'm not sure what your complicated query has to do with the question of searching for attribute pairs.
Assuming you want the document ids that have both attributes:
select SiconDMSDocumentID
from yourview y
where (AttributeName = 'FileExtension' AND AttributeValue = '.pdf') or
(AttributeName = 'AccountNumber' AND AttributeValue = 'ABB001'
group by SiconDMSDocumentID
having count(*) = 2;
Or, if the attributes could have multiple values:
having count(distinct AttributeName) = 2

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';

What is the problem with the logic in my UPDATE statement?

I would appreciate some help with an UPDATE statement.
I want to update tblOrderHead with the content from tblCustomer where the intDocumentNo corresponds to the parameter #intDocumentNo. But when I run the my statement, the order table is only updated with the content from the first row of the customer table.
What is the problem with my logic?
I use Microsoft SQL Server.
Thanks,
Stefan
UPDATE dbo.tblOrderHead
SET dbo.tblOrderHead.intCustomerNo = #intCustomerNo ,
dbo.tblOrderHead.intPaymentCode = dbo.tblCustomer.intPaymentCode,
dbo.tblOrderHead.txtDeliveryCode = dbo.tblCustomer.txtDeliveryCode,
dbo.tblOrderHead.txtRegionCode = dbo.tblCustomer.txtRegionCode,
dbo.tblOrderHead.txtCurrencyCode = dbo.tblCustomer.txtCurrencyCode,
dbo.tblOrderHead.txtLanguageCode = dbo.tblCustomer.txtLanguageCode
FROM dbo.tblOrderHead
INNER JOIN dbo.tblCustomer ON dbo.tblOrderHead.intOrderNo = #intDocumentNo
Solution
If anyone as stupid as me out there thing the same thing, this is how you solve it:
UPDATE dbo.tblOrderHead
SET dbo.tblOrderHead.intCustomerNo = #intCustomerNo ,
dbo.tblOrderHead.intPaymentCode = dbo.tblCustomer.intPaymentCode,
dbo.tblOrderHead.txtDeliveryCode = dbo.tblCustomer.txtDeliveryCode,
dbo.tblOrderHead.txtRegionCode = dbo.tblCustomer.txtRegionCode,
dbo.tblOrderHead.txtCurrencyCode = dbo.tblCustomer.txtCurrencyCode,
dbo.tblOrderHead.txtLanguageCode = dbo.tblCustomer.txtLanguageCode
FROM dbo.tblOrderHead
INNER JOIN dbo.tblCustomer ON dbo.tblOrderHead.intCustomerNo = dbo.tblCustomer.intCustomerNo
AND dbo.tblOrderHead.intOrderNo = #intDocumentNo
Problem is that, you are not specifying the condition on which the 2 tables tblOrderHead, tblCustomer need to be joined!
you need something like
INNER JOIN dbo.tblCustomer
ON dbo.tblOrderHead.someColumn = dbo.tblCustomer.someColumn
and dbo.tblOrderHead.intOrderNo = #intDocumentNo

Resources