How To append SQL Query in where Clause of Stored Procedure???
IN Stored Prcedure I have defined 1 parameter #ViewType, which accepts 1 of 3 value 'Uploaded','Not Uploaded' and 'ALL'. According to the value of ViewType the condition is applied in where caluse (FileType in below query).
Some Select Query
-> if(#ViewType = 'Uploaded')
WHERE ContractNumber=1234 AND DocumentType='VendorContract' AND ID=54 AND FileType IS NOT NULL
-> if(#ViewType = 'Not Uploaded')
WHERE ContractNumber=1234 AND DocumentType='VendorContract' AND ID=54 AND FileType IS NULL
->
if(#ViewType = 'ALL')
WHERE ContractNumber=1234 AND DocumentType='VendorContract' AND ID=54
You may refactor this logic as a single WHERE clause:
WHERE
(
(#ViewType = 'Uploaded' AND FileType IS NOT NULL) OR
(#ViewType = 'Not Uploaded' AND FileType IS NULL) OR
#ViewType = 'ALL'
) AND
ContractNumber = 1234 AND DocumentType = 'VendorContract' AND ID = 54
Related
I'm trying to run a somewhat complex sql statement using the 'group_concat' function for statistics.
When I run the sql statement using navicat, the correct result is returned.But when I use mybatis debug this sql in idea, it returns null value.
I use HashMapHashMap<String,Long> to receive the result set executed by mybatis,
I guess the result set returned by mybatis executing sql is wrongly encapsulated.
But I tried for a long time and couldn't find a solution.
I can't post pictures here,I display the results in a table.
cha
hege
lianghao
youxiu
1
1
2
1
Receive declaration at the mapper layer
HashMap<String, Long> getOption(#Param("column") String column);
Here is the sql code:
<select id="getOption" resultType="java.util.HashMap">
SELECT GROUP_CONCAT(form.cha SEPARATOR '') cha,
GROUP_CONCAT(form.hege SEPARATOR '') hege,
GROUP_CONCAT(form.lianghao SEPARATOR '') lianghao,
GROUP_CONCAT(form.youxiu SEPARATOR '') youxiu
FROM (
SELECT
( CASE WHEN #{column} = '0' THEN count(*) ELSE NULL END) AS 'cha',
( CASE WHEN #{column} = '1' THEN count(*) ELSE NULL END ) AS 'hege',
( CASE WHEN #{column} = '2' THEN count(*) ELSE NULL END ) AS 'lianghao',
( CASE WHEN #{column} = '3' THEN count(*) ELSE NULL END ) AS 'youxiu'
FROM corp_feedback_student
GROUP BY #{column}
) form
</select>
Thanks everyone for helping me!!
I have a stored procedure for search screen where I have 5 different filters.
Users can leave the filters empty or have one filter and click on search button.
All the data is coming from database, I am trying to create stored procedure which can exclude columns in where clause if the parameter value is empty string.
#Id as nvarchar(256) = 1
#blnIsinProgress bit = 0
#strStatus varchar(20) = ''
#strName varchar(50) = ''
#strConfirmationNumber varchar(50) = 123
#dtmSubmittedFrom Date = '12/31/9999'
#dtmSubmittedTo Date = '12/31/9999'
as
BEGIN
SELECT *
FROM tblTable
WHERE
(#Id IS NULL OR lngID = #Id) AND
(#blnIsinProgress IS NULL OR blnIsinProgress = #blnIsinProgress) AND
(#strStatus = '' OR strStatus = #strStatus) AND
(#strName= '' OR strName= #strName) AND
(#strConfirmationNumber = '' or #strConfirmationNumber = #strConfirmationNumber )
End
But
Execute spManage 1,0,'','',123
will give me all the results
The problem is this line:
(#strConfirmationNumber = '' or #strConfirmationNumber = #strConfirmationNumber )
The second condition is always true. You have an extra #. So, try this:
(#strConfirmationNumber = '' or #strConfirmationNumber = strConfirmationNumber )
UPDATE FD DCS
SET DCS.F_DISTANCE =
(SELECT FD.F_DISTANCE
FROM TMP FD
WHERE FD.DATE_SID = DCS.DATE_SID
AND FD.SID = DCS.SID
AND FD.DA_SID = DCS.DASID
AND FD.AA_SID = DCS.AA
AND FD.F_DISTANCE IS NOT NULL
)
WHERE DCS.BATCH_ID=BATCH_ID;
The inner SQl query is updating all the satisfied values to the FD table's column F_distance but where subquery didn't match it is updating the FD table F_Distance values to null. I don't want to update the values to null.
please suggest what to do.
In ORACLE you can use NVL
UPDATE FD DCS
SET DCS.F_DISTANCE = NVL(
(SELECT FD.F_DISTANCE
FROM TMP FD
WHERE FD.DATE_SID = DCS.DATE_SID
AND FD.SID = DCS.SID
AND FD.DA_SID = DCS.DASID
AND FD.AA_SID = DCS.AA
AND FD.F_DISTANCE IS NOT NULL
), DCS.F_DISTANCE)
WHERE DCS.BATCH_ID=BATCH_ID;
In this case if the result is null the NVL return 0 (you cna change with the value you prefer)
I have a query like this:
SELECT old.NPP,
old.NAMA,
old.JOB,
pgh.status
FROM a old
LEFT JOIN b pgh ON old.person_id = pgh.person_id
AND pgh.periode = 2015
AND pgh.LAST_STATUS = 1
WHERE old.UNIT_BESARAN = 'DIVISI TEKNOLOGI INFORMASI'
AND pgh.status = #status
ORDER BY pgh.status
i want my query can get every value from pgh status, like when i type status 5, the query must to return status is 5, but when i input null, the query will return data from database when the status is null.
how query will be set?
You can write the condition this way:
where old.UNIT_BESARAN = 'DIVISI TEKNOLOGI INFORMASI' and
(pgh.status = #status or pgh.status is null and #status is null)
If i understand your situation correctly, i think there is empty strings, so to make sure the condition is correct. just use ISNULL
SELECT old.NPP,
old.NAMA,
old.JOB,
pgh.status
FROM a old
LEFT JOIN b pgh ON old.person_id = pgh.person_id
AND pgh.periode = 2015
AND pgh.LAST_STATUS = 1
WHERE old.UNIT_BESARAN = 'DIVISI TEKNOLOGI INFORMASI'
AND pgh.status = ISNULL(#status,'')
ORDER BY pgh.status
I have the following full query, and it returns nothing.
#companies.where("(companies.status = 'active' AND (companies.status_override = '' OR companies.status_override = NULL)) OR companies.status_override = 'active'")
I expect that the first part (companies.status = 'active' AND (companies.status_override = '' OR companies.status_override = NULL)) would return all items that have status set to 'active' and status_override set to nothing.
Without knowing the DB structure, the = NULL is a suspect. Try IS NULL instead.
Arithmetic comparison with NULL doesn't behave as you might expect; more details can be found in the Mysql Reference
The most likely explanation is that there aren't any rows that satisfy the predicates in the WHERE clause. Your where clause is of the form:
( a AND ( b OR c ) ) OR d
where
a = companies.status = 'active'
b = companies.status_override = ''
c = companies.status_override = NULL
d = companies.status_override = 'active'
So, basically, we know that there aren't any rows in the table that satisfy condition d.
And we know there aren't any rows that satisfy both conditions a and b.
Condition c is impossible; it will never be satisfied.
To test whether an expression contains a NULL, use expr IS NULL. i.e.
companies.status_override IS NULL
If you aren't concerned with an index range scan operation on the status_override column (it's likely that you aren't) you can simplify a bit using the IFNULL() function...
(companies.status = 'active' AND IFNULL(companies.status_override,'') = '') OR companies.status_override = 'active'
or using the NULLIF() function
(companies.status = 'active' AND NULLIF(companies.status_override,'') IS NULL) OR companies.status_override = 'active'