SQL Server Update Statement - sql

Here is my stored procedure for updating tables in SQL Server, but I can not seem to get it working what is wrong with the statement.
Update pc.PatientCopayId, pc.amount, pc.patientid, pc.apptid ,p.PaymentId,p.PaymentDate,p.PayorType,p.PaymentMethod,
p.RefNumber,p.PaymentAmount,p.PayorId,pt.LastFirstName As PatientName,
ISNULL((SELECT note FROM dbo.PatientNote WHERE NoteTypeId = 28 AND KeyValue = pc.PatientCopayId),'') AS Note
from [dbo].[PatientCopay] pc, dbo.pymt_Payment p, dbo.Patient pt
where ApptId = #ApptId
and p.PaymentId = pc.Paymentid
And pt.PatientId = p.PayorId
Values and meaning
pc.amount, = #PaymentAmount
pc.patientid, = #PatientId
pc.apptid , = #ApptId
p.PaymentId, = #PaymentId
p.PaymentDate, = #PaymentDate
p.PayorType, = #PayorType
p.PaymentMethod, = #PaymentMethod
p.RefNumber, = #RefNumber
p.PaymentAmount, = #PaymentAmount
p.PayorId, = #PayorId

The UPDATE statement shoudl have the following form
UPDATE TableName SET....
Your update statement doesn't have the tablename or the SET keyword.
More Info HERE

Related

Procedure showing an error related to merge in sql and please verify that overall code is fine or not

Code is showing an error
Merge statements with a WHEN NOT MATCHED [BY TARGET] clause must target a hash distributed table
Also please verify that the overall syntax is fine or not?
MERGE IR_CREDITREQUEST_SPTESTING AS T
USING (
SELECT
TT.TaxYear
,TT.TaxPayerSSN
,TT.EngagementID
,TT.CreditRequestType
,TT.BorrowerID
,TT.NameSuffix
,TT.FirstName
,TT.MiddleName
,TT.LastName
,TT.SpouseName
,TT.SpouseSSN
,TT.PrintPositionType
,TT.MaritalStatusType
,TT.StreetAddress
,TT.City
,TT.State
,TT.PostalCode
,TT.Type
,TT.Value
,TT.RequestDateTime
,TT.CreatedDateTime
,TT.RequestProcessed
,TT.BorrowerResidencyType
,TT.isActive
FROM
IR_CREDITREQUEST_SPTESTING TT
INNER JOIN (
select DISTINCT
A.TaxYear,
A.[TaxPayer-SSN] AS TaxPayerSSN,
A.EngagementID AS EngagementID,
CASE
WHEN A.[Filing Status] = 'MFJ' THEN 'Joint'
ELSE A.[Filing Status]
END AS [CreditRequestType],
'' AS BorrowerID ,
A.[TaxPayer-Title/Suffix] AS NameSuffix,
A.[TaxPayer-FirstName] AS FirstName,
'' AS MiddleName,
A.[TaxPayer-LastName] AS LastName,
B.[Spouse-FirstName] + ' ' + B.[Spouse-LastName] AS [SpouseName],
B.[Spouse-SSN] AS [SpouseSSN],
CASE
WHEN A.[Filing Status] = 'MFJ' THEN 'CoBorrower'
WHEN A.[Filing Status] = 'Single' THEN 'Borrower'
ELSE A.[Filing Status]
END AS [PrintPositionType],
CASE
WHEN B.[Spouse-FirstName] IS not null THEN 'Married'
ELSE 'Unmarried'
END AS [MaritalStatusType],
C.[Address] AS StreetAddress,
C.City AS City,
C.[State] AS State,
C.[Postal Code] AS PostalCode,
A.[Primary Contact] AS Type,
A.[TaxPayer-Home/Evening Telephone Number] AS Value,
NULL AS RequestDateTime,
GETDATE() AS CreatedDateTime,
NULL AS RequestProcessed,
NULL AS BorrowerResidencyType,
0 AS IsActive
from DimTaxPayerInfo A
LEFT join DimTaxPayerSpouseInfo B on A.[TaxPayer-SSN] = B.[TaxPayer-SSN] AND B.TaxSoftwareId = 4
LEFT join stg.stg_DimTaxPayerAddress C on A.[TaxPayer-SSN] = C.[TaxPayer-SSN]
WHERE A.[TaxPayer-SSN] != ''
) as Y
ON TT.[EngagementID] = Y.[EngagementID]
) DD
ON T.[EngagementID] = DD.[EngagementID]
WHEN MATCHED THEN
Update
SET T.TaxYear = DD.TaxYear
,T.TaxPayerSSN = dd.TaxPayerSSN
,T.EngagementID = dd.EngagementID
,T.CreditRequestType = dd.CreditRequestType
,T.BorrowerID = dd.BorrowerID
,T.NameSuffix = dd.NameSuffix
,T.FirstName = dd.FirstName
,T.MiddleName = dd.MiddleName
,T.LastName = dd.LastName
,T.SpouseName = dd.SpouseName
,T.SpouseSSN = dd.SpouseSSN
,T.PrintPositionType = dd.PrintPositionType
,T.MaritalStatusType = dd.MaritalStatusType
,T.StreetAddress = dd.StreetAddress
,T.City = dd.City
,T.State = dd.State
,T.PostalCode = dd.PostalCode
,T.Type = dd.Type
,T.Value = dd.Value
,T.RequestDateTime = dd.RequestDateTime
,T.CreatedDateTime = dd.CreatedDateTime
,T.RequestProcessed = dd.RequestProcessed
,T.BorrowerResidencyType = dd.BorrowerResidencyType
,T.IsActive = dd.IsActive
WHEN NOT MATCHED THEN
INSERT
( TaxYear
,TaxPayerSSN
,EngagementID
,CreditRequestType
,BorrowerID
,NameSuffix
,FirstName
,MiddleName
,LastName
,SpouseName
,SpouseSSN
,PrintPositionType
,MaritalStatusType
,StreetAddress
,City
,State
,PostalCode
,Type
,Value
,RequestDateTime
,CreatedDateTime
,RequestProcessed
,BorrowerResidencyType
,IsActive
)
values (
dd.TaxYear
,dd.TaxPayerSSN
,dd.EngagementID
,dd.CreditRequestType
,dd.BorrowerID
,dd.NameSuffix
,dd.FirstName
,dd.MiddleName
,dd.LastName
,dd.SpouseName
,dd.SpouseSSN
,dd.PrintPositionType
,dd.MaritalStatusType
,dd.StreetAddress
,dd.City
,dd.State
,dd.PostalCode
,dd.Type
,dd.Value
,dd.RequestDateTime
,dd.CreatedDateTime
,dd.RequestProcessed
,dd.BorrowerResidencyType
,dd.IsActive
);
Your Merge Statement looks correct. This error is mostly seen in Azure Synapse. Make sure your target table is hash distributed to avoid this error.
Refer to the answer posted in the thread for a similar error.

