473,414 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Stored procedure returns duplicates

I am trying to create a report in Crystal Reports (v 8.5). I have a
stored procedure to pull data from two databases and parameters.
There are multiple one-to-many relationships and the stored procedure
returns duplicates; e.g., one schedule may have multiple resources,
supplies, and/or orders (and one order may have multiple foods). Is
there a way to stop the duplication?

The stored procedure looks like this:

************************************************** **********************************
SET QUOTED_IDENTIFIER OFF
GO

SET ANSI_NULLS OFF
GO

CREATE PROCEDURE usp_rpt1 (
@start_date smalldatetime,
@end_date smalldatetime,
@rpt_type varchar(3),
@rpt_id int
)
AS

set nocount on

--Set up some string variables to build the selection query for the
parameters supplied

declare @fields varchar(255)
declare @tables varchar(255)
declare @where varchar(2000)

CREATE TABLE #tmp_sched(sched_id int, rpt_type_desc varchar(100),
rpt_id int)

set end_date = midnight of next day
SELECT @end_date = DATEADD(day,1,@end_date)
SELECT @end_date = CONVERT(smalldatetime,
CONVERT(varchar(4),YEAR(@end_date)) + '-'
+
CONVERT(varchar(2),MONTH(@end_date)) + '-'
+
CONVERT(varchar(2),DAY(@end_date))

IF @rpt_type = 'LOC'
INSERT INTO #tmp_sched
SELECT DISTINCT s.sched_id, l.loc_desc, l.loc_id
FROM tbl_sched s
LEFT JOIN tbl_sched_res_date srd ON s.sched_id = srd.sched_id
LEFT JOIN tbl_res r ON srd.res_id = r.res_id
LEFT JOIN tbl_grp g ON r.grp_id = g.grp_id
LEFT JOIN tbl_loc l ON g.loc_id = l.loc_id
WHERE l.loc_id = CONVERT(varchar(12),@rpt_id)
AND g.obsolete_flag = 0
AND r.obsolete_flag = 0
ANd l.obsolete_flag = 0
AND s.deleted_flag = 0
AND srd.mtg_start_date_local >=
CONVERT(varchar(20), @start_date, 1)
AND srd.mtg_start_date_local <
CONVERT(varchar(20), @end_date+1,1)
IF @rpt_type = 'GRP'
INSERT INTO #tmp_sched
SELECT DISTINCT s.sched_id, g.grp_desc, g.grp_id
FROM tbl_sched s
LEFT JOIN tbl_sched_res_date srd ON s.sched_id =
srd.sched_id
LEFT JOIN tbl_res r ON srd.res_id = r.res_id
LEFT JOIN tbl_grp g ON r.grp_id = g.grp_id
WHERE (g.grp_id = CONVERT(varchar(12),@rpt_id)
OR g.parent_grp_id =
CONVERT(varchar(12),@rpt_id))
AND g.obsolete_flag = 0
AND r.obsolete_flag = 0
AND s.deleted_flag = 0
AND srd.mtg_start_date_local >=
CONVERT(varchar(20), @start_date, 1)
AND srd.mtg_start_date_local <
CONVERT(varchar(20), @end_date+1,1)
IF @rpt_type = 'RES'
INSERT INTO #tmp_sched
SELECT DISTINCT s.sched_id, r.res_desc, r.res_id
FROM tbl_sched s
LEFT JOIN tbl_sched_res_date srd ON s.sched_id =
srd.sched_id
LEFT JOIN tbl_res r ON srd.res_id = r.res_id
WHERE r.res_id = CONVERT(varchar(12),@rpt_id)
AND r.obsolete_flag = 0
AND s.deleted_flag = 0
AND srd.mtg_start_date_local >=
CONVERT(varchar(20), @start_date, 1)
AND srd.mtg_start_date_local <
CONVERT(varchar(20), @end_date+1, 1)
IF @rpt_type = 'REG'
INSERT INTO #tmp_sched
SELECT DISTINCT s.sched_id, reg.region_desc,
reg.region_id
FROM tbl_sched s
LEFT JOIN tbl_sched_res_date srd ON s.sched_id =
srd.sched_id
LEFT JOIN tbl_res r ON srd.res_id = r.res_id
LEFT JOIN tbl_grp g ON r.grp_id = g.grp_id
LEFT JOIN tbl_loc l ON g.loc_id = l.loc_id
LEFT JOIN tbl_region reg ON l.loc_id = reg.region_id
WHERE reg.region_id = CONVERT(varchar(12),@rpt_id)
AND reg.obsolete_flag = 0
AND l.obsolete_flag = 0
AND g.obsolete_flag = 0
AND r.obsolete_flag = 0
AND s.deleted_flag = 0
AND srd.mtg_start_date_local >=
CONVERT(varchar(20), @start_date, 1)
AND srd.mtg_start_date_local <
CONVERT(varchar(20), @end_date+1, 1)
IF @rpt_type NOT IN ('LOC','GRP','RES','REG')
INSERT INTO #tmp_sched
SELECT DISTINCT s.sched_id, g.grp_desc, g.grp_id
FROM tbl_sched s
LEFT JOIN tbl_sched_res_date srd ON s.sched_id =
srd.sched_id
LEFT JOIN tbl_res r ON srd.res_id = r.res_id
LEFT JOIN tbl_grp g ON r.grp_id = g.grp_id
WHERE (g.grp_id = 0 OR g.parent_grp_id = 0)
AND g.obsolete_flag = 0
AND r.obsolete_flag = 0
AND s.deleted_flag = 0
AND srd.mtg_start_date_local >=
CONVERT(varchar(20), @start_date, 1)
AND srd.mtg_start_date_local <
CONVERT(varchar(20), @end_date+1,1)

--This is the selection for our report
SELECT Description = ts.rpt_type_desc,
Date = CONVERT(varchar(12),srd.mtg_start_date_local,101),
StartTime = srd.mtg_start_date_local,
EndTime = srd.mtg_end_date_local,
SchedID = s.sched_id,
MeetingTitle = s.sched_desc,
ResourceUsed = r.res_desc,
ResourceSetup = su.setup_desc + ' (' +
CONVERT(varchar(10),rs.capacity) + ')',
NumberOfAttendees = Attendees.string_value,
OrderID = ord.order_id,
FoodQty = CONVERT (int,oi.order_qty),
FoodDesc = i.item_name,
Side = sidei.item_name,
MeetingDesc = ord.order_desc,
Supplies = suppliesudf.udf_desc,
SuppliesVal = supplies.value,
AccountCode = ord.order_user_acct_code,
host.string_value as MeetingHost,
CateringNotes = ord.order_notes,
FoodNotes = oi.order_notes

FROM #tmp_sched ts
JOIN tbl_sched s ON ts.sched_id = s.sched_id
JOIN tbl_sched_res_date srd ON ts.sched_id = srd.sched_id
JOIN tbl_res r ON srd.res_id = r.res_id
JOIN tbl_sched_res_setup srs ON s.sched_id = srs.sched_id and
r.res_id = srs.res_id
LEFT JOIN tbl_res_setup rs ON srs.setup_id = rs.setup_id AND
srs.res_id = rs.res_id
LEFT JOIN tbl_setup su ON rs.setup_id = su.setup_id
LEFT JOIN tbl_sched_request_tab_val supplies ON s.sched_id =
supplies.sched_id
AND ((supplies.request_tab_id =
(SELECT request_tab_id FROM tbl_request_tab WHERE
(request_tab_hdr = 'A) Meeting Supplies')))
OR (supplies.request_tab_id =
(SELECT request_tab_id FROM tbl_request_tab WHERE
(request_tab_hdr = 'Mtg Supplies-PEMC'))))
AND (CONVERT(varchar, supplies.value) NOT IN ('0', ''))
LEFT JOIN tbl_udf suppliesudf ON supplies.udf_id =
suppliesudf.udf_id
JOIN tbl_sched_udf_val attendees ON attendees.sched_id = s.sched_id
AND attendees.udf_id =
(SELECT udf_id FROM tbl_udf WHERE udf_desc = 'Number of
Attendees') --UDF For No of Attendees
JOIN tbl_sched_udf_val host ON host.sched_id = s.sched_id
AND host.udf_id =
(SELECT udf_id FROM tbl_udf WHERE udf_desc = 'Meeting
Host') --UDF For meeting host name
LEFT JOIN RSCatering.dbo.tbl_Order ord ON ord.order_sched_id =
s.sched_id --Our link to table in other database
JOIN RSCatering.dbo.tbl_order_item oi ON ord.order_id =
oi.order_id
LEFT JOIN RSCatering.dbo.tbl_menu_item mi ON oi.menu_item_id =
mi.menu_item_id
LEFT JOIN RSCatering.dbo.tbl_item i ON mi.item_id = i.item_id
LEFT JOIN RSCatering.dbo.tbl_order_item_sides side ON
oi.order_item_id = side.order_item_id
LEFT JOIN RSCatering.dbo.tbl_item sidei ON side.item_id =
sidei.item_id

WHERE ord.deleted_flag = 0 AND oi.deleted_flag = 0
ORDER BY
ts.rpt_type_desc,srd.mtg_start_date_local,srd.mtg_ end_date_local,
r.res_desc

DROP TABLE #tmp_sched
GO

SET QUOTED_IDENTIFIER OFF
GO

SET ANSI_NULLS ON
GO
************************************************** ****************************************

The simplified result looks like:

Sched2 Resource1 Supply1 Order5
Sched2 Resource1 Supply1 Order6
Sched2 Resource1 Supply3 Order5
Sched2 Resource1 Supply3 Order6
Sched2 Resource2 Supply1 Order5
Sched2 Resource2 Supply1 Order6
Sched2 Resource2 Supply3 Order5
Sched2 Resource2 Supply3 Order6

However, I want the result to look like:

Sched2 Resource1 Supply1 Order5
Sched2 Resource2 Supply3 Order6

Any suggestion is greatly appreciated.

Apr 9 '07 #1
6 5076
yi**********@yahoo.com wrote:
--Set up some string variables to build the selection query for the
parameters supplied

declare @fields varchar(255)
declare @tables varchar(255)
declare @where varchar(2000)
These are unused and should be removed. (Unless they're used in code
that you edited out because it wasn't relevant to the problem at hand.)
IF @rpt_type = 'GRP'
IF @rpt_type NOT IN ('LOC','GRP','RES','REG')
The blocks following these appear to be identical. I recommend removing
the latter, and adding the following above the first INSERT INTO block:

IF @rpt_type NOT IN ('LOC','GRP','RES','REG') THEN
SET @rpt_type = 'GRP'
The simplified result looks like:

Sched2 Resource1 Supply1 Order5
Sched2 Resource1 Supply1 Order6
Sched2 Resource1 Supply3 Order5
Sched2 Resource1 Supply3 Order6
Sched2 Resource2 Supply1 Order5
Sched2 Resource2 Supply1 Order6
Sched2 Resource2 Supply3 Order5
Sched2 Resource2 Supply3 Order6
Oversimplified. Are these SchedID, ResourceUsed, SuppliesVal, and
OrderID?
However, I want the result to look like:

Sched2 Resource1 Supply1 Order5
Sched2 Resource2 Supply3 Order6
We need more information about all the tables involved in the stored
procedure's final query, specifically

a) whether their relationships are 1:1 or 1:N or M:N

b) what criteria tell us that Resource1 should associate only with
Supply1 (not Supply3) and Order5 (not Order6), and similarly
for Resource2 and Supply3 (not Supply1) and Order6 (not Order5)
Apr 10 '07 #2
Thank you for your feedback.

The final query has all the columns specified in SELECT. Yes, your
guess is correct - SchedID, ResourceUsed, SuppliesVal, and OrderID
were what was on my mind as I typed the example.

Their relations are as follows:

Schedule to Resource is 1:N
Schedule to Supply is 1:N
Schedule to Order is 1:N
Order to Food is 1:N

Resource, Supply and Order are not directly related to each other.
There is no association that you were asking about in b).
b) what criteria tell us that Resource1 should associate only with
Supply1 (not Supply3) and Order5 (not Order6), and similarly
for Resource2 and Supply3 (not Supply1) and Order6 (not Order5)
Since we are pulling from two databases and using parameters, our
solution has been to use a stored procedure. Crystal Reports (v 8.5)
allows only one stored procedure.
On Apr 9, 6:47 pm, Ed Murphy <emurph...@socal.rr.comwrote:
yin_n_yan...@yahoo.com wrote:
--Set up some string variables to build the selection query for the
parameters supplied
declare @fields varchar(255)
declare @tables varchar(255)
declare @where varchar(2000)

