Do these simple T-SQL Update statements update identically? - sql-server

In the following example, assuming that NULL values are not allowed.
Are these 2 update statements:
Query #1:
UPDATE #R12
SET SOURCE_NAME = DEFLT.SOURCE_NAME
FROM R12_MLR_REBATE_GL_STRING_DEFLT DEFLT
WHERE #R12.PAYMT_TY = DEFLT.PAYMT_TY
AND #R12.AMOUNT_TYPE = DEFLT.AMOUNT_TYPE
AND #R12.COMPANY <> #BNKER_COMPANY
AND DEFLT.BNKER_IND = 'N'
Query #2:
UPDATE #R12
SET SOURCE_NAME = DEFLT.SOURCE_NAME
FROM R12_MLR_REBATE_GL_STRING_DEFLT DEFLT
WHERE #R12.PAYMT_TY = DEFLT.PAYMT_TY
AND #R12.AMOUNT_TYPE = DEFLT.AMOUNT_TYPE
AND #R12.COMPANY = #BNKER_COMPANY
AND DEFLT.BNKER_IND = 'Y'
equivalent to this one update statement?
UPDATE #R12
SET SOURCE_NAME = DEFLT.SOURCE_NAME
FROM R12_MLR_REBATE_GL_STRING_DEFLT DEFLT
WHERE #R12.PAYMT_TY = DEFLT.PAYMT_TY
AND #R12.AMOUNT_TYPE = DEFLT.AMOUNT_TYPE
AND ((#R12.COMPANY <> #BNKER_COMPANY AND DEFLT.BNKER_IND = 'N')
OR (#R12.COMPANY = #BNKER_COMPANY AND DEFLT.BNKER_IND = 'Y')
)
Do I get the same behavior if I use a JOIN syntax? By this I mean that if I run them both, in that order, that they will provide the same eventual output as running the 'unified' version.
UPDATE R12
SET SOURCE_NAME = DEFLT.SOURCE_NAME
FROM #R12 R12
INNER JOIN R12_MLR_REBATE_GL_STRING_DEFLT DEFLT ON R12.PAYMT_TY = DEFLT.PAYMT_TY
AND R12.AMOUNT_TYPE = DEFLT.AMOUNT_TYPE
AND ((R12.COMPANY <> #BNKER_COMPANY AND DEFLT.BNKER_IND = 'N')
OR (R12.COMPANY = #BNKER_COMPANY AND DEFLT.BNKER_IND = 'Y'))
I'm experiencing some paranoia and would feel better with a second opinion.
Thank you

Related

How to include IF ELSE in SQL Query

I am attempting to put an IF ELSE statement into the follow SQL Query, but it keeps breaking when I try to execute. I have never used IF ELSE in SQL so I am assuming I am not including it correctly
Query without IF ELSE
UPDATE RA SET
CreditInvoiceAmount = CSV.CreditInvoiceAmount,
CreditInvoiceDate = CSV.CreditInvoiceDate,
CreditInvoiceNumber = CSV.CreditInvoiceNumber,
CreditDeniedDate = CSV.CreditDeniedDate,
CreditDeniedReasonId = CSV.CreditDeniedReasonId,
CreditDeniedNotes = CSV.CreditDeniedNotes
FROM ReturnAuthorization RA
JOIN TemporaryCsvUpload CSV
on RA.Id = CSV.Id
IF ELSE
IF CSV.CreditInvoiceDate = null AND CSV.CreditDeniedDate != null THEN
StatusId = 7;
ELSE
StatusId = 8;
Insert Attempt
UPDATE RA SET
CreditInvoiceAmount = CSV.CreditInvoiceAmount,
CreditInvoiceDate = CSV.CreditInvoiceDate,
CreditInvoiceNumber = CSV.CreditInvoiceNumber,
CreditDeniedDate = CSV.CreditDeniedDate,
CreditDeniedReasonId = CSV.CreditDeniedReasonId,
CreditDeniedNotes = CSV.CreditDeniedNotes,
IF CSV.CreditInvoiceDate = null AND CSV.CreditDeniedDate != null THEN
StatusId = 7;
ELSE
StatusId = 8;
FROM ReturnAuthorization RA
JOIN TemporaryCsvUpload CSV
on RA.Id = CSV.Id
Any guidance on how to correctly put this if statement into the SQL block would be appreciated. Thanks!
Use a CASE expression to implement a IF logic based on which you determine what value to assign into a column for a specific row.
UPDATE RA
SET CreditInvoiceAmount = CSV.CreditInvoiceAmount
,CreditInvoiceDate = CSV.CreditInvoiceDate
,CreditInvoiceNumber = CSV.CreditInvoiceNumber
,CreditDeniedDate = CSV.CreditDeniedDate
,CreditDeniedReasonId = CSV.CreditDeniedReasonId
,CreditDeniedNotes = CSV.CreditDeniedNotes
,StatusId = CASE
WHEN CSV.CreditInvoiceDate IS NULL
AND CSV.CreditDeniedDate IS NOT NULL
THEN 7
ELSE 8
END
FROM ReturnAuthorization RA
INNER JOIN TemporaryCsvUpload CSV ON RA.Id = CSV.Id
CASE is valid for both SQL Server and MySQL, so it should work with either systems. Also, please consider editing your question and leaving only the relevant tag (what RDBMS you're actually using).
// You can try this
StatusId = IF ((CSV.CreditInvoiceDate IS NULL AND CSV.CreditDeniedDate IS NOT NULL), 7, 8)

Query conversion from sybase to SQL Server

I have one query that is to convert from sybase to SQL server, I think we just need to change join operations.. Here is part of the query:
Can someone help me here?
FROM sy_trcr_divided d, fd_income_trans t
WHERE d.fd_id_income is not null
AND t.fd_income_transfer_id = d.fd_proc_id
AND t.fd_income_est_yn = 'Y'
AND d.fd_id_income = a.fd_id
AND d.fd_dist_to_i_or_p = 'A'
AND d.gl_year = :p_lYear
AND d.gl_period = :p_lPeriod
AND d.fd_include_stip_yn = 'Y'),0) AS ADDTO_FUND
FROM sy_est_income_detail a,
fd_master
WHERE a.fd_id *= fd_master.fd_id and
a.hr_id = :p_szHRID
My thought: have to replace *= with a join.
The *= should be a left join and the piece of the query you provided should probably be:
FROM sy_trcr_divided d
INNER JOIN fd_income_trans t ON t.fd_income_transfer_id = d.fd_proc_id
WHERE d.fd_id_income is not null
AND t.fd_income_est_yn = 'Y'
AND d.fd_id_income = a.fd_id
AND d.fd_dist_to_i_or_p = 'A'
AND d.gl_year = :p_lYear
AND d.gl_period = :p_lPeriod
AND d.fd_include_stip_yn = 'Y'),0) AS ADDTO_FUND
FROM sy_est_income_detail a
LEFT JOIN fd_master ON a.fd_id = fd_master.fd_id
WHERE a.hr_id = :p_szHRID

oracle query to check not exists in other table

I have following query:
select vw.CONFERENCEID, alert.ALERTID, del.CHANNELID, del.DOCUMENTTEMPLATEID,
vw.starttime, vw.CONFERENCEID, alert.ALERTATTRIBUTEID,
alert.ALERTCATEGORYID, alert.ATTACHMENT, alert.ATTACHMENTLOCATION,
alert.ATTACHMENTNAME
from TBLMCONFERENCE vw,
TBLMSYSTEMALERTS alert,
TBLMALERTDELIVERYREL del,
tblmstandardmaster sm
WHERE alert.Alertid = del.alertid
and sm.masterid = del.CHANNELID
and alert.SYSTEMGENERATED = 'N'
and alert.alertid not in (
select sent.ALERTID
from TBLMSENTALERTHISTORY sent
where sent.REFACCOUNTID = vw.conferenceid
and sent.ALERTID = alert.ALERTID
and sent.CHANNELID = del.CHANNELID
and sent.RESETFLAG = 'N')
I am getting records even when a matching record exists in TBLMSENTALERTHISTORY table.
Is there anything wrong in this query?
I'm not sure why you are getting data, maybe you could build a small example with a couple of tables and inserts.
However, I can point that your use of the NOT IN operator is not standard: in most cases you should not join the subquery to the main query with IN and NOT IN operators (it is redundant). Instead you would write:
SELECT *
FROM TBLMCONFERENCE vw,
TBLMSYSTEMALERTS alert,
TBLMALERTDELIVERYREL del,
tblmstandardmaster sm
WHERE alert.Alertid = del.alertid
AND sm.masterid = del.CHANNELID
AND alert.SYSTEMGENERATED = 'N'
AND (alert.alertid, del.CHANNELID, vw.conferenceid)
NOT IN (SELECT sent.ALERTID, sent.CHANNELID, sent.conferenceid
FROM TBLMSENTALERTHISTORY sent
WHERE sent.RESETFLAG = 'N')
In your case you could use your subquery directly with NOT EXISTS:
SELECT *
FROM TBLMCONFERENCE vw,
TBLMSYSTEMALERTS alert,
TBLMALERTDELIVERYREL del,
tblmstandardmaster sm
WHERE alert.Alertid = del.alertid
AND sm.masterid = del.CHANNELID
AND alert.SYSTEMGENERATED = 'N'
AND NOT EXISTS
(SELECT sent.ALERTID
FROM TBLMSENTALERTHISTORY sent
WHERE sent.REFACCOUNTID = vw.conferenceid
AND sent.ALERTID = alert.ALERTID
AND sent.CHANNELID = del.CHANNELID
AND sent.RESETFLAG = 'N')
Note that in general NOT IN and NOT EXISTS are not equivalent, because of NULLs.

SQL Server Case statement in WHERE Clause

I tried to google for CaseStatement in WHERE clause. But i didn't find similar to my scenario.
Below is my SQL Statement with CASE Statement in WHERE clause. If PartName = B, then i should apply (RecoveraleFlag = 1) condition along with other conditions. Else This condition should not apply but all other conditions should remain.
FROM Rec.Communications A
INNER JOIN REC.CommunicationTypes B ON A.CommunicationTypeKey = B.CommunicationTypeKey
INNER JOIN occ.Cases c ON a.CaseId = c.CaseId
INNER JOIN occ.Claims cl on a.CaseId = cl.CaseId
INNER JOIN ops.Concepts d ON c.ConceptKey = d.ConceptKey
INNER JOIN OPS.Regions f ON d.MODSRegionKey = f.MODSRegionKey
INNER JOIN COM.RepriceRequestOccurrences e ON a.CommunicationId = e.CommunicationId
INNER JOIN occ.Providers prv ON c.MODSProviderKey = prv.MODSProviderKey
WHERE
**(
CASE WHEN f.PartName = 'B' and e.RecoverableFlag = 1 then 1
ELSE 0
END
) = 1**
AND
b.CommunicationTypeCode = 'RREQ'
AND f.Region = #Region
AND a.CurrentFlag = 1
Here Case Statement in where clause is working fine if Partname = B. For Partname A, this will be 0=1 – always false. Because of this it is not returning any data.
Can anyone gives any alternatives.
As a case statement, you would write this as:
CASE WHEN f.PartName = 'B' and e.RecoverableFlag = 1 then 1
WHEN f.ParName = 'A' then 1
ELSE 0 END ) = 1
Is this the logic you want?
Many would think that the case statement is irrelevant here, and instead use:
WHERE ((f.PartName = 'B' and e.RecoverableFlag = 1) or (f.partName <> 'B')) . . .
WHERE
(
f.PartName <> 'B'
OR e.RecoverableFlag = 1
)
AND b.CommunicationTypeCode = 'RREQ'
AND f.Region = #Region
AND a.CurrentFlag = 1
You might find reading on De Morgan's Laws interesting.
I don't believe you need a case statement at all.. The following should work...
WHERE f.PartName = 'B'
And e.RecoverableFlag = 1
AND b.CommunicationTypeCode = 'RREQ'
AND f.Region = #Region
AND a.CurrentFlag = 1
But if you want to use a case statement in a where clause, try to put it on the "other" side of the comparison operator from the column name so the query can process it without doing a table scan...
as an e.g.,
Where f.Partname =
Case b.CommunicationTypeCode
When 'RREQ' Then 'B'
When 'NOTREQ' Then 'C'
When 'OPT' Then 'D'
Else 'F' End

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