Replacing a parameter in 2008 R2 SSRS - sql-server

I am using SSRS 2008 R2 and report builder 3.0. I have a cascading report issue that I need assistance with. The first report runs fine. Clicking on a link in the report passes a #processID parameter to the follow up report.
Now, when running the query with a string instead of the parameter directly in SSMS it takes under 1 second. When I run it through SSRS with the parameter it takes about 15 minutes. I've read that SSRS doesn't handle parameters very well. What I would like to do is find a way to change the parameter into a string and then send that or if anyone knows of a better way.
Below is the query the report is running:
SELECT ResultDetail_View.processOid, ResultDetail_View.applicationId, outputItem.outputValue, ResultDetail_View.startTime, ResultDetail_View.resultStatus,
ResultDetail_View.statusMessage, ResultDetail_View.endTime, ResultDetail_View.ErrRec, COUNT(Summary.Id) AS SumRec
FROM ResultDetail_View LEFT OUTER JOIN
Summary ON ResultDetail_View.processOid = Summary.Id LEFT OUTER JOIN
outputItem ON ResultDetail_View.outputOid = outputItem.outputItemOid
GROUP BY ResultDetail_View.processOid, ResultDetail_View.applicationId, outputItem.outputValue, ResultDetail_View.startTime, ResultDetail_View.resultStatus,
ResultDetail_View.statusMessage, ResultDetail_View.endTime, ResultDetail_View.ErrRec
HAVING (ResultDetail_View.processOid = #processID)
ORDER BY ResultDetail_View.startTime

Regardless of the original issue, it looks like you can change your having to a where without changing the query result? If this is the case that will certainly help with performance.
Regarding the Parameter Sniffing, have you tried setting a local nvarchar variable and running the query this way in the report?
delcare #processIDLocal nvarchar(50) = #processID;
select r.processOid
,r.applicationId
,o.outputValue
,r.startTime
,r.resultStatus
,r.statusMessage
,r.endTime
,r.ErrRec
,count(s.Id) as SumRec
from ResultDetail_View as r
left outer join Summary as s
on r.processOid = s.Id
left outer join outputItem as o
on r.outputOid = o.outputItemOid
where r.processOid = #processID
group by r.processOid
,r.applicationId
,o.outputValue
,r.startTime
,r.resultStatus
,r.statusMessage
,r.endTime
,r.ErrRec
order by r.startTime;

Related

Pass string parameter to SQL over jdbc query in pentaho CDE

I'm trying to pass a string parameter obtained by another query to another query in the same dashboard but when I use the parameter no results was selected. What is the proper syntax or where is the problem.
I'm beginner in pentaho biserver-CE. I use version 6.1. with JDBC connection to SQL Server 2016 SP1.
Now I'm making a sales dashboard from invoices and i want to make a dynamic filter by the time units (which works fine) and by the country shortcuts (which didn't work). If i pass shortcut of some country directly with quotes it works fine but when I replace it by the parameter it doesn't select nothing (Also in CDA preview). When I made some reports in Jasper i used exclamation inside of the parameter to pass quoted value but here I didn't find anything like this for Pentaho.
select top 10
invrow.item as ITEM,
invrow.agent as AGENT,
sum(invrow.qt) as MEASURE,
sum(invrow.val) as VALUE
from invrow
left join invhead on invrow.type = invhead.type
and invrow.nr = invhead.nr
left join art on invrow.item = art.item
where left(invhead.date,4) = ${year}
and invhead.country like ${Country}
group by invrow.item, invrow.agent, invhead.country
order by MEASURE DESC
parameter ${Country} was acquired by another query by the same field. There is a query to gain the parameter:
select distinct
invhead.country
from
invhead
where
left(invhead.date,4) = ${year}
The original query shows nothing but when I Replace parameter ${Country} by for example 'UK' like this.
It works fine:
select top 10
invrow.item as ITEM,
invrow.agent as AGENT,
sum(invrow.qt) as MEASURE,
sum(invrow.val) as VALUE
from invrow
left join invhead on invrow.type = invhead.type
and invrow.nr = invhead.nr
left join art on invrow.item = art.item
where left(invhead.date,4) = ${year}
and invhead.country like 'UK'
group by invrow.item, invrow.agent, invhead.country
order by MEASURE DESC
Now when I use parameter I have nothing in the select list but there are rows, which should be selected.
The syntax is correct, but that way of passing parameters only works for single valued parameters.
To pass an array of parameters, as you seem to be trying, you need to pass the parameter as right hand side of a in operator,
(...)
and invhead.country in ( ${Country} )
(...)
and set the parameter to be of type StringArray.

Cannot input parameters in SQL code for MS query

