473,767 Members | 1,579 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stored procedure Question

Plamen Ratchev helped me at this post:

http://groups.google.com/group/comp....716c65bc21a2f#

You mentioned this:
You can still keep all weekly data in one table, just need an extra
column to store the week start date. That will make the system a lot
more flexible (and you do not have to create a new table every week).
Right now you have to change your queries every week to reflect the
table name change.

How do I do that with this stored procedure?

SELECT t1.* into HoldingTable
FROM Diary9_19 t1
left JOIN Diary9_29 t2
ON t1.PAN=t2.PAN and t1.recno=t2.rec no
WHERE t2.PAN IS NULL

Insert into ClearDiaries
Select * from HoldingTable

Drop Table HoldingTable

I have to bring in a new table once a week and compare the two to see
if PAN is listed in the new table(Diary9_29 ) and not the old
one(Diary9_19). I ran the above procedure fine but now I'm ready to
bring in Diary10_3 and compare them. I don't want to have to change
my queries each time. How do I set up another column to store the
week start date which you suggest?

Oct 9 '08 #1
9 1742
You simply insert all values to the Diaries table, then run the DELETE
statement I posted. There is no need for the HoldingTable and all other
queries you posted.

--
Plamen Ratchev
http://www.SQLStudio.com
Oct 10 '08 #2
On Oct 10, 11:28*am, Plamen Ratchev <Pla...@SQLStud io.comwrote:
You simply insert all values to the Diaries table, then run the DELETE
statement I posted. There is no need for the HoldingTable and all other
queries you posted.

--
Plamen Ratchevhttp://www.SQLStudio.c om
Okay that did it but I worded it wrong as to what I want.

When I compare the two files I want the dups to be deleted and the
single occurrences to remain in the table. I'm looking for the
cleared items and if there are dups then they are not cleared. Single
occurrences are cleard so I want them to be placed in the Diaries
table. So I should have Pan 1 Pan 3, and Pan 4 only in the Diaries
table as Pan 2 had duplicates. I hope this makes sense.
Oct 10 '08 #3
Then simply use this delete statement instead of the other one:

WITH Dups
AS
(SELECT week_start_date , pan,
COUNT(*) OVER(PARTITION BY pan) AS cnt
FROM Diaries)
DELETE Dups
WHERE cnt 1;

--
Plamen Ratchev
http://www.SQLStudio.com
Oct 10 '08 #4
On Oct 10, 11:42*am, Plamen Ratchev <Pla...@SQLStud io.comwrote:
Then simply use this delete statement instead of the other one:

WITH Dups
AS
(SELECT week_start_date , pan,
* * * * *COUNT(*) OVER(PARTITION BY pan) AS cnt
* FROM Diaries)
DELETE Dups
WHERE cnt 1;

--
Plamen Ratchevhttp://www.SQLStudio.c om
PERFECT!!!! Thanks so much. Can you explain the last section so I
can understand what you had me to do. Thanks so much you've taught me
a lot.
Oct 10 '08 #5
Here are some inline comments to the significant parts of the query:

-- define common table expression (CTE) using the WITH keyword,
-- which is pretty much like derived table but more powerful
WITH Dups
AS
(SELECT week_start_date , pan,
-- use aggregate function with the OVER clause
-- this allows to apply count over a partition/group
-- in this case will provide counts for each 'pan' value
-- note the PARTITION BY section that lists which column
-- will define the partition
COUNT(*) OVER(PARTITION BY pan) AS cnt
FROM Diaries)
DELETE Dups
-- delete only those rows that have count 1
-- since count was per 'pan' value it will remove
-- those that have any duplicate rows
WHERE cnt 1;

Note that the DELETE statement has to follow the CTE definition:

WITH
AS
(SELECT ...)
DELETE ...

The query that consumes the CTE has to be immediately after the CTE
definition (and it could be SELECT/INSERT/DELETE/UPDATE query).

--
Plamen Ratchev
http://www.SQLStudio.com
Oct 10 '08 #6
On Oct 10, 12:40*pm, Plamen Ratchev <Pla...@SQLStud io.comwrote:
Here are some inline comments to the significant parts of the query:

-- define common table expression (CTE) using the WITH keyword,
-- which is pretty much like derived table but more powerful
WITH Dups
AS
(SELECT week_start_date , pan,
* * * * *-- use aggregate function with the OVER clause
* * * * *-- this allows to apply count over a partition/group
* * * * *-- in this case will provide counts for each 'pan' value
* * * * *-- note the PARTITION BY section that lists which column
* * * * *-- will define the partition
* * * * *COUNT(*) OVER(PARTITION BY pan) AS cnt
* FROM Diaries)
DELETE Dups
-- delete only those rows that have count 1
-- since count was per 'pan' value it will remove
-- those that have any duplicate rows
WHERE cnt 1;

