SWITCH Incorrect Argument Amount - Can't See Where - ms-access

I'm trying to use the SWITCH function in MS Access and I keep getting told there aren't the right number of arguments. I'm new to the switch function but I understand the syntax.
Switch ( expression1, value1, expression2, value2, ... expression_n, value_n )
Am I doing something in the switch that can't be used, the sub-queries maybe? I've checked my commas, parentheses and that there is an expression and then a return value. Driving me crazy as I'm converting from T-SQL to jet and replacing a CASE statement that worked perfectly fine.
Switch(
(eh.STARTDTE <> sh.STARTDTE AND IsNull(eh.ENDDTE,"") <> IsNull(sh.ENDDTE,"") AND eh.STARTDTE < sh.STARTDTE),
"FAIL - Employment Start Date Before Service Start Date",
(eh.STARTDTE <> sh.STARTDTE AND IsNull(eh.ENDDTE,"") <> IsNull(sh.ENDDTE,"") AND eh.ENDDTE > sh.ENDDTE),
"FAIL - Employment End Date After Service End Date",
(eh.STARTDTE <> sh.STARTDTE AND IsNull(eh.ENDDTE,"") <> IsNull(sh.ENDDTE,"") AND (
SELECT
COUNT(*)
FROM
emphist AS eh2
WHERE
eh2.MEMBNO = sh.MEMBNO
AND eh2.EMPID = sh.EMPID
AND eh2.ENDDTE = DateAdd(dd,-1,eh.STARTDTE)
) = 0),
"FAIL - Previous Employment Period Corrupt or Missing",
(eh.STARTDTE <> sh.STARTDTE AND IsNull(eh.ENDDTE,"") <> IsNull(sh.ENDDTE,"") AND (
SELECT
COUNT(*)
FROM
emphist AS eh2
WHERE
eh2.MEMBNO = sh.MEMBNo
AND eh2.EMPID = sh.EMPID
AND eh2.ENDDTE = DateAdd(dd,-1,eh.STARTDTE)
) = 0),
"FAIL - Previous Employment Period Corrupt or Missing",
(eh.STARTDTE <> sh.STARTDTE AND IsNull(eh.ENDDTE,"") <> IsNull(sh.ENDDTE,"") AND (
SELECT
COUNT(*)
FROM
emphist AS eh2
WHERE
eh2.MEMBNO = sh.MEMBNO
AND eh2.EMPID = sh.EMPID
AND eh2.STARTDTE = DateAdd(dd,1,eh.ENDDTE)
) = 0),
"FAIL - Next Employment Period Corrupt or Missing",
(eh.STARTDTE = sh.STARTDTE AND eh.ENDDTE <> sh.ENDDTE AND (
SELECT
COUNT(*)
FROM
emphist AS eh2
WHERE
eh2.MEMBNO = sh.MEMBNO
AND eh2.EMPID = sh.EMPID
AND eh2.STARTDTE = DateAdd(dd,1,eh.ENDDTE)
) = 0),
"FAIL - Next Employment Period Corrupt or Missing",
(eh.STARTDTE <> sh.STARTDTE AND eh.ENDDTE = sh.ENDDTE AND (
SELECT
COUNT(*)
FROM
emphist AS eh2
WHERE
eh2.MEMBNO = sh.MEMBNO
AND eh2.EMPID = sh.EMPID
AND eh2.ENDDTE = DateAdd(dd,-1,eh.STARTDTE)
) = 0),
"FAIL - Previous Employment Period Corrupt or Missing",
(eh.STARTDTE = sh.STARTDTE AND eh.ENDDTE = sh.ENDDTE AND (
SELECT
COUNT(*)
FROM
emphist AS eh2
WHERE
eh2.MEMBNO = sh.MEMBNO
AND eh2.EMPID = sh.EMPID
) <> 1),
"FAIL - Too Many or No Employment History Records"
) AS "Reason"
UPDATED WITH ANSWERS
Switch(
(eh.STARTDTE <> sh.STARTDTE AND NZ(eh.ENDDTE,"") <> NZ(sh.ENDDTE,"") AND eh.STARTDTE < sh.STARTDTE),
"FAIL - Employment Start Date Before Service Start Date",
(eh.STARTDTE <> sh.STARTDTE AND NZ(eh.ENDDTE,"") <> NZ(sh.ENDDTE,"") AND eh.ENDDTE > sh.ENDDTE),
"FAIL - Employment End Date After Service End Date",
(eh.STARTDTE <> sh.STARTDTE AND NZ(eh.ENDDTE,"") <> NZ(sh.ENDDTE,"") AND (
SELECT
COUNT(*)
FROM
emphist AS eh2
WHERE
eh2.MEMBNO = sh.MEMBNO
AND eh2.EMPID = sh.EMPID
AND eh2.ENDDTE = DateAdd("dd",-1,eh.STARTDTE)
) = 0),
"FAIL - Previous Employment Period Corrupt or Missing",
(eh.STARTDTE <> sh.STARTDTE AND NZ(eh.ENDDTE,"") <> NZ(sh.ENDDTE,"") AND (
SELECT
COUNT(*)
FROM
emphist AS eh2
WHERE
eh2.MEMBNO = sh.MEMBNo
AND eh2.EMPID = sh.EMPID
AND eh2.ENDDTE = DateAdd("dd",-1,eh.STARTDTE)
) = 0),
"FAIL - Previous Employment Period Corrupt or Missing",
(eh.STARTDTE <> sh.STARTDTE AND NZ(eh.ENDDTE,"") <> NZ(sh.ENDDTE,"") AND (
SELECT
COUNT(*)
FROM
emphist AS eh2
WHERE
eh2.MEMBNO = sh.MEMBNO
AND eh2.EMPID = sh.EMPID
AND eh2.STARTDTE = DateAdd("dd",1,eh.ENDDTE)
) = 0),
"FAIL - Next Employment Period Corrupt or Missing",
(eh.STARTDTE = sh.STARTDTE AND eh.ENDDTE <> sh.ENDDTE AND (
SELECT
COUNT(*)
FROM
emphist AS eh2
WHERE
eh2.MEMBNO = sh.MEMBNO
AND eh2.EMPID = sh.EMPID
AND eh2.STARTDTE = DateAdd("dd",1,eh.ENDDTE)
) = 0),
"FAIL - Next Employment Period Corrupt or Missing",
(eh.STARTDTE <> sh.STARTDTE AND eh.ENDDTE = sh.ENDDTE AND (
SELECT
COUNT(*)
FROM
emphist AS eh2
WHERE
eh2.MEMBNO = sh.MEMBNO
AND eh2.EMPID = sh.EMPID
AND eh2.ENDDTE = DateAdd("dd",-1,eh.STARTDTE)
) = 0),
"FAIL - Previous Employment Period Corrupt or Missing",
(eh.STARTDTE = sh.STARTDTE AND eh.ENDDTE = sh.ENDDTE AND (
SELECT
COUNT(*)
FROM
emphist AS eh2
WHERE
eh2.MEMBNO = sh.MEMBNO
AND eh2.EMPID = sh.EMPID
) <> 1),
"FAIL - Too Many or No Employment History Records"
) AS "Reason"

