Pass the Parameter in run time in crystal report 2010 - crystal-reports

This is my Stored Procedure.
USE [DataStock]
GO
/****** Object: StoredProcedure [dbo].[getBillData] Script Date: 09/19/2014 10:47:21 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[getBillData]
#date1 varchar(20),
#date2 varchar(20)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT DISTINCT [Billlist].[billno] [Bill No.], temp.[BillDate] [Date], [Billlist].[user] [Customer Name], [Billlist].[total] Total, [paid] Paid, [discount] Discount FROM temp JOIN [Billlist] ON temp.[billno] = [Billlist].[billno] WHERE temp.[BillDate] BETWEEN #date1 and #date2
END
GO
and my crystal report code is,
Report.BillListReport rptBurndown = new Report.BillListReport();
CrystalDecisions.Shared.ConnectionInfo crDbConnection = new CrystalDecisions.Shared.ConnectionInfo();
crDbConnection.IntegratedSecurity = true;
crDbConnection.DatabaseName = "DataStock";
crDbConnection.ServerName = ChangeableFields.ServerName;
CrystalDecisions.CrystalReports.Engine.Database crDatabase = rptBurndown.Database;
CrystalDecisions.Shared.TableLogOnInfo oCrTableLoginInfo;
foreach (CrystalDecisions.CrystalReports.Engine.Table oCrTable in
crDatabase.Tables)
{
oCrTableLoginInfo = oCrTable.LogOnInfo;
oCrTableLoginInfo.ConnectionInfo = crDbConnection;
oCrTable.ApplyLogOnInfo(oCrTableLoginInfo);
}
crystalReportViewer1.ReportSource = rptBurndown;
crystalReportViewer1.RefreshReport();
I don't have idea to parse the parameter in Run Time.
In this code shown another pop up window comes when its running. I need to give that parameters in run time.

Before assigning to report source, call SetParameterValue with name and value for each parameter
rptBurndown.SetParameterValue("ParameterName", value);

Related

SSRS SQL-Restart subscription runs all scheduled Reports

I am using the folling SQL-Script to restart faild SSRS mailing subscriptions:
DECLARE #ScheduledReportName varchar(200)
DECLARE #JobID uniqueidentifier
DECLARE #LastRunTime datetime
Declare #JobStatus Varchar(100)
--------------------------------------------------------
DECLARE #RunAllReport CURSOR
SET #RunAllReport = CURSOR FAST_FORWARD
FOR
SELECT
CAT.[Name] AS RptName
, res.ScheduleID AS JobID
, sub.LastRuntime
, CASE WHEN job.[enabled] = 1 THEN 'Enabled'
ELSE 'Disabled'
END AS JobStatus
FROM
dbo.Catalog AS cat
INNER JOIN dbo.Subscriptions AS sub
ON CAT.ItemID = sub.Report_OID
INNER JOIN dbo.ReportSchedule AS res
ON CAT.ItemID = res.ReportID
AND sub.SubscriptionID = res.SubscriptionID
INNER JOIN msdb.dbo.sysjobs AS job
ON CAST(res.ScheduleID AS VARCHAR(36)) = job.[name]
INNER JOIN msdb.dbo.sysjobschedules AS sch
ON job.job_id = sch.job_id
INNER JOIN dbo.Users U
ON U.UserID = sub.OwnerID
----------------Filter der Subscriptions----------------
where sub.subscriptionid in
(
SELECT subscriptionid
FROM Subscriptions AS S
LEFT OUTER JOIN [Catalog] AS C
ON C.ItemID = S.Report_OID
WHERE S.LastStatus like 'Failure sending mail%'
)
----------------Filter der Subscriptions----------------
ORDER BY U.UserName, RptName
OPEN #RunAllReport
FETCH NEXT FROM #RunAllReport
INTO #ScheduledReportName,#JobID,#LastRunTime,#JobStatus
WHILE ##FETCH_STATUS = 0
BEGIN
Print #ScheduledReportName --&' ' & #JobID
EXEC msdb.dbo.sp_start_job #job_name =#JobID
FETCH NEXT FROM #RunAllReport
INTO #ScheduledReportName,#JobID,#LastRunTime,#JobStatus
END
CLOSE #RunAllReport
DEALLOCATE #RunAllReport
I run this if an subscripton fails. In my example I send the same Report to multiple persons as a subscription with differen parameters. Sometime one subscription fails and I want to restart the job. The query in the upper script provides the specific subscriptionID of the failed one.
But even though the ScheduleID is handed over AS JobID all the Reports are beeing resend to all people.
Is there something wrong with the script?
Pleas help me.
Turns out that the procedure in my old query:
EXEC msdb.dbo.sp_start_job #job_name =#JobID
Runs all the Subscriptions under a ScheduleID AS JobID. So all Report with a SharedSchedule are launched again, by a job restart.
To restart only a specific supbscription I have to run the following command at its place, with the subscriptionID instead of the scheduleID:
exec [dbo].[AddEvent] 'TimedSubscription', #SubscriptionID;
This will add an event into the [dbo].[Event] table that will run the subscription passed in the parameter. So only the required Report will be sent.

ColdFusion 2016 and stored proc throwing invalid character error

I am trying existing code in a CF 2016 install... I get this error
"[Macromedia][DB2 JDBC Driver][DB2]ILLEGAL SYMBOL =; VALID SYMBOLS ARE ..."
the line identified is a param of a stored proc call that looks like this:
<cfstoredproc datasource="#application.dsn#" procedure="LIVE.STOPS">
<cfprocparam type="In" cfsqltype="CF_SQL_BIGINT" dbvarname="STOPID" value="#val( variables.procstopid )#" null="no">
<cfprocparam type="In" cfsqltype="CF_SQL_INTEGER" dbvarname="TRIPID" value="#val( url.tripId )#" null="no">
</cfstoredproc>
I cannot find any mention on line of a change in stored proc tag - maybe the DB2 driver? I'm looking for any input. Thanks.
Other info;
Windows10, Apache2.4, connectiong to DB2 v10.
#pendo, Here is the stored proc - it should be noted that I abbreviated some of the sql, but the SP works and has for a long time in the app running CF10.
CREATE OR REPLACE PROCEDURE LIVE.STOP(
IN stopId BIGINT DEFAULT 0,
IN tripId INTEGER DEFAULT 0
) LANGUAGE SQL
BEGIN
DECLARE updateTripId INTEGER DEFAULT 0;
DECLARE minStopId BIGINT DEFAULT 0;
DECLARE maxStopId BIGINT DEFAULT 0;
DECLARE TripSearch_cursor CURSOR FOR
SELECT s1.fkTripsId
FROM live.paymentsTripsStops s1
JOIN live.Trips t ON s1.fkTripsId = t.Id
WHERE s1.fkStopsId = stopId
FETCH FIRST 1 ROWS ONLY;
DECLARE minMaxStop_cursor CURSOR FOR
SELECT
COALESCE(
(
SELECT s.Id
FROM live.Stops s
JOIN live.Trips t ON s.fkTripsId = t.Id
ORDER BY s.Sequence
FETCH FIRST 1 ROWS ONLY
),
0
) AS firstStopId,
COALESCE(
(
SELECT s.Id
FROM live.Stops s
JOIN live.Trips t ON s.fkTripsId = t.Id
ORDER BY s.Sequence DESC
FETCH FIRST 1 ROWS ONLY
),
0
) AS lastStopId
FROM live.Trips t
WHERE t.Id = updateTripId
FETCH FIRST 1 ROWS ONLY;
IF TripId > 0
THEN SET updateTripId = TripId;
ELSE OPEN TripSearch_cursor;
FETCH FROM TripSearch_cursor INTO updateTripId;
CLOSE TripSearch_cursor;
END IF;
IF updateTripId > 0
THEN OPEN minMaxStop_cursor;
FETCH FROM minMaxStop_cursor INTO minStopId, maxStopId;
CLOSE minMaxStop_cursor;
UPDATE live.Trips
SET fkFirstStopId = minStopId,
fkLastStopId = maxStopId
WHERE intId = updateTripId;
END IF;
END

Unable to use variable in DATEADD(#pinterval, #pinterval, #DayPlus)

DECLARE #pinterval INT = 1, #DayPlus DATETIME = '2016-07-01', #datepart VARCHAR(20) = 'MONTH'
SET #DayPlus = DATEADD(#datepart, #pinterval, #DayPlus)
SELECT #DayPlus
Do we have any alternative to accomplish task ? I have to do it in loop so I can't define it every time based on interval value. Only date part is not acceptable as variable because if I use as
DATEADD(MONTH, #pinterval, #DayPlus)
then it's working fine. Not sure but I can understand the issue but I am seeking for the quick solution. I have visited the web but didn't get exact solution.
It's not possible to substitute a variable for the first argument to DATEADD1, since what is wanted here is a name, not a string.
About the best you can do is a CASE expression:
SET #DayPlus =
CASE #datepart
WHEN 'MONTH' THEN DATEADD(MONTH, #pinterval, #DayPlus)
WHEN 'YEAR' THEN DATEADD(YEAR, #pinterval, #DayPlus)
WHEN 'DAY' THEN DATEADD(DAY, #pinterval, #DayPlus)
--TODO - add all parts you might wish to use
END
1This is even stated in the documentation:
User-defined variable equivalents are not valid.
if this is only the way then I have to repeat this for more than 10times
No, use CROSS APPLY
#Shnugo it sounds good, can you please help me by placing the complete code as an answer. Please !!
About CROSS APPLY: generate something like a variable dynamically
CREATE TABLE dbo.Test(ID INT, SomeDate DATE);
GO
INSERT INTO dbo.Test VALUES(1,{d'2017-01-01'}),(2,{d'2017-02-02'})
GO
DECLARE #Intervall VARCHAR(100)='DAY';
DECLARE #Count INT=1
SELECT dbo.Test.*
,t.AddedDate
FROM dbo.Test
CROSS APPLY(SELECT CASE #Intervall WHEN 'MONTH' THEN DATEADD(MONTH,#Count,SomeDate)
WHEN 'DAY' THEN DATEADD(DAY,#Count,SomeDate)
ELSE SomeDate END AS AddedDate) AS t;
GO
DROP TABLE dbo.Test;
You could use dynamic SQL:
DECLARE #pinterval INT = 1,
#DayPlus DATETIME = '2016-07-01',
#datepart NVARCHAR(20) = 'MONTH'
DECLARE #sql NVARCHAR(MAX) = N'SET #DayPlus = DATEADD('+#datepart+', #pinterval, #DayPlus)',
#params NVARCHAR(MAX) = N'#DayPlus DATETIME OUTPUT, #pinterval INT'
EXEC sp_executesql #sql, #params, #pinterval = #pinterval, #DayPlus = #DayPlus OUTPUT
SELECT #DayPlus
sp_executesql is used for 2 reasons:
to minimize SQL injections risk, it will not eliminate it because of #datepart
it will make sure your dynamic query's plan is going to be cached - which might be beneficial with simple queries (where query time vs compilation time matters)

Having a crystal report parameter as a declared SQL Value

I'm assuming this is a simple question, but I don't know Crystal Reports very well. I made a SQL Query which uses the declared dates fields of #beginning_date and #ending_date and I want Crystal to prompt for those fields when run. I added the paramter field in crystal and named it the same thing, but I'm unsure how to get them to sync up. My code is below. Thank you in advance.
--/*
DECLARE #beginning_date char(20)
DECLARE #ending_date char(20)
--SELECT #ending_date = '07/31/2016' --23:59:59'
--SELECT #beginning_date = '07/01/2016' --00:00:01'
--*/
SELECT --Sum(CASE when Billing_Ledger.subtype in ('BI','ND') then
--Billing_Ledger.amount ELSE 0 END) as 'Charges'
Patient_Clin_Tran.Clinic_id, Patient_Clin_Tran.service_id, Patient_Clin_Tran.program_id, Patient_Clin_Tran.protocol_id, billing_ledger.amount
FROM Billing_Ledger
JOIN Patient_Clin_Tran ON
Billing_Ledger.clinical_transaction_no = Patient_Clin_Tran.clinical_transaction_no
JOIN Coverage_Plan ON
Billing_Ledger.coverage_plan_id = Coverage_Plan.coverage_plan_id
and Billing_Ledger.hosp_status_code = Coverage_Plan.hosp_status_code
JOIN Payor ON
Coverage_Plan.payor_id = Payor.payor_id
WHERE ( Coverage_Plan.billing_type <> 'CAP' or Coverage_Plan.billing_type is null )
and Billing_Ledger.accounting_date >= #beginning_date
and Billing_Ledger.accounting_date < dateadd(day, 1, #ending_date)
and Patient_Clin_Tran.Clinic_id = 'NP' and payor.name = 'Mainecare' and (Billing_Ledger.subtype in ('BI','ND'))
--GROUP BY Patient_Clin_Tran.Clinic_id, Patient_Clin_Tran.service_id, Patient_Clin_Tran.program_id, Patient_Clin_Tran.protocol_id --, Payor.Name, billing_ledger.amount, payor.type
ORDER BY Patient_Clin_Tran.Clinic_id, Patient_Clin_Tran.service_id, Patient_Clin_Tran.program_id, Patient_Clin_Tran.protocol_id
Firstly you should turn your query into a stored procedure thus:
CREATE PROCEDURE [dbo].[mySPName]
#beginning_date date,
#ending_date date
AS
SELECT Patient_Clin_Tran.Clinic_id, Patient_Clin_Tran.service_id, Patient_Clin_Tran.program_id, Patient_Clin_Tran.protocol_id, billing_ledger.amount
etc.
Now when adding the database connection to your CR report file, after selecting your server and database you will see three options tables, views and stored procedures. Simply select the new procedure from the list. CR will now automtically add the parameters, and will prompt you for values. If you leave the values blank, then at runtime CR will automatically prompt the user for these values.
You will also note that I changed the type of the parameters to date; this is necessary so that CR knows to include a calendar selector in the parameter prompt.

Getting a value from stored procedure within a stored procedure

I have a stored procedure which returns an XML file. At the moment some calculations are done in XSL but I would like to do these within the database using another stored procedure. (adding the result of that calculation to the XML)
ALTER PROCEDURE [dbo].[app_Get_Phone_And_Tariffs]
-- Add the parameters for the stored procedure here
#phone nvarchar(150)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT
PB.UID as '#phoneid',
PB.Short_Title as '#title',
PB.Description as '#desc',
PB.Camera as '#camera',
PB.Storage as '#storage',
PB.Screen_Size as '#screensize',
PB.OS as '#os',
PB.Processor as '#chip',
PB.Image1 as '#image',
PB.Trade_Price as '#tradeprice',
(SELECT
TB.UID as '#tariffid',
TB.Tariff_Name as '#name',
TB.Carrier as '#network',
TB.Inclusive_Minutes as '#mins',
TB.Inclusive_Texts as '#texts',
TB.Inclusive_Data as '#data',
TB.Monthly_Cost as '#monthly',
TB.Commission as '#comm',
(TB.Commission - PB.Trade_Price) as '#upfront'
FROM dbo.Tariff_Base TB
WHERE TB.Active = 1 AND TB.Type = 1
FOR XML PATH('tariff'), TYPE
),
(SELECT
OP.GP_Margin as '#gpmargin'
FROM dbo.Options OP
FOR XML PATH('options'), TYPE
)
FROM dbo.Phone_Base PB
WHERE PB.Friendly_URL_Name = #phone AND PB.Active = 1
FOR XML PATH('detail'), TYPE
END
What I want to do is:
In the inner select (TB) is to call another SP lets call it "calculate" passing 2 variables (TB.Commission and PB.Trade_Price) for the sum
Calculate will return a value i.e. #hp to the stored procedure which can be added/used in the XML List.
Can this be done in SQL Server 2014/T-SQL?
No. But you could do it with a function. See the MSDN documentation, especially example A.
Something like this (untested):
CREATE FUNCTION dbo.Calculate (#Comission float, #TradePrice float)
RETURNS float
WITH EXECUTE AS CALLER
AS
BEGIN
DECLARE #SumVal float;
SET #SumVal = #Commission + # TradeValue;
RETURN(#SumVal);
END;
GO
--In your sub-query
SELECT values, dbo.Calculate(TB.Commission, PB.Trade_Price) AS A_Sum
FROM ...;