I'm creating an Excel report, which other users beside myself could manage.
At the moment I'm struggling with creating the parameters in MS Query, so that they would get the input from a cell in Excel.
Everytime I try to paste my code, where I have changed the original dates with the "question marks" I get the following error.
"Parameters are not allowed in queries that can't be displayed grpahically."
I have tried to create parameters for very simple queries. Like
select *
From dmbase.test
where ID = ?
And there the parameters work, I could add even 10 or more and it would still work.
In addition I have tried to paste the code as it is in SSMS and then later through Data-Properties->Query Properties ->Definitions to change the code and add parameters to the "Command Text" box, but Excel just gives an error.
Also tried to declare variables for my parameters, but that didn't work as well. Gave an error regarding symbols.
declare Parameter1 date(10)
set Parameter1 = ?
declare Parameter2 date(10)
set Parameter2 = ?
I'm not 100% sure that the joins are to blame, but that's my feeling.
My code can be seen below.
select
loc.LocationLang
,loc.DistrictLang
,loc.InventoryRegionLang
,cod.ContractNumber
,cus.CustomerLang
,cus.InternalCustomerType
,cus.CustomerGroupCode
,cus.Organizationalcode
,css.CustomerSubSegmentLang
,loc.AnalysisRegionLang
,dht.ContractHeaderTypeLang
,cod.RentalOutDate
from
dmbase.fContractOpenDetail as cod
left join dmbase.dLocation as loc
on cod.LocationID = loc.LocationID
left join dmbase.dCustomer as cus
on cod.CustomerID = cus.CustomerID
left join dmbase.dCustomerSubSegment as css
on cus.CustomerSubsegmentID = css.CustomerSubSegmentID
left join dmbase.dContractHeaderType as dht
on cod.ContractHeaderTypeAMID = dht.ContractHeaderTypeAMID
where
cod.CompanyID = '6'
and cod.RentalOutDate between ? and ?
and left(loc.LocationLang,4) = '7201'
order by cod.ContractNumber
I would appreciate if you could say, how I could put parameters in so that I would not get the error of "Parameters are not allowed...".

How can we get list of parameters & variables and their default values of a scheduled SQL job?

I went through all the column of following query and each tables:
***SELECT * FROM
[msdb].[dbo].[sysjobs] AS [sJOB]
LEFT JOIN [msdb].[sys].[servers] AS [sSVR]
ON [sJOB].[originating_server_id] = [sSVR].[server_id]
LEFT JOIN [msdb].[dbo].[syscategories] AS [sCAT]
ON [sJOB].[category_id] = [sCAT].[category_id]
LEFT JOIN [msdb].[dbo].[sysjobsteps] AS [sJSTP]
ON [sJOB].[job_id] = [sJSTP].[job_id]
AND [sJOB].[start_step_id] = [sJSTP].[step_id]
LEFT JOIN [msdb].[sys].[database_principals] AS [sDBP]
ON [sJOB].[owner_sid] = [sDBP].[sid]
LEFT JOIN [msdb].[dbo].[sysjobschedules] AS [sJOBSCH]
ON [sJOB].[job_id] = [sJOBSCH].[job_id]
LEFT JOIN [msdb].[dbo].[sysschedules] AS [sSCH]
ON [sJOBSCH].[schedule_id] = [sSCH].[schedule_id]***
I did't get any information regarding the parameters & variables and their default values used in the scheduled job.
I got a column "Command", in which I can see the arguments passed for that job, but it is not correct for all the jobs.
Basically, I need to get the list of parameters of all the scheduled SQL jobs and list of variables created/used inside the packages of those jobs.
How can I get that?
You can't get this programmatically or with a query.
The column "Command" that you mentioned, contains all the parameters and their values that are stored in the job-step configuration. I don't know why you say it's "not correct", because I assure you it is. It's presumably just not what you expect it to be for some unknown reason.
That column is as granular as the storage gets. There is no table that holds the individual parameters and values in separate columns. The only place those are stored is in the command column.
To see this, script your job is SSMS, scroll through the steps and find the command line, which will look something like this:
#command=N'/ISSERVER "\"\SSISDB\ODS\Marketing\ImportCalls.dtsx\"" /SERVER s1wdvsqlssis2 /Par "\"MyParameterName\"";"\"MyParameterValue\"" /Par "\"$ServerOption::LOGGING_LEVEL(Int16)\"";1 /Par "\"$ServerOption::SYNCHRONIZED(Boolean)\"";True /CALLERINFO SQLAGENT /REPORTING E',
The command value should look like what you see in the command column in the jobsteps table.

MS Access ODBC Error 6623: A Winsock virtual circuit was aborted

