Having a crystal report parameter as a declared SQL Value - sql-server-2008-r2

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.

Related

Firebird dynamic Var New and Old

I need validate dynamic Fields from a Table. For example:
CREATE TRIGGER BU_TPROYECTOS FOR TPROYECTOS
BEFORE UPDATE AS
DECLARE VARIABLE vCAMPO VARCHAR(64);
BEGIN
/*In then table "TCAMPOS" are the fields to validate*/
for Select CAMPO from TCAMPOS where TABLA = TPROYECTOS and ACTUALIZA = 'V' into :vCAMPO do
Begin
if (New.:vCAMPO <> Old.:vCampo) then
/*How i get dynamic New.Field1, New.Field2 on query return*/
End;
END ;
The question is : How can I put "The name of the field that the query returns me " in the above code .
Ie if the query returns me the field1 and field5 , I would put the trigger
if ( New.Field1 < > Old.Field1 ) or ( New.Field5 < > Old.Field5 ) then
There is no such feature in Firebird. You will need to create (and preferably) generate triggers that will reference all fields hard coded. If the underlying table changes or the requirements for validation, you will need to recreate the trigger to take the added or removed fields into account.

Report Server - Can't declare parameters

My problem is that I was not able to declare parameters through Report Builder.
I was receiving the following error:
ERROR [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Must
declare the scalar variable "#param".
So I googled it and found that it can be solved easily by putting symbol "?" instead of "#parameter" and it did solved my problem for a while.
But now I have another problem. I have a select like:
select * from table t where t.date = ? or t.date2 = ? or t.date3 = ?
Where all three "?" are '2013-aug-01', but each "?" creates a new parameter in the parameters section of Report Builder.
How can I use one parameter for all three cases?
Try this
select * from table t where ? IN (t.date, t.date2, t.date3)
Another way of solving is just delete the two variables created by SSRS and update your query to
select * from table t where t.date = #param1 or t.date2 = #param1 or t.date3 = #param1
Another method write your query
select * from table t where t.date = ? or t.date2 = ? or t.date3 = ?
Let it create the 3 variables. Then go to Dataset, under dataset properties goto parameters section. There update the parameter value of 2 & 3 to Parameter1. Next go in the parameters section and delete the automatically generated Parameter2 & Parameter3.

T-SQL not executing 2nd part of WHERE clause SSRS

I have a SSRS report which runs from a stored procedure. I've got 2 parameters that must be chosen. My first parameter works perfectly, but when the 2nd parameter is chosen (Rep) it gets ignored by the report and returns all the Reps.
#Town Varchar(100)
,#Rep Varchar(100)
select
a.Customer
,a.CustName
,a.Rep
,a.Town
,a.Qty
,a.SalesType
,b.Qty1
,c.Qty2
......
from #1 a
left join #2 b
on a.Rep = b.Rep
and a.Town = b.Town
and a.Customer = b.Customer
and a.SalesType = b.SalesType
left join #3 c
..........
WHERE ('ALL' IN (#Town)) OR (a.Town IN (#Town))
and ('ALL' IN (#Rep)) OR (a.Rep IN (#Rep))
Guessing here, but I am assuming you want the clauses for #Town and #Rep to be evaluates as a group each. To do so:
WHERE ('ALL' IN (#Town) OR a.Town IN (#Town))
and ('ALL' IN (#Rep) OR a.Rep IN (#Rep))
As you have it, since each clause is stand alone, you will get short circuit behavior.

Metadata about a column in SQL Server 2008 R2?

I'm trying to figure out a way to store metadata about a column without repeating myself.
I'm currently working on a generic dimension loading SSIS package that will handle all my dimensions. It currently does :
Create a temporary table identical to the given table name in parameters (this is a generic stored procedure that receive the table name as parameter, and then do : select top 0 * into ##[INSERT ORIGINAL TABLE NAME HERE] from [INSERT ORIGINAL TABLE NAME HERE]).
==> Here we insert custom code for this particular dimension that will first query the data from a datasource and get my delta, then transform the data and finally loads it into my temporary table.
Merge the temporary table into my original table with a T-SQL MERGE, taking care of type1 and type2 fields accordingly.
My problem right now is that I have to maintain a table with all the fields in it to store a metadata to tell my scripts if this particular field is type1 or type2... this is nonsense, I can get the same data (minus type1/type2) from sys.columns/sys.types.
I was ultimately thinking about renaming my fields to include their type in it, such as :
FirstName_T2, LastName_T2, Sex_T1 (well, I know this can be type2, let's not fall into that debate here).
What do you guyz would do with that? My solution (using a table with that metadata) is currently in place and working, but it's obvious that repeating myself from the systables to a custom table is nonsense, just for a simple type1/type2 info.
UPDATE: I also thought about creating user defined types like varchar => t1_varchar, t2_varchar, etc. This sounds like something a bit sluggy too...
Everything you need should already be in INFORMATION_SCHEMA.COLUMNS
I can't follow your thinking of not using provided tables/views...
Edit: As scarpacci mentioned, this somewhat portable if needed.
I know this is bad, but I will post an answer to my own question... Thanks to GBN for the help tho!
I am now storing "flags" in the "description" field of my columns. I, for example, can store a flag this way : "TYPE_2_DATA".
Then, I use this query to get the flag back for each and every column :
select columns.name as [column_name]
,types.name as [type_name]
,extended_properties.value as [column_flags]
from sys.columns
inner join sys.types
on columns.system_type_id = types.system_type_id
left join sys.extended_properties
on extended_properties.major_id = columns.object_id
and extended_properties.minor_id = columns.column_id
and extended_properties.name = 'MS_Description'
where object_id = ( select id from sys.sysobjects where name = 'DimDivision' )
and is_identity = 0
order by column_id
Now I can store metadata about columns without having to create a separate table. I use what's already in place and I don't repeat myself. I'm not sure this is the best possible solution yet, but it works and is far better than duplicating information.
In the future, I will be able to use this field to store more metadata, where as : "TYPE_2_DATA|ANOTHER_FLAG|ETC|OH BOY!".
UPDATE :
I now store the information in separate extended properties. You can manage extended properties using sp_addextendedproperty and sp_updateextendedproperty stored procedures. I have created a simple store procedure that help me to update those values regardless if they currently exist or not :
create procedure [dbo].[UpdateSCDType]
#tablename nvarchar(50),
#fieldname nvarchar(50),
#scdtype char(1),
#dbschema nvarchar(25) = 'dbo'
as
begin
declare #already_exists int;
if ( #scdtype = '1' or #scdtype = '2' )
begin
select #already_exists = count(1)
from sys.columns
inner join sys.extended_properties
on extended_properties.major_id = columns.object_id
and extended_properties.minor_id = columns.column_id
and extended_properties.name = 'ScdType'
where object_id = (select sysobjects.id from sys.sysobjects where sysobjects.name = #tablename)
and columns.name = #fieldname
if ( #already_exists = 0 )
begin
exec sys.sp_addextendedproperty
#name = N'Scd_Type',
#value = #scdtype,
#level0type = N'SCHEMA',
#level0name = #dbschema,
#level1type = N'TABLE',
#level1name = #tablename,
#level2type = N'COLUMN',
#level2name = #fieldname
end
else
begin
exec sys.sp_updateextendedproperty
#name = N'Scd_Type',
#value = #scdtype,
#level0type = N'SCHEMA',
#level0name = #dbschema,
#level1type = N'TABLE',
#level1name = #tablename,
#level2type = N'COLUMN',
#level2name = #fieldname
end
end
end
Thanks again

SSRS 2008: How to create parameter based on another parameter

I know others have asked similar questions, but I have tried their solutions and it still is not working for me.
I have one parameter called "Region" which uses the "region" dataset and another report parameter called "Office" which uses the "office" dataset.
Now I want "Office" list of values to filter based on "Region" selection. Here is what I did so far. For the region dataset, it returns "regions_id" and "region_description". Then for "Region" report parameter, I selected "Text" datatype and allow Null values. This may be a mistake to select "text" since this is a uniqueidentifier value. For available values, I selected the region dataset and regions_id for value, region_description for label. I went to Advanced tab and selected "Always refresh". And on Default tab, I entered "(Null)", for when they want to see all regions.
NExt, I created a report parameter called "regions_id2", allow null values, and I set available values = region dataset. For values and label both, I specified the regions_id. For default value, I again entered "(Null)". And I again selected "Always refresh".
Finally, I added this "regions_id2" parameter to the "office" dataset. And then the office report parameter uses the "office" dataset with available values. Value field = "group_profile_id" and label field = "name_and_license". Default values = "(Null)". Advanced "Always refresh".
And I ordered these report parameters in this same order: Regions, regions_id2, and Office. But now when I run this report I get no errors, however, the list of offices includes all of the offices regardless of what I choose for regions. Here is my T-SQL for these datasets:
CREATE Procedure [dbo].[rpt_rd_Lookup_Regions]
(
#IncludeAllOption bit = 0,
)
As
SET NOCOUNT ON
If #IncludeAllOption = 1
BEGIN
Select Distinct
NULL AS [regions_id],
'-All-' AS [region_description]
UNION ALL
SELECT Distinct
[regions_id],
[region_description]
FROM [evolv_cs].[dbo].[regions]
Where [region_description] not in ('NA','N/A')
Order By [region_description]
END
Else
BEGIN
SELECT Distinct
[regions_id],
[region_description]
FROM [evolv_cs].[dbo].[regions]
Where [region_description] not in ('NA','N/A')
Order By [region_description]
END
CREATE Procedure [dbo].[rpt_rd_Lookup_Facilities]
(
#IncludeAllOption bit = 0,
#regions_id uniqueidentifier = NULL
)
As
SET NOCOUNT ON
If #IncludeAllOption = 1
BEGIN
Select
Null As [group_profile_id],
Null As [profile_name],
Null As [license_number],
Null As [other_id],
--Null As [Regions_id],
'-All-' As [name_and_license]
UNION ALL
SELECT
[group_profile_id],
[profile_name],
[license_number],
[other_id],
--[regions_id],
[profile_name] + ' (' + LTRIM(RTRIM([license_number])) + ')' As [name_and_license]
FROM [evolv_cs].[dbo].[facility_view] With (NoLock)
Where [is_active] = 1 and (#regions_id is NULL or #regions_id = [regions_id])
Order By [profile_name]
END
Else
BEGIN
SELECT
[group_profile_id],
[profile_name],
[license_number],
[other_id],
[regions_id],
[profile_name] + ' (' + LTRIM(RTRIM([license_number])) + ')' As [name_and_license]
FROM [evolv_cs].[dbo].[facility_view] With (NoLock)
Where [is_active] = 1 and (#regions_id is NULL or #regions_id = [regions_id])
Order By [profile_name]
END
What could I possibly be doing wrong?
I fixed this by selecting the region parameter value from region dataset for the office dataset