I am trying to create a Crystal Report within VS2015 but I am having issues with a stored procedure. When I run my report without a stored procedure it gets all the data required and all the groups and formulas work fine. Below is my main SQL code.
SELECT type, description, hire_status, fleet_no, location, date_starting, time_finishing, date_finishing, type, week_number, changes
FROM XXXXXX INNER JOIN XXXXXX ON fleet_no= XXXXXX.fleet_no
WHERE week_number= #weekNo
When I try and add a stored procedure to the report I only get a blank report, I lose all the data from the report. The stored procedure is as follows:
USE [xxxxxx]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [XXXXXXX].[getCurrentLocation]
#fleet varchar(50) = NULL
AS
Begin
SELECT TOP 1 (dbo.tbl_job_planning.location)
FROM dbo.tbl_job_planning
WHERE (fleet_no = #fleet) AND (date_starting <= DATEADD(week, DATEDIFF(day, 0, GETDATE()) / 7, 5))
ORDER BY date_starting DESC
end;
I pass the parameter to this stored procedure in the record selection.
I want the stored procedure to display a location on each line of the report. When I run the report with just the stored procedure I only get 1 value returned and not a value for each item in the report which is what I need.
you are getting 1 value back because you are specifying
SELECT TOP 1 (dbo.tbl_job_planning.location). Just remove TOP 1 or set the 1 to however many results you want it limited to , but certainly not one as you specified
Related
I'm stuck and in need of some help please. I am using SQL Server 2017.
I have written a stored procedure which is being pulled into Crystal Reports. The report is a picking list. When it has been printed I want to update column PickingSlip with 'Y'
ALTER PROCEDURE [dbo].[Picking]
AS
BEGIN
SET NOCOUNT ON;
SELECT
SorMaster.SalesOrder, SorDetail.SalesOrderLine, SorDetail.StockCode, SorDetail.OrderQty, SorDetail.ShipQty
FROM
SorMaster
INNER JOIN
SorDetail ON SorMaster.SalesOrder = SorDetail.SalesOrder
WHERE
SorDetail.OrderQty = SorDetail.ShipQty
AND (PickingSlip = '')
UPDATE SorDetail
SET PickingSlip = 'Y'
WHERE
SorDetail.OrderQty = SorDetail.ShipQty
AND PickingSlip = ''
END
The report is blank. The update is working so I'm guessing the SP completes and then generates output into Crystal Reports, which is why I'm not seeing anything.
How can I update also get an output?
Crystal doesn't know what part of the SP it should use as the result set.
Perhaps you could load the SELECT into a temporary table, do the update, and finish by returning the temp table.
Or remove the update statement and call it from another SP used in a subreport.
Or call the update statement from a Crystal formula using a UFL.
I'm creating a report in SSRS using a stored procedure. My report is simple, basically pulls basic information of a client in different programs.
In my stored-procedure I have two parameters: #StartDate DATETIME and #VendorId INT = NULL.
I'm setting #Vendorid to NULL because I would like to get all clients in every program.
I have two options in my sproc:
IF (#VendorID > 0) --Select Program
BEGIN
INSERT INTO #Report
SELECT * FROM table1 WHERE VendorId = #VendorId
END
IF (#VendorID IS NULL) --All Programs
BEGIN
INSERT INTO #Report
SELECT * FROM table1
END
This report gives me the flexibility to get clients from different programs from the day they were enrolled. Hence, if I want to get all clients in every program I simply execute the sproc with the #StartDate parameter. I would like to do the same in SSRS. I just can't figure how to set the #vendorid parameter function the same way in my sproc.
Any insight would be helpful!
AS you know generally in SSRS you can have parameters as Query string or selected value
so
Just add
SELECT * FROM table1 WHERE #VendorId=-1 OR VendorId = #VendorId
To specify a custom default value here -1
Switch to Design view.
In the Report Data pane, right-click #VendorId, and then click Parameter Properties.
Click Default Values > Specify values > Add. A new value row is added.
In Value, type -1.
Click OK.
Preview the report.
You have drop down and you can use -1 as none selected.
https://learn.microsoft.com/en-us/sql/reporting-services/tutorial-add-a-parameter-to-your-report-report-builder?view=sql-server-ver15
I'm begginer to stored procedures so i start with explanation what i'm trying to achive.
I've got excel app to fetch data from SQL server. I got 6 sql SELECT statements done in a loop with changing parameters.
I'v already managed to change one SQL into stored procedure and it's working.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE Test
#itm nvarchar(20) = 0
AS
BEGIN
SET NOCOUNT ON;
SELECT ISNULL(SUM(T.[some_field]),0) as [Sum]
FROM [some_table] AS T, [some_other_table] AS F, [some_other_table2] AS I
WHERE T.[field] = 1 AND I.[field] = #itm AND
F.[field] = T.[field] AND F.[field] = I.[field]
END
When i got this first one to work i started to wonder if it's possible to put all 6 SELECT queries into one stored procedure?
All queries results in one field which is sum of quantities.
All queries takes at least one parameter.
At the end i'd like to recive one record with 6 fields which i can simply put in excel by Range("A1").copyfromrecordset rst
instead of creating 6 different stored procedures for each SELECT
Will it be as simple as just put one SELECT after another? And what about parameters? If parameter is same for 2 queries should it be named same in both?
You could return all 6 fields from one stored procedure. At the most basic level the way to do that would be something like:
SELECT
(
SELECT ISNULL(SUM(T.[some_field]),0) as [Sum]
FROM [some_table] AS T, [some_other_table] AS F, [some_other_table2] AS I
WHERE T.[field] = 1 AND I.[field] = #itm AND
F.[field] = T.[field] AND F.[field] = I.[field]
) Field1
,
(
-- Another query
) Field2
, etc.
I have a Crystal Report (using CR 2008) that has three database queries (the below is sourced from the "Show SQL" menu option)
MYSERVER
"mydb"."dbo"."run_report";1 {d '2000-01-01'}, {d '2015-01-01'}, 'A12345'
MYSERVER
select SUser_SName() as [CurrentUser]
MYSERVER
select parent_name from containers where id = '{?#id}'
None of these queries depend on each other, although the first and last queries use the same input parameter - #id (which is A12345 in this example)
The above works fine when the run_report stored procedure returns > 0 results. When it returns no results (but no actual errors as far as I can tell), the second and third SQL queries don't appear to be executed as their results (which are inserted into the report as fields) are just blank.
I can see two possible approaches to the issue:
Reorder the SQL queries so that the ones that will always return a result run first, and the stored procedure (which may return no results) runs last; or
Somehow I make Crystal Reports continue on its merry way even if the stored procedure query doesn't return any rows.
...but I'm not sure how to achieve either approach.
I cross-posted on the SAP forums and got this answer.
There are two possible solutions
Make the stored procedure return a single row of all NULL values when there's no actual results.
Split the report up into one main report and two sub-reports. The Stored Procedure runs in the main report, and each of the other queries runs in its own sub-report
I used solution #1 (as it was less work in my case) and it worked fine. I changed my stored procedure into a SELECT INTO, with results getting put into a temporary table. I could then go:
-- Do the main SELECT INTO query first, then...
-- Check if temporary table is empty
IF NOT EXISTS (SELECT TOP 1 Id FROM #report WHERE Id IS NOT NULL)
BEGIN
-- If the source table you SELECTed from had NOT NULL columns,
-- you'll need to alter them prior to inserting your row of NULLS
ALTER TABLE #report ALTER COLUMN Name VARCHAR(255) NULL
-- Rinse and repeat for other affected columns
-- Then insert the row of NULLS
INSERT INTO #report VALUES(NULL, NULL, NULL, NULL, NULL, NULL)
END
-- Return the contents of the report
SELECT * FROM #report
I created a stored procedure in SQL Server. It works fine there.
When I call it from Crystal Reports, it shows parameters fields but it does not show the outputs in database fields. Actually it shows the stored procedure in database fields but not the output fields or + sign beside the stored procedure.
Here is the codes in stored procedure:
CREATE PROCEDURE [test]
#mcode char(10),
#zcode [int],
#odolmas[float]=0 output,
#gmas[float]=0 output,
#vmas[float]=0 output
AS
set #gmas=0
set #vmas=0
set #gmas=9
set #vmas=5
set #odolmas=((#vmas-#gmas)/2)
GO
In Crystal Reports I can see the #mcode and #zcode as input parameters. But I cannot see #gmas, #vmas and #odolmas as outputs.
Please help me do it and because I am not professional please say it step by step what should I do
Thanks
When we execute the stored procedure shown in your question, we won't get any output. Try using the select statement inside your stored procedure, the return values will be seen by crystal report.
Say for example Select #gmas as Col1, #vmas as Col2 in your procedure at the end, may show the fileds as col1 and col2. You can place these col1 and col2 on your report for display purpose.