These are unused and should be removed. (Unless they're used in code
that you edited out because it wasn't relevant to the problem at hand.)
IF @rpt_type = 'GRP'
IF @rpt_type NOT IN ('LOC','GRP','RES','REG')

The blocks following these appear to be identical. I recommend removing
the latter, and adding the following above the first INSERT INTO block:

IF @rpt_type NOT IN ('LOC','GRP','RES','REG') THEN
SET @rpt_type = 'GRP'
The simplified result looks like:
Sched2 Resource1 Supply1 Order5
Sched2 Resource1 Supply1 Order6
Sched2 Resource1 Supply3 Order5
Sched2 Resource1 Supply3 Order6
Sched2 Resource2 Supply1 Order5
Sched2 Resource2 Supply1 Order6
Sched2 Resource2 Supply3 Order5
Sched2 Resource2 Supply3 Order6

Oversimplified. Are these SchedID, ResourceUsed, SuppliesVal, and
OrderID?
However, I want the result to look like:
Sched2 Resource1 Supply1 Order5
Sched2 Resource2 Supply3 Order6

We need more information about all the tables involved in the stored
procedure's final query, specifically

a) whether their relationships are 1:1 or 1:N or M:N

b) what criteria tell us that Resource1 should associate only with
Supply1 (not Supply3) and Order5 (not Order6), and similarly
for Resource2 and Supply3 (not Supply1) and Order6 (not Order5)

