Writing to an excel sheet - sql

I am trying to add variable values in WinAutomation into a spreadsheet by column header for every row that doesn't have a value. I've tried two different methods (insert into and update). I am getting the following error: "Runtime Error: Error in SQL Statement: Syntax error in INSERT INTO statement." Does anyone see something that I did wrong here that is causing said error?
INSERT INTO [Sheet1$]
[SAP Employee ID] VALUES(%SapEmployeeNumber%)
WHERE RowNumber IS null
UPDATE [Sheet1$] SET
([SAP Employee ID] = %SapEmployeeNumber%
,[Concur Travel] = %CreditOrOutofPocket%
,[Participant Name] = %ParticipantName%
,[Participant Birth Date] = %ParticipantBirth%
,[Work Location] = %ParticipantWorkLocation%
,[Street Address] = %ParticipantStreetAddress%
,[City] = %ParticipantCity%
,[State] = %ParticipantState%
,[Zip] = %ParticipantZip%
,[Participant Email] = %ParticipantEmail%
,[Contact Phone] = %ParticipantContactPhone%
,[Credit Card Profile] = %ParticipantCreditProfile%
,[Credit Limit] = %ParticipantCreditLimit% )
WHERE
[SAP Employee ID] IS NULL;

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.

"The multi-part identifier could not be bound" for update statement from 'inner' set to 'update' set

Error
'The multi-part identifier "ar_update.Id" could not be bound.'
as line denoted in sql script.
I also tried other columns for the errant join to try to prompt different errors and gives same error results.
Why/How to qualify the 'inner' processing set back to the 'update' set?
update ar_update
set ar_update.[GLEntry_Amount] = ar_sum.[GLEntry_Amount_sum]
from [dbo].[ASL_Result_VGO] as ar_update
join (select ar.[Id] as [Id]
,ar.[Company Id] as [Company Id]
,ar.[Schedule Name] as [Schedule Name]
,ar.[Line No_] as [Line No_]
,ar.[Row No_] as [Row No_]
,ar.[Posting Date] as [Posting Date]
,ar.[Business Centre Code] as [Business Centre Code]
,ar.[Site Locations Code] as [Site Locations Code]
,GLEntry_Amount_sum =
(select sum(are.[GLEntry_Amount])
from [dbo].[ASL_Result_VGO] as are
where are.[Company Id] = ar.[Company Id] and
are.[Schedule Name] = ar.[Schedule Name] and
are.[Posting Date] = ar.[Posting Date] and
are.[Business Centre Code] = ar.[Business Centre Code] and
are.[Site Locations Code] = ar.[Site Locations Code] and
are.[Row No_] in ( select * from dbo.udf_range_expand_VGO(ar.[Totaling]) )
)
from [dbo].[ASL_Result_VGO] as ar
where ar.[Id] = ar_update.[Id] -- The multi-part identifier "ar_update.Id" could not be bound.
) ar_sum on ar_update.[Id] = ar_sum.[Id]
where ar_update.[Totaling Type] = 2 and -- formulas
len(trim(ar_update.[Totaling])) > 0 and
charindex('..', ar_update.[Totaling]) > 0 -- P ranges
this happened because on where ar.[Id] = ar_update.[Id] because ar_update.[Id] is not a value it is a multivalued type, it's like you were comparing a list with an integer, try this
update ar_update
set ar_update.[GLEntry_Amount] = ar_sum.[GLEntry_Amount_sum]
from [dbo].[ASL_Result_VGO] as ar_update
join (select ar.[Id] as [Id]
,ar.[Company Id] as [Company Id]
,ar.[Schedule Name] as [Schedule Name]
,ar.[Line No_] as [Line No_]
,ar.[Row No_] as [Row No_]
,ar.[Posting Date] as [Posting Date]
,ar.[Business Centre Code] as [Business Centre Code]
,ar.[Site Locations Code] as [Site Locations Code]
,GLEntry_Amount_sum = (select sum(are.[GLEntry_Amount])
from [dbo].[ASL_Result_VGO] as are
where are.[Company Id] = ar.[Company Id] and
are.[Schedule Name] = ar.[Schedule Name] and
are.[Posting Date] = ar.[Posting Date] and
are.[Business Centre Code] = ar.[Business Centre Code] and
are.[Site Locations Code] = ar.[Site Locations Code] and
are.[Row No_] in ( select * from dbo.udf_range_expand_VGO(ar.[Totaling]) )
)
from [dbo].[ASL_Result_VGO] as ar
) ar_sum on ar_update.[Id] = ar_sum.[Id]
where ar_update.[Totaling Type] = 2 and -- formulas
len(trim(ar_update.[Totaling])) > 0 and
charindex('..', ar_update.[Totaling]) > 0

