Variables in SBO Query Generator
Hi Expert,
I have created a VIEW in SQL and querying this via SBO. I need to use FROM and TO Date inputs from the user.
When using '[%0]' like I normally do, the query does not run.
{/code}
Declare @DateFrom as DateTime
Declare @DateTo as DateTime
Set @DateFrom = '[%0]'
Set @DateTo = '[%1]'
SELECT EARNER, PRODUCTLINE, Sum(LINETOTAL) AS 'Total', Sum(COMMISSION_PAYABLE) AS 'Total Commission'
FROM [dbo].[COMMISSION]
WHERE ([dbo].[COMMISSION].[DocDate] >= @DateFrom
AND [dbo].[COMMISSION].[DocDate] <= @DateTo)
Group by EARNER, PRODUCTLINE
ORDER BY EARNER, PRODUCTLINE
{/code}
Using Date values duriectly through Variables is not possible due to the formats.
So try this.
Declare @DateFrom as DateTime
Declare @DateTo as DateTime
/* SELECT FROM [dbo].[OPCH] T0 WHERE */ SET @DateFrom = /* T0.DocDate*/ '[%0]'
/* SELECT FROM [dbo].[OPCH] T0 WHERE */ SET @DateTo = /* T0.DocDueDate*/ '[%1]'
SELECT EARNER, PRODUCTLINE, Sum(LINETOTAL) AS 'Total', Sum(COMMISSION_PAYABLE) AS 'Total Commission'
FROM dbo.COMMISSION
WHERE (dbo.COMMISSION.DocDate >= @DateFrom
AND dbo.COMMISSION.DocDate <= @DateTo)
Group by EARNER, PRODUCTLINE
ORDER BY EARNER, PRODUCTLINE
DocDate from OPCH is just an example. You could use this syntax with any date field from any table.
Similar Messages
-
Wildcard empty variable query generator
Hi all,
I am using the typical [%1] variables in the query generator. However, my latest query needs to have many different variable parameters and I would like to leave some of them blank.
For example, the parameters are:
Warehouse Code [%0]
Item Group [%1]
Unit (UDF) [%2]
Row (UDF) [%3]
Column (UDF) [%4]
Problem is... I want to be able to leave some of the parameters blank and I don't want the system to filter by that parameter at all.
SO if I just want to put warehouse code in, then all the other parameters should be *. Or if i want to use item group and row, then all the others will be *. Right now I have to enter exactly what i want from all parameters, so it doesn't work very well. I figured this wouldn't be so difficult.
Any suggestions?
MThanks guys, my final code was kind of a mish mash. Works where if you don't enter any variables you will see the entire list of serialized items unfiltered, if you want to sort by location (unit, column, row, warehouse) then you can enter them as needed.
SELECT
T0.[IntrSerial],
T1.[ItemCode],
T1.[ItemName],
T2.[ItmsGrpNam] as 'Item Group Name',
T1.[ItmsGrpCod] as 'Item Group Code',
T1.[U_Unit],
T1.[U_Row],
T1.[U_Col],
T3.[WhsName],
T0.[WhsCode]
FROM OSRI T0
INNER JOIN OITM T1 ON T0.ItemCode = T1.ItemCode
INNER JOIN OITB T2 ON T1.ItmsGrpCod = T2.ItmsGrpCod
INNER JOIN OWHS T3 ON T0.WhsCode = T3.WhsCode
WHERE
((T3.[WhsName] ='[%0]') OR ('[%0]'=''))
AND
((T2.[ItmsGrpNam] ='[%1]') OR ('[%1]'=''))
AND
((T1.[U_Unit] ='[%2]') OR ('[%2]' IS NULL) OR ('[%2]'=''))
AND
((T1.[U_Row] ='[%3]') OR ('[%3]' IS NULL) OR ('[%3]'=''))
AND
((T1.[U_Col] ='[%4]') OR ('[%4]' IS NULL) OR ('[%4]'=''))
ORDER BY
T1.[ItmsGrpCod],
T1.[ItemCode]
The where clauses allow you to leave the variable blank if you want, which is very handy! I know I will use this code again. -
Declare func on Query Generator
Hi,
I am Using SAP B1 for hana ver 9.0 PL10
I have a difficulty to use Declare function as variable reference in Query Generator, Any tips for this matter ?
ThanksYou can do this using the SAP Business One Query Manager. The code below calls a stored procedure with one parameter of the DateTime type, which the user is prompted to enter:
declare @tmp as nvarchar(100)
select @tmp = T0.Name from [@ADV] T0
WHERE T0.U_Date = '[%0]'
EXEC myReport '[%0]'
The table @ADV is used just to let SBO know that the actual type of the [%0] parameter is Date. -
Using Variables in Query generator
I'm using the following query to pull a report together later to be used in a query print layout. When i specify the specific part number and customer code it returns what i expect but as soon as i add a variable it says "Must specify table"!
SELECT T0.U_ACI_CAPT, T0.U_ACI_VDS1, T0.U_ACI_VDE1, T0.U_ACI_VSS1, T0.U_ACI_VSE1, T0.U_ACI_VDS2, T0.U_ACI_VDE2, T0.U_ACI_VSS2, T0.U_ACI_VSE2, T0.U_ACI_KWT1, T0.U_ACI_KWT2, T0.U_ACI_SPD1, T0.U_ACI_SPD2, T0.U_ACI_RNC1, T0.U_ACI_RNC2, T0.U_ACI_RNC3, T0.U_ACI_RNC4, T1.ItemCode, T1.Substitute, T0.U_ACI_VDS3, T0.U_ACI_VDE3, T0.U_ACI_VSS3, T0.U_ACI_VSE3, T0.U_ACI_KWT3, T0.U_ACI_SPD3, T0.U_ACI_RNC5, T0.U_ACI_RNC6, T0.U_ACI_ACDC, T0.U_ACI_PHSE, T0.U_ACI_50HZ, T0.U_ACI_60HZ, T0.U_ACI_OFRQ
FROM ((OSCN T1 INNER JOIN ITT1 T2 ON T1.ItemCode = T2.Father) INNER JOIN ITT1 T3 ON T2.Code = T3.Father) INNER JOIN "@ACI_MRBK" T0 ON T3.Code = T0.U_ItemCode
WHERE T1.ItemCode = '[%0]' AND T1.CardCode = '[%1]'
Anyone know how to get these things working?Hi,
if your problem is similar with this symptom :
Using SAP Business One Survey Variables (parameters) in a 'Set' SQL Statement can give an error in the Query Generator.
here the solution I've got from SAP notes:
In such cases you need to put in an SQL remark statement a reference field in the DB for the Survey Variable (any field).
For Example: (to make the above query work)
Declare @Num as numeric(2)
Set @Num = /* T0.DocNum */ [%0]
Select T0.DocNum
From ORDR T0
Where docnum = @Num
Good luck
Rgds,, -
Query Generator Variable Alias
Hi,
How can I create an alias for variables in scripts made with query generator? For example, if i include where clauses such as begin date and end date using greater/less than, they will be both shown in the criteria selection box as "Posting Date." This may be confusing to users unfamiliar with the query format. Can I change these prompt names? Thank you.Hi Allen,
If you mean the selection box when you run query to input values, then the answer is no. The selection box will only desplay the field description like you can see in DTW template.
For the query results, I am sure you know how alias can be assigned
Thanks,
Gordon -
In Query Generator I need to prompt for dates using [%0] and [%1]. However, I get an error once I input dates. Do the query variables come in as string format and if so, how do I compare a date input
I'm not a specialist on this, but I have to learn this for the Certification exam. So, I played with Queries and noticed that the date format is driven by the Date format parameter in Administration - General settings - Display.
I tried the following query:
SELECT CardCode, UpdateDate FROM OCRD T0 WHERE T0.UpdateDate >= '[%1]' ORDER BY T0.UpdateDate
When the date format is CCYY-MM-DD it works with 2003-11-31 and 03-11-31 and 03.11.31.
When the date format is set to DD.MM.YYYY the query runs OK with 31.12.02.
HTH
Juha -
Query Generator Funny error message
Hello Experts, Not sure if you ever seen something like this, but be great if anyone can offer me some insight...
on a normal query with parameter I have something like thsi
/*select from ORDR t0*/
declare @begandate as datetime
/* where */
set @begandate = /* t0.docdate*/ '[%0]'
and it runs fine with just about any table, view...etc.. But as soon as you have a UDT in SBO... put in like
/*select from ORDR t0*/
declare @begandate as datetime
/* where */
set @begandate = /* t0.docdate*/ '[%0]'
select * from dbo.[@bos_del_pri] a inner join ordr b on b.u_prioritycode = a.code
where b.docdate = @begandate
it give me funny error messages, "Only one epxression can be specifed in the select list when the subquery is not intrudoced with Exist"
This is ran at a SBO 2005B PL35... when I ran a similar query in SBO 2005A PL45 I get soemthing like
"must specify table to select from".. I know this error message doesn't make sense, but not sure if anyone has encounter the same problem, and if yes possible solutions?
Thank YouHello Friends,
This is in accordance to provide the information that Query Generator does not work with parameters and User defined tables together. As per my experience in SQL UDT tables start with "@" and variables declared in SQL are also prefixed with "@" so SQL does not make any difference between UDT of SAP and variable of SQL. Thus we are not able to use ([%0] parameters and UDT tables) together.
Please correct me if I am wrong.
Please forgive me if i have posted wrong information
I look forward to your reply.
Regards,
Murtaza -
Dear all,
I face an issue in Query generator.
When i execute the query in query generator, the error is like this
1). [Microsoft][SQL Server Native Client 10.0][SQL Server]Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
'Service Contracts' (OCTR)
My query is like this
Declare @StartYear as char(4)
Declare @EndYear as char(4)
Declare @Dept as char(3)
Declare @UnitBusiness as char(3)
Begin
set @StartYear = (Select YEAR(T0.[RefDate]) from JDT1 T0 where T0.[RefDate] = [%0] )
set @EndYear = (Select YEAR(T1.[RefDate]) from JDT1 T1 where T1.[RefDate] = [%1])
set @Dept = (Select T2.[Name] from OASC T2 where T2.[Name] = [%2])
set @UnitBusiness = (Select distinct SUBSTRING(T3.[Segment_0],10,2) from OACT T3 where ( T3.[Segment_0] = [%3] OR ( 1 = (CASE WHEN [%3] = 'All' THEN 1 ELSE 2 END) ) ))
Exec NEC_RPT_FinanceReport @StartYear,@EndYear,@Dept,@UnitBusiness
End
The problem is i can not show the input screen after i execute the query generator.
If i remove the the Variable @Dept and replace with the value it runs well.
Does any one know where is the problem ?
Thanks in advance
Regards
Bodhi86HI Neetu, i have change the query without using store procedure
my query is like this
Declare @StartYear as char(4)
Declare @EndYear as char(4)
Declare @Dept as char(3)
Declare @UnitBusiness as char(3)
Begin
set @StartYear = (Select YEAR(T0.[RefDate]) from JDT1 T0 where T0.[RefDate] = [%0] )
set @EndYear = (Select YEAR(T1.[RefDate]) from JDT1 T1 where T1.[RefDate] = [%1])
set @Dept = (Select T2.[Name] from OASC T2 where T2.[Name] = [%2])
set @UnitBusiness = (Select distinct SUBSTRING(T3.[Segment_0],10,2) from OACT T3 where ( T3.[Segment_0] = [%3] OR ( 1 = (CASE WHEN [%3] = 'All' THEN 1 ELSE 2 END) ) ))
Select @StartYear,@EndYear,@Dept,@UnitBusiness
The error is like this
1). [Microsoft][SQL Server Native Client 10.0][SQL Server]Must specify table to select from.
2). [Microsoft][SQL Server Native Client 10.0][SQL Server]Statement 'User-Defined Values' (CSHS) (s) could not be prepared.
Can you help me
Thanks in advance -
Hi, I'm having sounds like silly problem using Query Generator, if I execute this query it does not give me the 'selection list' for Fiscal Year. I'm using v8.8 PL10 but in v2007 it's working fine. If I use other table like OITM w/ fieldX =N'[%0]', its working in v8.8.
SELECT T0.[Code], T0.[Name] FROM [dbo].[@BA_OAMD2] T0 WHERE T0.[U_FisYear] =N'[%0]'
thanks,
Edited by: Jojo on Jan 8, 2011 4:55 PM
Edited by: Jojo on Jan 8, 2011 4:56 PMHi, I think the problem has nothing to do w/ the SQL, because if I run it in v2007 it works but not in v8.8.
SELECT T0.[Code], T0.[Name] FROM [dbo].[@BA_OAMD2] T0 WHERE T0.[U_FisYear] =[%0]
creating inner join w/ OFPR and make where condition in OFPR.[Code] is the alternative solution, but I want to use only the code above and let me have 'list selection'
SELECT T0.* FROM [dbo].[@BA_OAMD2] T0
inner join OFPR T1 on T0.[U_FisYear] =T1.Code WHERE T1.[Code] =[%0]
I have noticed that if I use UDT in my SQL w/ variable selection the option for 'list selection' and right click on selection criteria 'List of Items' is not working. I'm using v8.8 PL10, is it a bug?
Edited by: Jojo on Jan 9, 2011 4:22 PM -
User defined query parameter in query generator
Hi All,
i am using query generator in SAP to print some records of user defined query. precisely, the details about sales employees are to be printed. now the columns which exist in the database (ie. sales employee code, name, cardcode) etc can be given as a parameter like [%0] and [%1]. but some user defined parameter such as sales commission does not show as such parameter. i want one column as
select T0.doctotal, (T0.doctotal * [%0]) / 100 as brokerage from OINV T0
--where [%0] is some commission percentage that the user will give while running this query
This query gives correct results but it gives the parameter name as 'doctotal' while showing. any idea as to how to handle it properly in SAP?
thanks in advance,
Binita
Edited by: Binita Joshi on Sep 8, 2009 3:35 PMI sometimes defined a UDT only for getting the appropriate parameter entering window. The table has no data; I used it only for its field names in this window.
(The parameter request can be placed inside a comment and the entered value is used setting an SQL variable like this:
declare @d datetime
/*select t.createdate from ordr t where t.createdate=[%0]*/
set @d=[%0]
This SQL variable can be used later in the real query.) -
Query generator used to make general total and row total
Dear experts/gurus,
I have a problem how to create general total and total field in row by using query generator. In inventory in warehouse report of SBO 2004A SP 00 PL 41, there are total row of price x quantity / warehouse (if selection criteria is warehouse) and general total. I have made query report that quite similar with inventory in warehouse report using query generator and put selection criteria and item but I still can't find out the solution. is it possible using query generator to do that or if I use query analyzer, what is the code that I have to put ? is it compute sum(qty x price) by itemcode or there is stil another ? i appreciate your answer. TIA
Rgds,Hi Jimmy,
Here's one approach to solve your problem:
= = = = = = =
select oitm.itemcode, oitm.itemname, whs.whscode, whs.quantity
from oitm inner join
(select itemcode, sum(onhand) as quantity, whscode from oitw group by itemcode, whscode) whs
on oitm.itemcode=whs.itemcode
= = = = = = =
...the main query refers to the subquery as it was another table. The subquery calculates quantity per warehouse from the oitw table.
Of course you could create a view in SQL Server instead of a subquery. That would make the query inside B1 query generator more simple and would also make it faster to execute.
Henry -
Sales report from query generator
Hi Experts,
Im using query generator i had given selection criteria like, Posting date from and To, Profit centre, Numbering series name,
query:
SELECT T0.[DocNum], T0.[VatSum], T0.[DocTotal], T1.[PrcCode], T1.[PrcName], T1.[Balance] FROM OINV T0 , OPRC T1 INNER JOIN NNM1 T2 ON T0.Series = T2.Series WHERE T0.[DocDate] > ='[%0]' AND T0.[DocDate] < ='[%1]' AND T1.[PrcCode] ='[%2]', T2.[SeriesName] ='[%3]'
Error:
1. 1)(Microsoft)SQL Native client)(SQL Server)Incorrect syntax error near u2018 u2018,Microsoft(SQL Native client) (SQL Server)statement u2018User Defined Valuesu2019 (CSHS)(1) could not be prepared.
can u give any idea to solve this problem,
thanks in advance,
anand.rHi,
I made a mistake in the variables:
SELECT T0.DocNum, T0.VatSum, T0.DocTotal, T1.PrcCode, T1.PrcName, T1.Balance
FROM dbo.OPRC AS T1 INNER JOIN
dbo.OINV AS T0 INNER JOIN
dbo.NNM1 AS T2 ON T0.Series = T2.Series ON T1.PrcCode = T0.Project
WHERE (CAST(T0.DocDate AS VarChar) <= [%1]) AND (T1.PrcCode = [%2]) AND (T2.SeriesName = [%3]) AND (CAST(T0.DocDate AS VarChar) >= [%0])
Kind regards,
Hendri Wessels
EDIT: I see that the variables do work in B1UP like this, but not in SAP Query generator.
Edited by: Hendri Wessels on Jul 27, 2009 8:58 AM -
Create new variable in a query using if
Is it possible to create a new variable in a query based on an if statement.
eg select A, B, C from table where condition
C is the new variable and should have a value of 10 if A is > 100 and a value of one if A > 100 and B < 200 for example.Yes. Why not.
select a,
b,
case
when a> 100 then
10
when a> 100 and b<200 then
1
end c
from table
where <condition>;Regards.
Satyaki De. -
CAN WE HAVE IF CONDITION IN QUERY GENERATOR
Hi friends,
am, trying to make use of if condition to obtain a set of values in query generator. Its possible to retrieve the required result set using query analyser but not in query generator. am trying to execute the following query
if exists (select owor.u_reactor from owor,oitt where oitt.code=owor.itemcode and owor.u_reactor = 1 and owor.itemcode = '100-100')
begin
select itt1.u_reactor1 from oitt,itt1 where itt1.father = oitt.code and itt1.father = '100-100'
end
else
if exists (select owor.u_reactor from owor,oitt where oitt.code=owor.itemcode and owor.u_reactor = 2 and owor.itemcode = '100-100')
begin
select itt1.u_reac_23 from oitt,itt1 where itt1.father = oitt.code and itt1.father = '100-100'
end
else
if exists (select owor.u_reactor from owor,oitt where oitt.code=owor.itemcode and owor.u_reactor = 3 and owor.itemcode = '100-100')
begin
select itt1.u_reactor4 from oitt,itt1 where itt1.father = oitt.code and itt1.father = '100-100'
end
if it is not possible to write such query in query generator can someone help me with a workaround solution.
Thank u
VaitheeswaranHi,
I dont think Nested IF Statements are allowed in PLD. The reason that u get the result in Query analyser and not in Query Generator is that SAP supports Transact SQL and many SQL Server 2000 functions are not supported.
However there is a work around in Print Layout Designer for using IF condition by having Linked Objects. Kindly see some tutorials regarding Linked fields available on SDN site and think of some other way of implementing this nested IF query.
Regards
Rizwan Hafeez
Team Lead
SAP Addon Development Section
Abacus Consulting - Pakistan -
How to do this in Query Generator?
Dear Experts,
Please check my following query I've written in Query Generator.
/* select from dbo.OCRD t0 */
declare @BP nvarchar(20)
set @BP=/* t0.CardName */ '[%0]'
declare @Dt1 datetime
declare @Dt2 datetime
set @Dt1=/* Start Date */ [%1]
set Dt2=[%2]
select distinct t1.po, Max(t1.supplier) Supplier,Max(t1.process) Process, Max(t1.OrderNo) OrderNo,Max(t1.Date) Date,
isnull((select sum(t2.Quantity) from dbo.ir t2 where t2.po=t1.po and t2.TType<0),0) Issued,
isnull((select sum(t3.Quantity) from dbo.ir t3 where t3.po=t1.po and t3.TType>0),0) Received,
isnull((select sum(t2.Quantity) from dbo.ir t2 where t2.po=t1.po and t2.TType<0),0)-isnull((select sum(t3.Quantity) from dbo.ir t3 where t3.po=t1.po and t3.TType>0),0) Balance
from dbo.ir t1 where t1.Supplier=@BP and (t1.Date>=@Dt1 and t1.Date<=@Dt2) group by t1.po
The Query works well. But for the the lines
set @Dt1=/* Start Date */ [%1]
set Dt2=[%2]
it displays BP Code as the prompt next the text boxes meant for inputting dates. I would like to display 'Start Date' and 'End Date' respectively for Dt1 and Dt2.
Please explain me how to go about it.
Thanks in advance.
Regards
Ananddeclare @Dt1 datetime
set @Dt1=/* Start Date */ [%0]
declare @Dt2 datetime
set @Dt2=/* End Date */ [%1]
/* select from dbo.OCRD t0 */
declare @BP nvarchar(20)
set @BP=/* t0.CardName */ '[%2]'
I've changed. When I run the query the input box does not appear, the query shows only a blank table.
Thanks
Anand
Maybe you are looking for
-
[Solved] Bluetooth A2DP source to pulseaudio OR ALSA
Edit So, I'm happy to say that I have it working to some degree! I wasn't happy with the default bluez tools, but I managed to find a useful set of tools which seemed to work better: http://gitorious.org/bluez-tools/bluez-tools/ I compiled and make'd
-
Issues with Macbook pro and siemens gigaset se587
Hello! Wondering if anyone can help me with this issue; Bought my macbook pro about a year ago as a refurb off the apple site, everything works fine with it up until about a month ago, when the wireless internet kept dropping out and required me to t
-
hi all, I have a 6i form which generate data into excel, I have migrated with in 10g, configure Webutil, attach webutil in form, create object group by webutil.olb, replace all ole2 with Client_ole2. there is no problem in compile time but when I run
-
Create a popup window in Swing
Hi, I want to create a popup window that contains some buttons and text fields when the user selects a menuItem from the menuBar using Swing. What would be the best way to do this?
-
Does the iphone 5 have apt-x?
Hey just wondering if the iphone 5 has apt-x now. Really would like my new HK headphones to get good audio from my iphone.