Apr 10 '07 #3
yi**********@yahoo.com wrote:
Resource, Supply and Order are not directly related to each other.
There is no association that you were asking about in b).
> b) what criteria tell us that Resource1 should associate only with
Supply1 (not Supply3) and Order5 (not Order6), and similarly
for Resource2 and Supply3 (not Supply1) and Order6 (not Order5)
Then it sounds like, instead of this monstrosity of a stored
procedure, what you really want is three Crystal subreports
side by side.
Since we are pulling from two databases and using parameters, our
solution has been to use a stored procedure. Crystal Reports (v 8.5)
allows only one stored procedure.
You can do this with views instead, e.g.
create view vTable2 as select * from other_database.dbo.Table2
Apr 10 '07 #4
(yi**********@yahoo.com) writes:
The final query has all the columns specified in SELECT. Yes, your
guess is correct - SchedID, ResourceUsed, SuppliesVal, and OrderID
were what was on my mind as I typed the example.

Their relations are as follows:

Schedule to Resource is 1:N
Schedule to Supply is 1:N
Schedule to Order is 1:N
Order to Food is 1:N

Resource, Supply and Order are not directly related to each other.
There is no association that you were asking about in b).
So when you want:
Sched2 Resource1 Supply1 Order5
Sched2 Resource2 Supply3 Order6
That's completely arbitrary, and you could just as well be satisfied
with:
Sched2 Resource1 Supply3 Order6
Sched2 Resource2 Supply1 Order5
Or any other combination? Get data into yet another temp table,
and