It is not Switch but IsNull that raises the error. So replace all these faulty statements:
IsNull(eh.ENDDTE,"")
with:
Nz(eh.ENDDTE)

OK, there were two issues.
The IsNull function isn't an Access function so I replaced with NZ (same syntax).
For the DateAdd function, I had neglected to surround the datepart argument with speech marks.
The query now happily runs. I'm guessing as the IsNull function wasn't recognised, it appeared as though the argument was missing which I suppose it was. Lesson learnt, incorrect number of arguments could also indicate invalid functions.
NB. I will update my OP with the amended switch statement for others to compare.

Related

Pass Parameters from Excel sheet to Power Query from MS SQL

I am using Excel 365.
I have a Power Query (PQ) that reads off a MS SQL database and I need to set parameters for that query from a table in the same Excel workbook.
I created the parameters in PQ, when I feed them manually the query works
But when I link those parameters to my Excel table get the following error message
Formula.Firewall: Query 'SQL_Query' (step 'Source') references other queries or steps, so it may not directly access a data source. Please rebuild this data combination.
I changed the security settings but it did not work.
I've seen several posts that work but in instances where both data sources come directly only from Excel.
let
Source = Sql.Database("www.com", "db", [Query="
DECLARE #START_DATE AS DATE
DECLARE #END_DATE AS DATE
DECLARE #HYP AS VARCHAR(4)
SET #START_DATE = convert(datetime, " & Start_Dte & ",101)
SET #END_DATE = convert(datetime, " & End_Dte & ",101)
SET #HYP = " & Hyp & " -- = Query_Parameters('ParameterTable', 'Store')
select
case WHEN view1.STORE_NAME <> 'NULL' then view1.STORE_NAME ELSE view2.STORE end as [Store Name],
case WHEN view1.Store_Street <> 'NULL' then view1.Store_Street ELSE view2.Store_Street end as [Store Street],
case WHEN view1.Store_City <> 'NULL' then view1.Store_City ELSE view2.Store_City end as [Store City],
case WHEN view1.Store_State <> 'NULL' then view1.Store_State ELSE view2.[Store State] end as [Store State],
case WHEN view1.Store_zip <> 'NULL' then view1.Store_zip ELSE view2.Store_ZIP end as [Store ZIP],
case WHEN view1.Store <> 'NULL' then view1.Store ELSE view2.Store end as [Store],
case WHEN view1.ZIP_CODE <> 'NULL' then view1.ZIP_CODE ELSE view2.ZIP_CODE end as [ZIP Code],
case WHEN view2.Distance ='[1-5 MILE]' then '[0-5 miles]' ELSE
case WHEN view2.Distance ='[6-10 MILE]' then '[06-10 miles]' ELSE
case WHEN view2.Distance ='[+50 MILE]' then '[50+ miles]' ELSE
case WHEN view1.Distance <> 'NULL' then replace(view1.Distance,'MILES','miles') ELSE replace(view2.Distance,'MILES','miles')
end end
end
end as Distance,
case WHEN view1.Provider <> 'NULL' then view1.Provider ELSE view2.Provider end as Provider,
isnull(view1.Location,'New / Old') as Location,
case WHEN view1.[Lead Source] <> 'NULL' then view1.[Lead Source] ELSE '' end as [Lead Source],
isnull(view1.[YEAR],'') as [Year],
isnull(view1.[month],'') as [Month],
isnull(view1.[Opps],'') as [Opps],
isnull(view1.[Compass_Sales],'') as [Compass Sales],
case WHEN view2.TC_Terrain='Yes' THEN 'Yes' ELSE 'No' END as [TC Terrain]
from (
----Opps & Sales by Provider and by ZIP ---------
SELECT
b.STORE_NAME,
f.STREET1 as Store_Street,
g.CITY as Store_City,
g.STATE as Store_State,
g.ZIP_CODE as Store_ZIP,
b.STORE_STORE_ID as Store,
a.CONTACT_ZIP as ZIP_CODE,
a.DISTANCE as [Distance],
a.media_name as [Provider],
d.Location_name as [Location],
ccategory_name as [Lead Source],
YEAR(TRANSACTION_DATE) as [YEAR],
MONTH(TRANSACTION_DATE) as [MONTH],
SUM(Opportunities) as Opps,
SUM(Compass_Sales) as Compass_Sales
FROM [DB].[fact].[FACT_SALES_TRAFFIC_DETAIL] a
left join db.dim.dim_store b with(nolock) on b.store_id = a.store_id
left join db.dim.dim_lead_category c with(nolock)on a.SOURCE = ccategory
left join db.dim.dim_Location d with(nolock)on a.Location_Key = d.Location_key
left join db.dim.dim_market e with(nolock)on b.market_id = e.market_id
left join DB.DIM.DIM_STORE f with(nolock) on b.STORE_STORE_ID = f.STORE_STORE_ID
left join db.dim.dim_zipcode g with(nolock) on f.ZIP = g.ZIP_CODE
WHERE b.store_Store_id in (#HYP)
and a.Source = 'e'
and TRANSACTION_DATE between #START_DATE and #END_DATE
and a.MEDIA_NAME='TC'
GROUP BY
b.STORE_NAME,
b.STORE_STORE_ID,
d.Location_name,
a.media_Name,
ccategory_name,
YEAR(TRANSACTION_DATE),
MONTH(TRANSACTION_DATE),
CONTACT_ZIP,
a.DISTANCE,
f.STREET1,
g.CITY,
g.ZIP_CODE,
g.STATE) as view1 full outer join (
---True Car ZIP code Terrain------
SELECT
b.STORE_name as STORE,
b.STORE_STORE_ID as [Store],
b.STREET1 as [Store_Street],
c.CITY as [Store_City],
c.STATE as [Store State],
b.ZIP as [Store_ZIP],
INCLUDED_ZIP AS ZIP_CODE,
DISTANCE AS Distance,
'TC' AS Provider,
'Yes' as TC_Terrain
FROM BITESTDB.TIMLINM.TC_ZIPCODE a
left join DB.DIM.DIM_STORE b with(nolock) on b.STORE_id = a.STORE_id
left join db.dim.dim_zipcode c with(nolock) on c.ZIP_CODE= b.ZIP
WHERE STORE_STORE_ID = #HYP AND INCLUDED_ZIP <> '') as view2
on view1.ZIP_CODE = view2.zip_code
ORDER BY Distance
", MultiSubnetFailover=true])
in
Source
The easy way: open Power Query, then File > Options > CURRENT FILE > Privacy > Ignore

mysql equivalent of over function in lower versions

I wrote the following query for a project of mine, it is working perfect, however it is working only for mysql 8 since I am using 'over' function in the query which is not supported by earlier mysql versions. I need to rewrite the query without using over so that it will work for lower versions of mysql as well.
$new_query = "
update ospos_sales_items t
inner join (
select sales_items_id, sum(total_amount) over(order by sales_items_id) sum_rate
from ospos_sales_items t
where payment_type = 'Credit' AND t.customer_id = $pid
) t1 on t1.sales_items_id = t.sales_items_id
set
t.payment_type = 'Cash',
t.total_amount = case when t1.sum_rate >= $amount then t1.sum_rate - $amount else (t.total_amount) end,
t.payment_type = case when t1.sum_rate >= $amount then 'Credit' else 'Cash' end
where t1.sum_rate - (t.total_amount) < $amount AND t.customer_id = $pid
";
I tried the below but it is not working.
$new_query = "
update ospos_sales_items t
inner join (
select sales_items_id, sum(total_amount) sum_rate
from ospos_sales_items t
where payment_type = 'Credit' AND t.customer_id = $pid
) t1 on t1.sales_items_id = t.sales_items_id
set
t.payment_type = 'Cash',
t.total_amount = case when t1.sum_rate >= $amount then t1.sum_rate - $amount else (t.total_amount) end,
t.payment_type = case when t1.sum_rate >= $amount then 'Credit' else 'Cash' end
where t1.sum_rate - (t.total_amount) < $amount AND t.customer_id = $pid
";
UPDATE:
I have simplified the subquery that is using over so that an answer can be easy, please check the fiddle suggest how to write the query without using over function but get the same results as the fiddle
select sum(total_amount) over(order by item_id) sum_rate from mytable order by item_id
select sum(y.total_amount) sum_rate
from mytable x
join mytable y
on y.item_id <= x.item_id
group
by x.item_id
order
by x.item_id

filter rows based on column values in mysql query

This is my mysql query
SELECT a.model_name,
IF( b.officially_released_year = '".$currentyear."',1,0 ) AS release_year,
IF( b.officially_released_month = '".$first_month."' OR b.officially_released_month = '".$second_month."' OR b.officially_released_month = '".$third_month."' OR b.officially_released_month = '".$currentmonth."' ,1,0) AS release_month
FROM ".TBL_CAR_ADD_MODELS." a, ".TBL_CAR_SPEC_GENERAL." b
WHERE a.model_id = b.model_id AND a.model_status = '1'
ORDER BY a.model_created_on DESC
I want to do one more filtering option in this query. I need to get the records based on release_year = 1 & release_year = 1. I have done release_year and release_month columns through IF STATEMENT in MYSQL QUERY
release_year
IF( b.officially_released_year = '".$currentyear."',1,0 ) AS release_year
release_month
IF( b.officially_released_month = '".$first_month."' OR b.officially_released_month = '".$second_month."' OR b.officially_released_month = '".$third_month."' OR b.officially_released_month = '".$currentmonth."' ,1,0) AS release_month
How do I get the records based on these values (release_month = 1 & release_year = 1) in this query? I have tried WHERE release_month = 1 AND release_year = 1 but this one returns unknown column
You could do this:
SELECT
*
FROM
(
SELECT
a.model_name,
a.model_created_on,
IF( b.officially_released_year = '".$currentyear."',1,0 ) AS release_year,
IF( b.officially_released_month = '".$first_month."' OR b.officially_released_month = '".$second_month."' OR b.officially_released_month = '".$third_month."' OR b.officially_released_month = '".$currentmonth."' ,1,0) AS release_month
FROM ".TBL_CAR_ADD_MODELS." a, ".TBL_CAR_SPEC_GENERAL." b
WHERE a.model_id = b.model_id AND a.model_status = '1'
) AS tbl
WHERE tbl.release_year=1 AND release_month=1
ORDER BY tbl.model_created_on DESC

How to write this sql query efficiently in this case

Here I have a query like below and am looping the child id and passing it to this query and getting the appropriate result. Here I have 33 child ids so the result for this query contains 33 rows
DECLARE #DATETIMENOW DATETIME
SET #DATETIMENOW = GETDATE()-7
SELECT COUNT(1) FROM BKA.CHILDEVENTS CHE JOIN BKA.CHILDEVENTPROPERITIES CHEP ON CHEP.EVENTID = CHE.EVENTID
WHERE (CHE.TYPE = 'ACCIDENT' OR (CHE.TYPE = 'BREAK' AND CHEP.PROPERTY = 'SUCCESS' AND CHEP.PROPERTYVALUE = 'FALSE'))
AND CHE.CHILDID = #CHILDID AND CHE.ADDDATE BETWEEN DATEADD(DD, -(DATEPART(DW, #DATETIMENOW-7)-1), #DATETIMENOW-7) AND
DATEADD(DD, 7-(DATEPART(DW, #DATETIMENOW-7)), #DATETIMENOW-7) AS PREVIOUSWEEKACCIDENTS
here instead of looping the childid I have tried using join in this query but am getting the result in a single row. what I am doing wrong in this
DECLARE #DATETIMENOW DATETIME
SET #DATETIMENOW = GETDATE()-7
SELECT COUNT(1) FROM BKA.CHILDEVENTS CHE JOIN BKA.CHILDEVENTPROPERITIES CHEP
ON CHEP.EVENTID = CHE.EVENTID
JOIN BKA.CHILDINFORMATION CHINFO
ON CHE.CHILDID = CHINFO.CHILDID
WHERE (CHE.TYPE = 'ACCIDENT' OR (CHE.TYPE = 'BREAK' AND CHEP.PROPERTY = 'SUCCESS' AND CHEP.PROPERTYVALUE = 'FALSE'))
AND CHE.ADDDATE
BETWEEN DATEADD(DD, -(DATEPART(DW, #DATETIMENOW-7)-1), #DATETIMENOW-7) AND
DATEADD(DD, 7-(DATEPART(DW, #DATETIMENOW-7)), #DATETIMENOW-7)
Any suggestion?
You need to add a GROUP BY clause:
SELECT CHINFO.CHILDID
, COUNT(1)
FROM BKA.CHILDEVENTS CHE
JOIN BKA.CHILDEVENTPROPERITIES CHEP ON CHEP.EVENTID = CHE.EVENTID
JOIN BKA.CHILDINFORMATION CHINFO ON CHE.CHILDID = CHINFO.CHILDID
WHERE ( CHE.TYPE = 'ACCIDENT'
OR ( CHE.TYPE = 'BREAK'
AND CHEP.PROPERTY = 'SUCCESS'
AND CHEP.PROPERTYVALUE = 'FALSE'
)
)
AND CHE.ADDDATE BETWEEN DATEADD(DD,
-( DATEPART(DW, #DATETIMENOW - 7) - 1 ),
#DATETIMENOW - 7)
AND DATEADD(DD,
7 - ( DATEPART(DW, #DATETIMENOW - 7) ),
#DATETIMENOW - 7)
GROUP BY CHINFO.CHILDID
A value in the where will invalidate an outer join
SELECT CHE.CHILDID
, COUNT(1)
FROM BKA.CHILDEVENTS CHE
LEFT JOIN BKA.CHILDEVENTPROPERITIES CHEP
ON CHEP.EVENTID = CHE.EVENTID
AND ( CHE.TYPE = 'ACCIDENT'
OR ( CHE.TYPE = 'BREAK'
AND CHEP.PROPERTY = 'SUCCESS'
AND CHEP.PROPERTYVALUE = 'FALSE'
)
)
AND CHE.ADDDATE BETWEEN DATEADD(DD,
-( DATEPART(DW, #DATETIMENOW - 7) - 1 ),
#DATETIMENOW - 7)
AND DATEADD(DD,
7 - ( DATEPART(DW, #DATETIMENOW - 7) ),
#DATETIMENOW - 7)
GROUP BY CHE.CHILDID
DECLARE #DATETIMENOW DATETIME
SET #DATETIMENOW = GETDATE()
SELECT B.WEEK FROM BKA.CHILDINFORMATION CI LEFT OUTER JOIN
(SELECT Distinct CHINFO.CHILDID,COUNT(*) as week FROM BKA.CHILDINFORMATION CHINFO
JOIN BKA.CHILDEVENTS CHE
ON CHE.CHILDID = CHINFO.CHILDID
JOIN BKA.CHILDEVENTPROPERITIES CHEP
ON CHE.EVENTID = CHEP.EVENTID
WHERE
(CHE.TYPE = 'ACCIDENT' OR (CHE.TYPE = 'POTTYBREAK' AND CHEP.PROPERTY = 'SUCCESS'
AND CHEP.PROPERTYVALUE = 'FALSE'))
AND
CHE.ADDDATE
BETWEEN DATEADD(DD, -(DATEPART(DW, #DATETIMENOW-14)-1), #DATETIMENOW-14) AND
DATEADD(DD, 7-(DATEPART(DW, #DATETIMENOW-14)), #DATETIMENOW-14) group by CHINFO.CHILDID) b
on CI.ChildID = b.ChildID

Error in last line near end

While i Execute a stored procedure I don't know why showing error in last line...
I can't find any error with it
The error is telling
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '' at
line 60
_
CREATE PROCEDURE `PartyBalanceViewByLedgerId`
(
p_ledgerId varchar(50),
p_crOrDr varchar(50),
p_branchId varchar(50)
)
BEGIN
IF (p_crOrDr='Dr')
THEN
SELECT
TEMP.voucherNo +'_'+ TEMP.voucherType AS ID,
TEMP.voucherType,
CASE WHEN (TEMP.voucherType = 'Receipt Voucher')
THEN
(SELECT receiptMasterId FROM tbl_ReceiptMaster
WHERE (receiptMasterId = TEMP.voucherNo))
ELSE
(SELECT purchaseMasterId FROM tbl_PurchaseMaster
WHERE (purchaseMasterId = TEMP.voucherNo))
END AS voucherNo,
CAST(CAST(TEMP.balance AS DECIMAL(24,2)) AS char(27))
AS amount
FROM(SELECT
A.voucherNo,
A.voucherType,
(SUM(ifnull(A.credit, 0)) - SUM(ifnull(A.debit, 0)))
AS balance
FROM tbl_PartyBalance AS A
WHERE (A.voucherType = 'Purchase Invoice'
OR A.voucherType = 'Receipt Voucher')
AND A.ledgerId=p_ledgerId
AND A.branchId=p_branchId
AND A.optional='False'
GROUP BY A.voucherNo,A.voucherType
)AS TEMP
WHERE TEMP.Balance>0 ;
ELSE
SELECT
TEMP.voucherNo +'_'+ TEMP.voucherType ID,
TEMP.voucherType,
CASE WHEN TEMP.voucherType = 'Payment Voucher' THEN
(SELECT paymentMasterId FROM tbl_PaymentMaster WHERE (paymentMasterId = TEMP.voucherNo))
ELSE
(SELECT salesInvoiceNo FROM tbl_SalesMaster WHERE (salesMasterId = TEMP.voucherNo)) END AS voucherNo,
CAST(CAST(TEMP.balance AS DECIMAL(24,2))AS char(27)) AS amount
FROM(
SELECT
A.voucherNo,
A.voucherType,
(SUM(ifnull(A.debit, 0)) - SUM(ifnull(A.credit,0))) AS balance
FROM tbl_PartyBalance AS A
WHERE (A.voucherType = 'Sales Invoice' OR A.voucherType = 'Payment Voucher'OR A.voucherType = 'Job Invoice') AND A.ledgerId=p_ledgerId AND A.branchId=p_branchId AND A.optional='False'
GROUP BY A.voucherNo,A.voucherType
)AS TEMP
WHERE TEMP.Balance > 0 ;
END
try with this i have update some changes END IF and ; etc
CREATE PROCEDURE `PartyBalanceViewByLedgerId`
(
p_ledgerId varchar(50),
p_crOrDr varchar(50),
p_branchId varchar(50)
)
BEGIN
IF (p_crOrDr='Dr')
SELECT
TEMP.voucherNo +'_'+ TEMP.voucherType AS ID,
TEMP.voucherType,
CASE WHEN (TEMP.voucherType = 'Receipt Voucher')
THEN
(SELECT receiptMasterId FROM tbl_ReceiptMaster
WHERE (receiptMasterId = TEMP.voucherNo))
ELSE
(SELECT purchaseMasterId FROM tbl_PurchaseMaster
WHERE (purchaseMasterId = TEMP.voucherNo))
END AS voucherNo,
CAST(CAST(TEMP.balance AS DECIMAL(24,2)) AS char(27))
AS amount
FROM(SELECT
A.voucherNo,
A.voucherType,
(SUM(ifnull(A.credit, 0)) - SUM(ifnull(A.debit, 0)))
AS balance
FROM tbl_PartyBalance AS A
WHERE (A.voucherType = 'Purchase Invoice'
OR A.voucherType = 'Receipt Voucher')
AND A.ledgerId=p_ledgerId
AND A.branchId=p_branchId
AND A.optional='False'
GROUP BY A.voucherNo,A.voucherType
)AS TEMP
WHERE TEMP.Balance>0;
ELSE
SELECT
TEMP.voucherNo +'_'+ TEMP.voucherType ID,
TEMP.voucherType,
CASE WHEN TEMP.voucherType = 'Payment Voucher' THEN
(SELECT paymentMasterId FROM tbl_PaymentMaster WHERE (paymentMasterId = TEMP.voucherNo))
ELSE
(SELECT salesInvoiceNo FROM tbl_SalesMaster WHERE (salesMasterId = TEMP.voucherNo)) END AS voucherNo,
CAST(CAST(TEMP.balance AS DECIMAL(24,2))AS char(27)) AS amount
FROM(
SELECT
A.voucherNo,
A.voucherType,
(SUM(ifnull(A.debit, 0)) - SUM(ifnull(A.credit,0))) AS balance
FROM tbl_PartyBalance AS A
WHERE (A.voucherType = 'Sales Invoice' OR A.voucherType = 'Payment Voucher'OR A.voucherType = 'Job Invoice') AND A.ledgerId=p_ledgerId AND A.branchId=p_branchId AND A.optional='False'
GROUP BY A.voucherNo,A.voucherType
)AS TEMP
WHERE TEMP.Balance > 0 ;
END IF;
END;
Are you using a DELIMITER? If not pl go through this description:
Delimiters in MySQL
I hope this will solve your problem.
Remove the semicolon before ELSE in :
GROUP BY A.voucherNo,A.voucherType
)AS TEMP
WHERE TEMP.Balance>0
ELSE
SELECT
TEMP.voucherNo +'_'+ TEMP.voucherType ID,
TEMP.voucherType,