Only Update Table if Field is NULL

I have this query which is working OK
UPDATE Master
SET Closed = Date(),
Status = 'Closed',
[Closure Reason] = 'Not on Extract'
WHERE NOT EXISTS ( SELECT 1
FROM Extract
WHERE Master.Case = Extract.Case
);
I need to amend it to add another criteria, ie ONLY do the update if the value of Master.[Closure Reason] is NULL
But this gives a syntax error:
UPDATE Master SET Closed = Date(), Status = 'Closed', [Closure Reason] = 'Not on Extract'
WHERE [Closure Reason] = "" AND
WHERE NOT EXISTS (SELECT 1
FROM Extract
WHERE Master.Case = Extract.Case
);
I've tried using IS NULL rather than = "" to no avail.
What is wrong with it please?
UPDATE Master SET Closed = Date(), Status = 'Closed', [Closure Reason] = 'Not on Extract'
WHERE [Closure Reason] IS NULL
AND NOT EXISTS (SELECT 1
FROM Extract
WHERE Master.Case = Extract.Case
);
You can't add multiple WHERE clauses, you need to use the AND. If your requirement is "ONLY do the update if the value of Master.[Closure Reason] is NULL", then check for NULL, not for an empty string.
You need to remove the second where before not exists.
UPDATE Master SET Closed = Date(), Status = 'Closed', [Closure Reason] = 'Not on Extract'
WHERE [Closure Reason] = "" AND
NOT EXISTS (SELECT 1
FROM Extract
WHERE Master.Case = Extract.Case
);
You need to add a AND conditional operator instead of having two WHERE clauses like
WHERE NOT EXISTS (SELECT 1
FROM Extract
WHERE Master.Case = Extract.Case
)
AND [Closure Reason] IS NULL;

putting if else clause in where condition sql

I am a beginner and first time in sql procedure. If the user doesnt give any value to the parameter, it will return all the item. But I am trying to adding more where clause based on user input. I know there is if-else statement in sql procedure, but I have no idea if is it possible to append more condition.
Here is the statement
#FirstName varchar (50)
AS
SELECT distinct [user_identity], [first name],
[last name], email, Name, orderdate,RenewalCount, RenewalDate
FROM
db.[dbo].Users,
db.[dbo].[Order],
db.[dbo].[Subscription],
db.[dbo].[Products],
db.[dbo].[Order_Details]
WHERE
db.[dbo].Users.user_identity = db.[dbo].Subscription.userid AND
db.[dbo].Users.user_identity = db.[dbo].[Order].userid AND
db.[dbo].[Order].Orderid = db.[dbo].[Order_Details].OrderId AND
db.[dbo].Products.ProductId = db.[dbo].[Order_Details].Productid AND
db.[dbo].[Order_Details].OD_Id = db.[dbo].[Subscription].OD_Id
//if #firstname is not null and not empty
//append where clause condition
//AND db.[dbo].Users.[first name] = #FirstName
Thanks in advance!
Your where condition should be like this:
WHERE
db.[dbo].Users.user_identity = db.[dbo].Subscription.userid AND
db.[dbo].Users.user_identity = db.[dbo].[Order].userid AND
db.[dbo].[Order].Orderid = db.[dbo].[Order_Details].OrderId AND
db.[dbo].Products.ProductId = db.[dbo].[Order_Details].Productid AND
db.[dbo].[Order_Details].OD_Id = db.[dbo].[Subscription].OD_Id AND
(#FirstName IS NULL OR #FirstName = '' OR db.[dbo].Users.[first name] = #FirstName)
append below condition in your where condition
where #FirstName=case when isnull(#firstname,'')<>'' then db.[dbo].Users.[first name] else #FirstName end

SQL Server Update Statement

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