changing schema at run time delphi 5/ crystal 8.5
-
20-06-2021 - |
Question
Ive got a database right now with one username that the front (delphi 5) end connects to. we call reports using the following:
with tcrpe.create(self) do
try
report_name := 'CrystalLotRecapSummary.rpt';
if fileexists(dmposting.tws_drive+'\tws\special\'+report_name) then
ReportName := dmposting.tws_drive+'\tws\special\'+Report_Name
else
ReportName := dmposting.tws_drive+'\tws\reports\harvest\'+Report_Name;
WindowButtonBar.PrintSetupBtn := true;
Paramfields.retrieve;
ParamFields[0].Value := cmbCropYear.text; // IN OLD REPORT
ParamFields[1].Value := 'ALL';
ParamFields[2].Value := real_to_str(unitfactor,0); // IN OLD REPORT
ParamFields[3].Value := cmbUnit.text; // IN OLD REPORT
ConnectMethod := useConnect;
Connect.Retrieve;
Connect.Password := 'PASSWORD';
Output := toWindow;
Execute;
finally
CloseJob;
end;
end; // PRINT SUMMARY BY WAREHOUSE
This works just fine. the problem is that we are now about to have 2 schemas (sysdba and sysdba2).
right now the crystal reports query looks like:
SELECT
LOT_RECAP."GROWING_YEAR", LOT_RECAP."GREEN", LOT_RECAP."ADJUSTED", LOT_RECAP."SHIPPED",
LOT_RECAP."WAREHOUSE_ID", LOT_RECAP."REMAINING", LOT_RECAP."LOT_ID",
LOT_RECAP."FINISH_DRYING", LOT_RECAP."NAME", LOT_RECAP."STATUS",
LOT_RECAP."COMMODITY_ID",
LOT_RECAP."VARIETY_ID", LOT_RECAP."PRODUCER_ID", LOT_RECAP."LR_AVMOISTURE",
LOT_RECAP."PROJECTED", LOT_RECAP."NOTE", LOT_RECAP."MASTER_ID",
LOT_RECAP."VARIETY_TYPE",
LOT_RECAP."CROP", LOT_RECAP."STORAGE_ONLY"
FROM
"SYSDBA"."LOT_RECAP" LOT_RECAP
WHERE
LOT_RECAP."GROWING_YEAR" = 2009 AND
LOT_RECAP."COMMODITY_ID" = 'RICE' AND
LOT_RECAP."STORAGE_ONLY" = 'FALSE'
ORDER BY
LOT_RECAP."LOT_ID" ASC
I need to figure out some way to have the report to decide if the userid is sysdba then the table is sysdba.lot_recap, and if the userid is sysdba2 then the table name is sysdba2.lot_recap. BTW, Ive got to do this for about 300 reports. Any help would be greatly appreciated. If i didnt provide enough info please let me know
Solution
Ok issue was finally figured out. By going into the report itself and then going to Set Location. If i remove the schema name from the location Crystal reports will let me assign the schema name dynamically based on the username i sign in with.
OTHER TIPS
See the ALTER SESSION SET CURRENT SCHEMA command.
create a view to union both tables and select by a new column "userid":
create view LOT_RECAP_ALL as
select 'sysdba' userid, l.* from sysdba.lot_recap l
union all
select 'sysdba2' userid, l.* from sysdba2.lot_recap l
;
And now modify your select to select from LOT_RECAP_ALL and add LOT_RECAP."USERID" = 'sysdba' (or sysdba2):
SELECT
LOT_RECAP."GROWING_YEAR", LOT_RECAP."GREEN", LOT_RECAP."ADJUSTED", LOT_RECAP."SHIPPED",
LOT_RECAP."WAREHOUSE_ID", LOT_RECAP."REMAINING", LOT_RECAP."LOT_ID",
LOT_RECAP."FINISH_DRYING", LOT_RECAP."NAME", LOT_RECAP."STATUS",
LOT_RECAP."COMMODITY_ID",
LOT_RECAP."VARIETY_ID", LOT_RECAP."PRODUCER_ID", LOT_RECAP."LR_AVMOISTURE",
LOT_RECAP."PROJECTED", LOT_RECAP."NOTE", LOT_RECAP."MASTER_ID",
LOT_RECAP."VARIETY_TYPE",
LOT_RECAP."CROP", LOT_RECAP."STORAGE_ONLY"
FROM
"LOT_RECAP_ALL" LOT_RECAP
WHERE
LOT_RECAP."GROWING_YEAR" = 2009 AND
LOT_RECAP."COMMODITY_ID" = 'RICE' AND
LOT_RECAP."STORAGE_ONLY" = 'FALSE' AND
LOT_RECAP."USERID" = 'sysdba' -- or sysdba2
ORDER BY
LOT_RECAP."LOT_ID" ASC
Depending on your connecting user you might need to add
grant select on LOT_RECAP_ALL to sysdba;
or
grant select on LOT_RECAP_ALL to sysdba2;
And maybe you also need to add select grants on sysdba(2).lot_recap to the view owner. Can't tell this from here, depends on your schemas' access privileges.