SELECT schedule, resource, min(Supply), Min(Order)
FROM #temp
GROUP BY schedule, resource

But arbitrary results sets do not really make sense to me.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Apr 10 '07 #5
On 9 Apr 2007 15:42:24 -0700, yi**********@yahoo.com wrote:
>I am trying to create a report in Crystal Reports (v 8.5). I have a
stored procedure to pull data from two databases and parameters.
There are multiple one-to-many relationships and the stored procedure
returns duplicates; e.g., one schedule may have multiple resources,
supplies, and/or orders (and one order may have multiple foods). Is
there a way to stop the duplication?
(snip)

Hi yin_n_yang74,

The first time you posted this question, I advised you to find a client
side solution and provided an outline of the algorithm to use. I also
pointed you to my bog entry that details a possible way to solve this
server-side, in case a client-side solution is not possible.

You now reposted the problem, with more detail, but my answer remains
the same. Either have Crystal Reports open three datasets and process
data from all of them at the same time, pairing data from the datasets
to form formatted output lines - or read my blog article at
http://sqlblog.com/blogs/hugo_kornel...ated-rows.aspx
if you really prefer a server-side solution and don't mind the
performance hit this will incur.

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Apr 11 '07 #6
>Any suggestion is greatly appreciated. <<

EVERYTHING you are doing is TOTALLY wrong. You have just been cussed
out by one of the people who wrote this language. If you have brain
instead of an ego, you might want to listen.