Note that the DELETE statement has to follow the CTE definition:

WITH
AS
(SELECT ...)
DELETE ...

The query that consumes the CTE has to be immediately after the CTE
definition (and it could be SELECT/INSERT/DELETE/UPDATE query).

--
Plamen Ratchevhttp://www.SQLStudio.c om
Thanks so much Plamen for this explaination. Like I said you've
taught me a lot. I'm sure I'll be back again!
Oct 10 '08 #7
I really do not understand what you are trying to do. It will be best to
post your complete table structures, insert statements to populate
sample data, and describe what are the desired results.

The error message should tell you what is the invalid column name. Seems
to me the first query will not accomplish much as you are grouping on
all columns. Plus not sure if you already have added the week_start_date
column which is essential for it to work.

You do not have to list any columns in the delete statement, it can be
as simple as this:

WITH Dups
AS
(SELECT COUNT(*) OVER(PARTITION BY pan) AS cnt
FROM ClearedDiaries)
DELETE Dups
WHERE cnt 1;

--
Plamen Ratchev
http://www.SQLStudio.com
Oct 10 '08 #8
One more note - perhaps you do not even need a delete statement to clean
data. I am not sure how you get the data loads, but assuming you have
some input staging table, you can insert only rows with 'pan' values
that do not exists in the target table:

INSERT INTO Diaries
SELECT ...
FROM Staging AS S
WHERE NOT EXISTS(SELECT *
FROM Diaries AS D
WHERE D.pan = S.pan);

--
Plamen Ratchev
http://www.SQLStudio.com
Oct 10 '08 #9
On Oct 10, 2:55*pm, Plamen Ratchev <Pla...@SQLStud io.comwrote:
I really do not understand what you are trying to do. It will be best to
post your complete table structures, insert statements to populate
sample data, and describe what are the desired results.

The error message should tell you what is the invalid column name. Seems
to me the first query will not accomplish much as you are grouping on
all columns. Plus not sure if you already have added the week_start_date
column which is essential for it to work.

You do not have to list any columns in the delete statement, it can be
as simple as this:

WITH Dups
AS
(SELECT COUNT(*) OVER(PARTITION BY pan) AS cnt
* FROM ClearedDiaries)
DELETE Dups
WHERE cnt 1;

--
Plamen Ratchevhttp://www.SQLStudio.c om
This is what I have to do...I have to compare last week's diary with
this week's diary to get PAN's that are cleared. If PAN is in last
week and not in this week then it's cleared and should go into
ClearedDiaries. I am bringing in the Flat File via SSIS then I will
add this stored procedure you helped me with to the Execute SQL Task
in SSIS. Here's the table structure. I didn't put it out there
because there's so many fields.

I first did a select * but got this:

Column 'diaries.week_s tart_date' is invalid in the select list because
it is not contained in either an aggregate function or the GROUP BY
clause.

So I thought I had to add all of the fields?

RGN varchar(2)
AREA varchar(3)
DIST varchar(3)
DOC varchar(3)
TSC varchar(3)
HUN varchar(9)
RECNO varchar(2)
PAN varchar(9)
Fname varchar(15)
Lname varchar(20)
Person-Type varchar(1)
Redet-Ind varchar(1)
OverPay-Ind varchar(1)
OverPay-Amt varchar(8)
Curstat varchar(3)
Appeal varchar(1)
GKC varchar(1)
Redlowpro varchar(1)
Profile varchar(1)
GK-Status varchar(1)
ARC varchar(1)
OPST varchar(1)
OPSC varchar(1)
OPSND varchar(8)
OPSNT varchar(1)
Filler varchar(8)
IN-CT varchar(2)
DryCd1 varchar(2)
DryDte1 varchar(8)
DryDte1 varchar(1)
DryCd2 varchar(2)
DryDte2 varchar(8)
DryDte2 varchar(1)
DryCd1 varchar(2)
DryDte1 varchar(8)
DryDte1 varchar(1)
DryCd1 varchar(2)
DryDte1 varchar(8)
DryDte1 varchar(1)
DryCd1 varchar(2)
DryDte1 varchar(8)
DryDte1 varchar(1)
DryCd1 varchar(2)
DryDte1 varchar(8)
DryDte1 varchar(1)
DryCd1 varchar(2)
DryDte1 varchar(8)
DryDte1 varchar(1)
DryCd1 varchar(2)
DryDte1 varchar(8)
DryDte1 varchar(1)
DryCd1 varchar(2)
DryDte1 varchar(8)
DryDte1 varchar(1)
DryCd1 varchar(2)
DryDte1 varchar(8)
DryDte1 varchar(1)
DryCd1 varchar(2)
DryDte1 varchar(8)
DryDte1 varchar(1)
DryCd1 varchar(2)
DryDte1 varchar(8)
DryDte1 varchar(1)
DryCd1 varchar(2)
DryDte1 varchar(8)
DryDte1 varchar(1)
DryCd1 varchar(2)
DryDte1 varchar(8)
DryDte1 varchar(1)
DryCd1 varchar(2)
DryDte1 varchar(8)
DryDte1 varchar(1)
DryCd1 varchar(2)
DryDte1 varchar(8)
DryDte1 varchar(1)
DryCd1 varchar(2)
DryDte1 varchar(8)
DryDte1 varchar(1)
OP-Juris varchar(1)
Redtype varchar(1)
Out_DryDte1 datetime
Out_DryDte2 datetime
Out_DryDte3 datetime
Out_DryDte4 datetime
Out_DryDte5 datetime
Out_DryDte6 datetime
Out_DryDte6 datetime
Out_DryDte8 datetime
Oct 10 '08 #10

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

