Omit a condition in SQL dynamically - sql

I am developing a website with Asp.net. I have a problem in the following code!
I want to use the DataSqlSource and a attached it to Gridview. I also have a drop down list for categorizing in that page. What i want is, to categorize the Gridview by drop down list. In this case in one condition, a condition must be omitted from the query(Step3_AddArticleType.Status = #Status). For example when the drop down list value is 'All', the mentioned condition must not be counting.
Can any body help me?
I don't know the structure of using that. Here is the code in my DataSqlSource:
SELECT Step3_AddArticleType.ArticleType, Step3_AddArticleType.JournalName, Step5_AddTitle.FullTitle, Step3_AddArticleType.Status, Step3_AddArticleType.ArticleNum,
Step5_AddTitle.ArticleID, Step3_AddArticleType.ID, Step3_AddArticleType.Date, CONVERT(VARCHAR(10), Step3_AddArticleType.Date, 103) AS SubmissionDate
FROM Step3_AddArticleType
INNER JOIN Step5_AddTitle ON Step3_AddArticleType.ArticleID = Step5_AddTitle.ArticleID
WHERE (Step3_AddArticleType.CheckFinish = '0')
AND (Step3_AddArticleType.JournalName = #JournalName)
CASE
WHEN #Status<>'All' THEN
AND (Step3_AddArticleType.Status = #Status)
END
Thank you so much

Your WHERE condition should be like below. Put OR between checking for all and a specific value:
WHERE (Step3_AddArticleType.CheckFinish = '0') AND (Step3_AddArticleType.JournalName = #JournalName)
AND (#Status = 'All' OR Step3_AddArticleType.Status = #Status)

Related

How to write an Open SQL statement with substring in the JOIN ON condition? [duplicate]

I have the following select statement in ABAP:
SELECT munic~mandt VREFER BIS AB ZZELECDATE ZZCERTDATE CONSYEAR ZDIMO ZZONE_M ZZONE_T USAGE_M USAGE_T M2MC M2MT M2RET EXEMPTMCMT EXEMPRET CHARGEMCMT
INTO corresponding fields of table GT_INSTMUNIC_F
FROM ZCI00_INSTMUNIC AS MUNIC
INNER JOIN EVER AS EV on
MUNIC~POD = EV~VREFER(9).
"where EV~BSTATUS = '14' or EV~BSTATUS = '32'.
My problem with the above statement is that does not recognize the substring/offset operation on the 'ON' clause. If i remove the '(9) then
it recognizes the field, otherwise it gives error:
Field ev~refer is unknown. It is neither in one of the specified tables
nor defined by a "DATA" statement. I have also tried doing something similar in the 'Where' clause, receiving a similar error:
LOOP AT gt_instmunic.
clear wa_gt_instmunic_f.
wa_gt_instmunic_f-mandt = gt_instmunic-mandt.
wa_gt_instmunic_f-bis = gt_instmunic-bis.
wa_gt_instmunic_f-ab = gt_instmunic-ab.
wa_gt_instmunic_f-zzelecdate = gt_instmunic-zzelecdate.
wa_gt_instmunic_f-ZZCERTDATE = gt_instmunic-ZZCERTDATE.
wa_gt_instmunic_f-CONSYEAR = gt_instmunic-CONSYEAR.
wa_gt_instmunic_f-ZDIMO = gt_instmunic-ZDIMO.
wa_gt_instmunic_f-ZZONE_M = gt_instmunic-ZZONE_M.
wa_gt_instmunic_f-ZZONE_T = gt_instmunic-ZZONE_T.
wa_gt_instmunic_f-USAGE_M = gt_instmunic-USAGE_M.
wa_gt_instmunic_f-USAGE_T = gt_instmunic-USAGE_T.
temp_pod = gt_instmunic-pod.
SELECT vrefer
FROM ever
INTO wa_gt_instmunic_f-vrefer
WHERE ( vrefer(9) LIKE temp_pod ). " PROBLEM WITH SUBSTRING
"AND ( BSTATUS = '14' OR BSTATUS = '32' ).
ENDSELECT.
WRITE: / sy-dbcnt.
WRITE: / 'wa is: ', wa_gt_instmunic_f.
WRITE: / 'wa-ever is: ', wa_gt_instmunic_f-vrefer.
APPEND wa_gt_instmunic_f TO gt_instmunic_f.
WRITE: / wa_gt_instmunic_f-vrefer.
ENDLOOP.
itab_size = lines( gt_instmunic_f ).
WRITE: / 'Internal table populated with', itab_size, ' lines'.
The basic task i want to implement is to modify a specific field on one table,
pulling values from another. They have a common field ( pod = vrefer(9) ). Thanks in advance for your time.
If you are on a late enough NetWeaver version, it works on 7.51, you can use the OpenSQL function LEFT or SUBSTRING. Your query would look something like:
SELECT munic~mandt VREFER BIS AB ZZELECDATE ZZCERTDATE CONSYEAR ZDIMO ZZONE_M ZZONE_T USAGE_M USAGE_T M2MC M2MT M2RET EXEMPTMCMT EXEMPRET CHARGEMCMT
FROM ZCI00_INSTMUNIC AS MUNIC
INNER JOIN ever AS ev
ON MUNIC~POD EQ LEFT( EV~VREFER, 9 )
INTO corresponding fields of table GT_INSTMUNIC_F.
Note that the INTO clause needs to move to the end of the command as well.
field(9) is a subset operation that is processed by the ABAP environment and can not be translated into a database-level SQL statement (at least not at the moment, but I'd be surprised if it ever will be). Your best bet is either to select the datasets separately and merge them manually (if both are approximately equally large) or pre-select one and use a FAE/IN clause.
They have a common field ( pod = vrefer(9) )
This is a wrong assumption, because they both are not fields, but a field an other thing.
If you really need to do that task through SQL, I'll suggest you to check native SQL sentences like SUBSTRING and check if you can manage to use them within an EXEC_SQL or (better) the CL_SQL* classes.

exclude items based on field value

I've created a view using the below script but one of the fields is pulling back information that I don't need. Within the 'IsServer' column, items that are servers are marked with a 1 and items that aren't servers are marked with a 0. Is there a way of creating the view and exclude items that are servers that are marked as 1 in the IsServer column? I'm unsure how to build this into the create view script.
CREATE VIEW [dbo].[vw_Cherwell_Machines] AS
SELECT dbo.rptComputers.HostName,
dbo.rptComputers.Vendor,
dbo.tblComputer.IsVirtual,
dbo.rptComputers.ProcessorCount,
dbo.tblComputer.IsServer,
dbo.rptComputers.BiosSerialNumber,
dbo.rptComputers.PhysicalMemory,
dbo.rptComputers.ProcessorType,
dbo.rptComputers.ProcessorSpeed,
dbo.rptComputers.MostFrequentUser,
dbo.tblComputer.Domain,
dbo.rptComputers.ClientVersion,
dbo.rptComputers.ClientInstallDate,
dbo.rptComputers.ClientConfigurationName,
dbo.rptComputers.BiosVersion,
dbo.rptComputers.BiosDate,
dbo.rptComputers.Manufacturer,
dbo.rptComputers.Model,
dbo.rptComputers.IsPortable,
dbo.rptComputers.OperatingSystem,
dbo.tblComputer.OSServicePack,
dbo.rptComputers.ComputerStatusCode,
dbo.rptComputers.IPAddress AS 'MultipleIPAddress',
CASE WHEN PATINDEX ('%[, ]%',dbo.rptComputers.IPAddress) > 0 THEN
left(dbo.rptComputers.IPAddress,(PATINDEX('%[, ]%',dbo.rptComputers.IPAddress))-1)
ELSE dbo.rptComputers.IPAddress
END AS 'IPAddress',
dbo.rptComputers.LastScanDate
FROM dbo.rptComputers INNER JOIN dbo.tblComputer ON dbo.rptComputers.CID = dbo.tblComputer.CID
AND dbo.rptComputers.ComputerID = dbo.tblComputer.ComputerID
GROUP BY dbo.rptComputers.HostName,
dbo.tblComputer.IsVirtual,
dbo.rptComputers.Vendor,
dbo.rptComputers.ProcessorCount,
dbo.rptComputers.BiosSerialNumber,
dbo.tblComputer.IsServer,
dbo.rptComputers.PhysicalMemory,
dbo.rptComputers.ProcessorType,
dbo.rptComputers.ProcessorSpeed,
dbo.rptComputers.MostFrequentUser,
dbo.tblComputer.Domain,
dbo.rptComputers.ClientVersion,
dbo.rptComputers.ClientInstallDate,
dbo.rptComputers.ClientConfigurationName,
dbo.rptComputers.BiosVersion,
dbo.rptComputers.BiosDate,
dbo.rptComputers.Manufacturer,
dbo.rptComputers.Model,
dbo.rptComputers.IsPortable,
dbo.rptComputers.OperatingSystem,
dbo.tblComputer.OSServicePack,
dbo.rptComputers.ComputerStatusCode,
dbo.rptComputers.IPAddress,
dbo.rptComputers.LastScanDate
You need a filtered view; i.e., a view with a WHERE clause. Something like this (I marked off in comments the begin/end of my addition to your code):
CREATE VIEW [dbo].[vw_Cherwell_Machines] AS
SELECT dbo.rptComputers.HostName,
dbo.rptComputers.Vendor,
dbo.tblComputer.IsVirtual,
dbo.rptComputers.ProcessorCount,
dbo.tblComputer.IsServer,
dbo.rptComputers.BiosSerialNumber,
dbo.rptComputers.PhysicalMemory,
dbo.rptComputers.ProcessorType,
dbo.rptComputers.ProcessorSpeed,
dbo.rptComputers.MostFrequentUser,
dbo.tblComputer.Domain,
dbo.rptComputers.ClientVersion,
dbo.rptComputers.ClientInstallDate,
dbo.rptComputers.ClientConfigurationName,
dbo.rptComputers.BiosVersion,
dbo.rptComputers.BiosDate,
dbo.rptComputers.Manufacturer,
dbo.rptComputers.Model,
dbo.rptComputers.IsPortable,
dbo.rptComputers.OperatingSystem,
dbo.tblComputer.OSServicePack,
dbo.rptComputers.ComputerStatusCode,
dbo.rptComputers.IPAddress AS 'MultipleIPAddress',
CASE WHEN PATINDEX ('%[, ]%',dbo.rptComputers.IPAddress) > 0 THEN
left(dbo.rptComputers.IPAddress,(PATINDEX('%[, ]%',dbo.rptComputers.IPAddress))-1)
ELSE dbo.rptComputers.IPAddress
END AS 'IPAddress',
dbo.rptComputers.LastScanDate
FROM dbo.rptComputers INNER JOIN dbo.tblComputer ON dbo.rptComputers.CID = dbo.tblComputer.CID
AND dbo.rptComputers.ComputerID = dbo.tblComputer.ComputerID
/*begin change*/
WHERE tblComputer.IsServer = 0
/*end change*/
GROUP BY dbo.rptComputers.HostName,
dbo.tblComputer.IsVirtual,
dbo.rptComputers.Vendor,
dbo.rptComputers.ProcessorCount,
dbo.rptComputers.BiosSerialNumber,
dbo.tblComputer.IsServer,
dbo.rptComputers.PhysicalMemory,
dbo.rptComputers.ProcessorType,
dbo.rptComputers.ProcessorSpeed,
dbo.rptComputers.MostFrequentUser,
dbo.tblComputer.Domain,
dbo.rptComputers.ClientVersion,
dbo.rptComputers.ClientInstallDate,
dbo.rptComputers.ClientConfigurationName,
dbo.rptComputers.BiosVersion,
dbo.rptComputers.BiosDate,
dbo.rptComputers.Manufacturer,
dbo.rptComputers.Model,
dbo.rptComputers.IsPortable,
dbo.rptComputers.OperatingSystem,
dbo.tblComputer.OSServicePack,
dbo.rptComputers.ComputerStatusCode,
dbo.rptComputers.IPAddress,
dbo.rptComputers.LastScanDate
Documentation: https://learn.microsoft.com/en-us/sql/relational-databases/indexes/create-filtered-indexes?view=sql-server-ver15 Be sure to see the "limitations" section there, in case there's something in your environment which will not work here.

How to give change working of having function dynamicaly on executing an sql statement?

I'm having a Sql code like as follows
Select a.ItemCode, a.ItemDesc
From fn_BOM_Material_Master('A', #AsOnDate, #RptDate, #BranchID, #CompID)a
Left Outer Join fn_INV_AsOnDate_Stock(#StockDate, #AsOnDate, #RptDate, #BranchID, #CompID, #Finyear)b
On a.ItemCode=b.ItemCode and b.WarehouseCode<>'WAP'
and a.BranchID=b.BranchID and a.CompID=b.COmpID
Where a.ItemNatureCode = 'F' and a.BranchID = #BranchID and a.CompID = #CompID
Group by a.ItemCode, a.ItemDesc
Having sum(b.CBQty)<=0
Here the problem is that im passing an "#ShowZeroStock" value as as bit if the "#ShowZeroStock" value is '1' then Having should not be validated or (i.e: All values from the table should be returned including zero)
So How to change the query based on passed bit value "#ShowZeroStock"
I can Use "If else " condition at the top and remove having in else part, but for a lengthy query i can't do the same.
Is this the logic you want?
Having sum(b.CBQty) <= 0 or #ShowZeroStock = 1

Excel MS Query - How to write parameter equals "" or show all in SQL query

Just finished writing an SQL script in the MS-Query and I'm having difficulty trying to get it to work.
What I'm after is the equivalent of this SQL where clause:
AND ((examplefield = #Para) or (#Para = ''))
So if parameter = something in the field, only show that or if the parameter = blank then show all results.
So far this is what I have which works fine if I want to select a particular item, now I just need to include the additional if blank show all.
AND (`'Project Master List$'`.`Type of Work`= ?)
This unfortunately doesn't work.
AND ((`'Project Master List$'`.`Type of Work`= ?) OR (? = ""))
Any suggestions?
Try a case?
Case When ?="" THEN
'docode'
WHEN ?="OtherValue" THEN
'DoCode'
Else
'DoCode '
End
The IIF Example:
iif(?="",
iif(?="OtherValue",ReturnSomethingTrue,ReturnSomethingFalse)
,ReturnSomethingTrue,ReturnsomethingFalse)
I looked back at my original question and realised I wrapped the where clause in the wrong quotes. See below
Wrong statement
AND ((`'Project Master List$'`.`Type of Work`= ?) OR (? = ""))
Correct statement
AND (`'Project Master List$'`.`Type of Work`= ? OR ? = '')
Once I'd made the change right at the end of the clause it worked.

SQL CONCAT IF Statement?

Morning All,
Im not to sure how i need to solve my following query... I have the following query which pulls back the desired records in SQL server...
SELECT agenda.AgendaItemNumber,Agenda.AgendaName, AgendaType.AgendaTypeDescription, userdetails.fullName
FROM Agenda
JOIN AgendaType ON AgendaType.AgendaTypeID=Agenda.AgendaTypeID
JOIN UserDetails ON Agenda.AgendaID = Userdetails.AgendaID
WHERE agenda.AgendaTypeID = '2'
AND AgendaItemNumber = AgendaItemNumber
AND AgendaName = AgendaName
AND AgendaTypeDescription = AgendaTypeDescription
AND AgendaItemNumber >= '3'
The above query works but i need to enhance this slightly. It pulls back the following results, which essentially are duplicate records except for the 'fullname' column...
What i would like to do is be able to add some extra code to this query so that when i run the query i am able to display one record for each 'AgendaItemNumber' and for it to concat both of the fullnames for this record. However i have additional AgendaItemsNumbers in this table that only have 1 x user fullname assigned to them. its just these few records within the image file i need to do something clever with.
Maybe there is a better way to complete this task?
Many thanks in advance. Any queries please dont hesitate to ask.
Regards
Betty
SELECT agenda.AgendaItemNumber,
Agenda.AgendaName,
AgendaType.AgendaTypeDescription,
STUFF(( SELECT ';' + FullName
FROM UserDetails
WHERE UserDetails.AgendaID = Agenda.AgendaID
FOR XML PATH('')
), 1, 1, '') AS fullName
FROM Agenda
INNER JOIN AgendaType
ON AgendaType.AgendaTypeID=Agenda.AgendaTypeID
INNER JOIN UserDetails
ON Agenda.AgendaID = Userdetails.AgendaID
WHERE agenda.AgendaTypeID = '2'
AND AgendaItemNumber = AgendaItemNumber
AND AgendaName = AgendaName
AND AgendaTypeDescription = AgendaTypeDescription
AND AgendaItemNumber >= '3'
ADENDUM
The XML extension in SQL-Server allows you to concatenate multiple rows into a single row. The actual intention of the extension is so you can output as XML (obviously), but there are some nifty tricks that are byproducts of the extensions. In the above query, if there were a column name in the subquery (FullName) it would output as <FullName>Joe Bloggs1</FullName><FullName>Joe Bloggs2</FullName>, because there is no column name it simply concatenates the rows (not forming proper XML). The PATH part allows you to specify an additional node, for example if you use PATH('Name') in the above you would get <Name>;Joe Bloggs</Name><Name>;Joe Bloggs2</Name> If you combine Path with a column name you would get Joe Bloggs.
Finally the STUFF just removes the semicolon at the start of the list.