This is a (bad) COBOL program written in SQL! There is so much
formatting done in SQL code! The bad news -- for me-- is that this
code is so awful I cannot use it in my next book as a bad example
because it is too proprietary! You could be famous!

Your code is so awful, you even use the "tbl-" prefixes to tell us you
have no idea about RDBMS! You keep converting dates to strings because
you are writing COBOL in SQL and want strings!

Why do your have "CREATE TABLE #tmp_sched" when view would work?
Answer: because magnetic tape files have to be materialized

Why do you spit on ISO-11179 rules and use a "tbl-" prefix? Because
you know only BASIC programming, which needs the prefixes for the one
pass compiler.

You write SQL with flags like it was 1950's Assembly language! Flags
in SQL!! Ghod Damn!! Varying length identifiers!? And I loved the way
spit on ANSI/ISO Standards with "SET QUOTED_IDENTIFIER OFF", etc.?

You need help you cannot get on a newsgroup.
Apr 12 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Nashat Wanly | last post by:
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET View products that this article applies to. This article was previously published under Q310070 For a Microsoft...
4
by: marc | last post by:
I've been developing a stored procedure that uses a user defined function in the query portion of the procedure. However, since the end product needs to allow for dynamic table names, the UDF will...
2
by: Dino L. | last post by:
How can I run stored procedure (MSSQL) ?
0
by: ccshine | last post by:
I'm working on an app that implements a Structure to store a recordset in an ArrayList. I used this setup to bind to a DataGrid and it worked out so well, I thought it might be a better solution...
9
by: fniles | last post by:
I am using VB.NET 2003 and SQL2000 database. I have a stored procedure called "INSERT_INTO_MYTABLE" that accepts 1 parameter (varchar(10)) and returns the identity column value from that table....
9
by: vikram.mankar | last post by:
I have a stored procedure thats transferring/processing data from one table to two different tables. The destination tables have a unique value constraint as the source tables at times has...
4
by: yin_n_yang74 | last post by:
I am new to SQL and SQL Server world. There must be a simple solution to this, but I'm not seeing it. I am trying to create a crystal report (v8.5) using a stored procedure from SQL Server...
12
by: Dooza | last post by:
I have a stored procedure that takes a number of inputs, does a bulk insert, and then outputs a recordset. When I run the stored procedure in Server Management Studio I also get a return value from...
7
by: jamesclose | last post by:
My problem is this (apologies if this is a little long ... hang in there): I can define a function in VB.NET with optional parameters that wraps a SQL procedure: Sub Test(Optional ByVal Arg1...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.