Similar topics

3
2485
by: Ryan.Chowdhury | last post by:
This is a general question regarding the use of view and stored procedures. I'm fairly new to databases and SQL. I've created a SQL database using an Access Data Project ("ADP") and I'm satified with the table structure. I've moved on to building some front ends for our users. I'm running into situations where I want subreports to be built from queries that are dependent on the values in other controls. I've played with stored...
1
2573
by: Raquel | last post by:
Have a question on the Stored procedure method code generated by DB2 development center for Java stored procedures. Suppose I have a requirement to return the resultset consisting of FIRSTNME, LASTNAME of all employees belonging to a particular department, following is the stored procedure method code generated by "DB2 development center": public static void sproc3mtd ( String workdept, String firstnme,
8
7944
by: Thomasb | last post by:
With a background in MS SQL Server programming I'm used to temporary tables. Have just started to work with DB2 ver 7 on z/OS and stumbled into the concept of GLOBAL TEMPORARY TABLE. I have created a temporary database with a tables space. Verified that DECLARE GLOBAL TEMPORARY TABLE TEMP (A INTEGER); INSERT INTO SESSION.TEMP VALUES(10); SELECT A FROM SESSION.TEMP; works from a query tool.
2
9244
by: Kent Lewandowski | last post by:
hi all, Recently I wrote some stored procedures using java jdbc code (admittedly my first stab) and then tried to implement the same within java packages (for code reuse). I encountered problems doing this. I wanted to implemented a generic "Helper" class like this: /** * Helper
5
2141
by: Rhino | last post by:
This question relates to DB2 Version 6 on OS/390. Can a (COBOL) stored procedure on this platform do file I/O, i.e. write to a sequential file? I am trying to debug a stored procedure. As far as I know, DB2 stored procedures cannot do terminal I/O on any operating system but I know that (Java) stored procedures in Windows/Linux/Unix can write to files and I have done this many times.
5
1727
by: Andy G | last post by:
I have a registration page that captures 75% of the users data. After they enter that info they are redirected to one of two pages depending on how they answered a question on the registation page. My question is would it be better to store all of the fields(15 or so) from the first page in session variables and reference them from the next page and run ONE stored procedure OR would it be better to run a stored procedure the first page...
2
1434
by: singlal | last post by:
Hi, my question was not getting any attention because it moved to 2nd page; so posting it again. Sorry for any inconvenience but I need to get it resolved fast. Need your help! **************************************************************************************************** Original Question: -------------------- Has anyone called a COBOL subroutine using COBOL CALL from a COBOL/DB2
7
9715
by: Siv | last post by:
Hi, I have a stored procedure that I want to execute and then wait in a loop showing a timer whilst it completes and then carry on once I get notification that it has completed. The main reason for this being to stop the user thinking the application has frozen when in fact it is just waiting for a long SP to complete. Another reason for doing it like this is that I also have had a problem in the past where the SP takes longer than the...
5
2361
by: ric_deez | last post by:
Hi there, I would like to create a simple search form to allow users to search for a job number based on a number of parameters. I think I understand how to use parameteres associated with Stored Procedures with a data reader to add various parameters. However, if I have a stored procedure such as CREATE usp_SelectfromJobNumbers (@par1 datatype, @par2 datatype, @par3 datatype)
11
3444
by: peter | last post by:
I am trying to get a SQL stored procedure to use user maintained MQT implicitly which raises questions on when they are used or not used. In theory you would expect the stored procedure to pick up the MQT at the time it is bound on the creation of the static SQL. This raises the question on how you stop it or start it using a MQT as there is no option on the bind. What happens when it is rebound? What happens if the plan is made invalid...
0
9575
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9960
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9841
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8840
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6656
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5280
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3534
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2808
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.