I tried to create a query with multiple tables in it, all work fine except that when I add this one table, I got ODBC call failed error
6623: A Winsock virtual circuit was aborted.
I used Advantage SQL to link the external database to create reports from it.
The sql for the query that gives the error:
SELECT podetail.ItemPartNbr
,podetail.ItemDescription
,Sum(podetail.Qty) AS LastYearOrdQty
FROM poheader
LEFT JOIN podetail
ON poheader.PoNbr = podetail.PoNbr
WHERE poheader.PoDate >= DateSerial(Year(Date())-1,Month(Date()),1)
and poheader.PoDate <= Date()
GROUP BY podetail.ItemPartNbr
,podetail.ItemDescription;
The main sql where I want to combine other tables with the query above:
SELECT
itemmast.ItemPartNbr
, itemmast.Description
, Sum(iteminv.QtyOnHand) AS SumOfQtyOnHand
, itemmast.MinOrderQty
, itemmast.Cost
, Sum(iteminv.QtyAllocated) AS SumOfQtyAllocated
, itemmast.ReOrderQty
, QtyLastYearPurchase.LastYearOrdQty
FROM
(itemmast
LEFT JOIN iteminv ON itemmast.ItemPartNbr = iteminv.ItemPartNbr)
LEFT JOIN QtyLastYearPurchase ON (itemmast.Description = QtyLastYearPurchase.ItemDescription)
AND (itemmast.ItemPartNbr = QtyLastYearPurchase.ItemPartNbr)
GROUP BY
itemmast.ItemPartNbr
, itemmast.Description
, itemmast.MinOrderQty
, itemmast.Cost
, itemmast.ReOrderQty
, QtyLastYearPurchase.LastYearOrdQty;
I set the joint fields by the ItemPartNbr and I just need the QtyLastYearPurchase.LastYearOrdQty from the first query above to be added into the second query.
I tried to open each one of the queries/tables including the one that gives error and they all open just fine individually, so it doesn't have anything to do with the connection.
If I remove WHERE (((poheader.PoDate)>=DateSerial(Year(Date())-1,Month(Date()),1) And (poheader.PoDate)<=Date())) from the first query, the second query can display the records just fine, but it takes extremely long to show the records.
Any recommendation to fix this? Thank you!
So I tried to create queries between the itemmast and the iteminv tables, and connect the itemmast table with the query i just created and the first query I posted. It works now for now, except that it is still taking forever to open.
This is a bit troublesome since I have to create multiple queries just to combine them all into one query. I have almost 20 queries just to create three reports.

How to do Sql Server CE table update from another table

I have this sql:
UPDATE JOBMAKE SET WIP_STATUS='10sched1'
WHERE JBT_TYPE IN (SELECT JBT_TYPE FROM JOBVISIT WHERE JVST_ID = 21)
AND JOB_NUMBER IN (SELECT JOB_NUMBER FROM JOBVISIT WHERE JVST_ID = 21)
It works until I turn it into a parameterised query:
UPDATE JOBMAKE SET WIP_STATUS='10sched1'
WHERE JBT_TYPE IN (SELECT JBT_TYPE FROM JOBVISIT WHERE JVST_ID = #jvst_id)
AND JOB_NUMBER IN (SELECT JOB_NUMBER FROM JOBVISIT WHERE JVST_ID = #jvst_id)
Duplicated parameter names are not allowed. [ Parameter name = #jvst_id ]
I tried this (which i think would work in SQL SERVER 2005 - although I haven't tried it):
UPDATE JOBMAKE
SET WIP_STATUS='10sched1'
FROM JOBMAKE JM,JOBVISIT JV
WHERE JM.JOB_NUMBER = JV.JOB_NUMBER
AND JM.JBT_TYPE = JV.JBT_TYPE
AND JV.JVST_ID = 21
There was an error parsing the query. [ Token line number = 3,Token line offset = 1,Token in error = FROM ]
So, I can write dynamic sql instead of using parameters, or I can pass in 2 parameters with the same value, but does someone know how to do this a better way?
Colin
Your second attempt doesn't work because, based on the Books On-Line entry for UPDATE, SQL CE does't allow a FROM clause in an update statement.
I don't have SQL Compact Edition to test it on, but this might work:
UPDATE JOBMAKE
SET WIP_STATUS = '10sched1'
WHERE EXISTS (SELECT 1
FROM JOBVISIT AS JV
WHERE JV.JBT_TYPE = JOBMAKE.JBT_TYPE
AND JV.JOB_NUMBER = JOBMAKE.JOB_NUMBER
AND JV.JVST_ID = #jvst_id
)
It may be that you can alias JOBMAKE as JM to make the query slightly shorter.
EDIT
I'm not 100% sure of the limitations of SQL CE as they relate to the question raised in the comments (how to update a value in JOBMAKE using a value from JOBVISIT). Attempting to refer to the contents of the EXISTS clause in the outer query is unsupported in any SQL dialect I've come across, but there is another method you can try. This is untested but may work, since it looks like SQL CE supports correlated subqueries:
UPDATE JOBMAKE
SET WIP_STATUS = (SELECT JV.RES_CODE
FROM JOBVISIT AS JV
WHERE JV.JBT_TYPE = JOBMAKE.JBT_TYPE
AND JV.JOB_NUMBER = JOBMAKE.JOB_NUMBER
AND JV.JVST_ID = 20
)
There is a limitation, however. This query will fail if more than one row in JOBVISIT is retuned for each row in JOBMAKE.
If this doesn't work (or you cannot straightforwardly limit the inner query to a single row per outer row), it would be possible to carry out a row-by-row update using a cursor.

Resources