Subquery returned more than 1 value. Update Select Minus

I have the following update scrpit..
UPDATE LIMRA_Retail_Current
SET AnnualizedWOExcessPremium = (SELECT
ISNULL(ul.AnnualizedWOExcessPremium, 0)-
isnull((select iul.AnnualizedWOExcessPremium
from dbo.LIMRA_Retail_Current iul
where ul.Distribution = iul.distribution
and ul.company = iul.company
and ul.Year = iul.year
and ul.Quarter = iul.quarter
and iul.Market = 'Retail'
and replace(ul.Product, 'Universal Life', 'Index Universal Life') = iul.product
),0) UL_MINUS_IUL
FROM dbo.LIMRA_Retail_Current UL
WHERE ul.Product like 'Universal Life%'
AND ul.Market = 'Retail')
How can I update the column AnnualizedWOExcessPremium using ALL the values of UL_MINUS_IUL. I want to use ALL the values in my update.
Looks like your sub select just needs a SUM:
(SELECT
SUM(ISNULL(ul.AnnualizedWOExcessPremium, 0)- isnull((select iul.AnnualizedWOExcessPremium )
from dbo.LIMRA_Retail_Current iul
where ul.Distribution = iul.distribution
and ul.company = iul.company
and ul.Year = iul.year
and ul.Quarter = iul.quarter
and iul.Market = 'Retail'
and replace(ul.Product, 'Universal Life', 'Index Universal Life') = iul.product
),0) UL_MINUS_IUL

Updating two tables in one SQL query

I'm attempting to update my Scout and ScoutRole tables from one SQL query. I've tried to follow this example: How to update two tables in one statement in SQL Server 2005?.
But I keep receiving the error message
The multi-part identifier "SR.Role" could not be bound
How do I resolve this?
BEGIN TRANSACTION
UPDATE Scout
SET Scout.FirstName = #ScoutFirstName,
Scout.LastName = #ScoutLastName,
Scout.EmailAddress = #EmailAddress,
Scout.ClubID = #ClubID
FROM Scout S, ScoutRole SR
WHERE S.ScoutID = SR.ScoutID AND S.ScoutID = #ScoutID
UPDATE ScoutRole
SET SR.Role = #ScoutRole,
SR.Username = #Username,
SR.Password = #Password
FROM Scout S, ScoutRole SR
WHERE S.ScoutID = SR.ScoutID AND S.ScoutID = #ScoutID
COMMIT
This should be all you need to use:
BEGIN TRANSACTION
UPDATE Scout
SET FirstName = #ScoutFirstName,
LastName = #ScoutLastName,
EmailAddress = #EmailAddress,
ClubID = #ClubID
WHERE ScoutID = #ScoutID
UPDATE ScoutRole
SET Role = #ScoutRole,
Username = #Username,
Password = #Password
WHERE ScoutID = #ScoutID
COMMIT

SQL if else stored procedure

I'm writing a stored procedure and it's working for if but not for else. For if I'm getting the correct ID value but for else it's just giving me null.
SELECT #ID = ID FROM PRODUCTS WHERE SN = #SN
SELECT #Chip_ID = Chip_ID FROM PRODUCTS WHERE SN = #SN
SELECT #Power = Power From Module_Cycle WHERE ChipID = #Chip_ID
SELECT #Test_ID=Test_ID FROM TestEE_Mod WHERE ID=#ID AND #TypeID = TypeID
IF(#Test_ID IS NOT NULL)
BEGIN
IF(#TypeID = '3')
BEGIN
SELECT #Temp_TestID=TestID FROM TempCycle WHERE ChipID = #Chip_ID AND #Power = 'false'
BEGIN
UPDATE TestEE_Mod SET Temp_TestID = #Temp_TestID WHERE ID = #ID AND TypeID = #TypeID
END
END
ELSE
BEGIN
SELECT #Temp_TestID=TestID FROM TempCycle WHERE ChipID = #Chip_ID AND #Power = 'true'
BEGIN
UPDATE TestEE_Mod SET Temp_TestID = #Temp_TestID WHERE ID = #ID AND TypeID = #TypeID
END
END
END
Most likely the issue lies in the #Power variable. In your two SELECT statements, the only difference is that one includes #Power = 'false' in the WHERE clause, while the other includes #Power = 'true'.
Since #Power is a variable, not an actual column in your TempCycle table, I'm guessing you actually meant to filter by Power = #Power in both of them.

How to write Update with join in where clause

I am trying to run this SQL to update a table. The issue is that I need a join in order to update the correct record. This is what I have come up with so far:
Update UserProfile
set UserProfile.PropertyValue = 'Test'
join ProfilePropertyDefinition
on ProfilePropertyDefinition.Id = UserProfile.PropertyDefinitionId
where UserProfile.UserId = 11
and ProfilePropertyDefinition.PortalID = 0
and ProfilePropertyDefinition.PropertyName = 'Address1'
and ProfilePropertyDefinition.PropertyCategory = 'Address'
This is the message I get:
Incorrect syntax near the keyword 'join'.
You are almost there, you forgot the from clause:
Update UserProfile
set UserProfile.PropertyValue = 'Test'
from UserProfile
join ProfilePropertyDefinition
on ProfilePropertyDefinition.Id = UserProfile.PropertyDefinitionId
where UserProfile.UserId = 11
and ProfilePropertyDefinition.PortalID = 0
and ProfilePropertyDefinition.PropertyName = 'Address1'
and ProfilePropertyDefinition.PropertyCategory = 'Address'
Update UserProfile
set UserProfile.PropertyValue = 'Test'
from UserProfile
join ProfilePropertyDefinition
on ProfilePropertyDefinition.Id = UserProfile.PropertyDefinitionId
where UserProfile.UserId = 11
and ProfilePropertyDefinition.PortalID = 0
and ProfilePropertyDefinition.PropertyName = 'Address1'
and ProfilePropertyDefinition.PropertyCategory = 'Address'
You have to repeat the table to update in the from clause - even iof this syntax looks a little strange.
You are missing a FROM clause:
Update a
set PropertyValue = 'Test'
FROM UserProfile as a
inner join ProfilePropertyDefinition as b
on b.Id = a.PropertyDefinitionId
where a.UserId = 11
and b.PortalID = 0
and b.PropertyName = 'Address1'
and b.PropertyCategory = 'Address';