Stored Procedure with Increments

Please support our MS SQL advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2005
Posts: 27
Reputation: wchitamb is an unknown quantity at this point 
Solved Threads: 0
wchitamb wchitamb is offline Offline
Light Poster

Stored Procedure with Increments

 
0
  #1
25 Days Ago
I need some help I have managed to get this far (as below), from thatI can put a AAA_NUMBER and be able to get that record details and thats the 1st step that I wanted the 2nd step that I want is say if the DOCID is not available I then need the system to generate a new number (the tricky bit for me now is to have it do the increment since I want the system to 1st look at the current year since each and every year has a different sequence) so the YEAR_SEQUENCE table has CURRENT_YEAR, SEQUENCE, LAST_NUM Fields so it will be incrementing from the last generated number (+1 increment)

Any suggestions or twicks are welcome!

  1. CREATE PROCEDURE Sp_Show_Presc_Num
  2.  
  3. @AAA_NUMBER VARCHAR(7)
  4.  
  5. AS
  6.  
  7. SELECT
  8. <TABLE A>.Surname,
  9. <TABLE A>.Initials,
  10. <TABLE A>.AAA_NUMBER,
  11. <TABLE B>.DOCID,
  12. CONVERT(VARCHAR (10),<TABLE B>.ALLOCATIONDATE,103) AS ALLOCATIONDATE,
  13. <TABLE A>.Sex_Code
  14. FROM <TABLE A>
  15. LEFT JOIN <TABLE B> ON <TABLE B>.BBBNUMBER = <TABLE A>.AAA_NUMBER
  16. WHERE AAA_NUMBER=@AAA_NUMBER

EXECUTION EXAMPLE
---------------------------------------------------------------------USE [TEST DB]
EXEC [Sp_Show_Presc_Num] '4300058'
---------------------------------------------------------------------

MORE INFO
Some of the them are populated and the DocID should be displayed if available or else if its NULL then there should be an option for the user to have one generated

The are 3 table
<TABLE A> which will display these fields(Surname, Initials, AAA_NUMBER)
<TABLE B> which will display these fields(DOCID,ALLOCATIONDATE)
<TABLE C> which has these fields (@ the moment)(CURRENT_YEAR, SEQUENCE, LAST_NUM)

the ALLOCATIONDATE is populated when the DOCID is generated so its on that day
Last edited by peter_budo; 25 Days Ago at 7:30 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,187
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #2
24 Days Ago
I don't understand what you are asking. Can you post your table structures with sample on this thread (with code tags) and explain what you are trying to accomplish?


[code]
...code here...
[/code]
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 27
Reputation: wchitamb is an unknown quantity at this point 
Solved Threads: 0
wchitamb wchitamb is offline Offline
Light Poster
 
0
  #3
22 Days Ago
Right thanks, I m sorry if I wasn’t clear. I have 3 tables (structured as below) and all are in SQL Server. what I want to do is have a front-end like .Net or any other but the backend is SQL.

[The purpose is to get the DOCID on <TABLE B> and if its not there already then it has to be generated]
Now I want to have stored procedures that will do this job and what will be happening is a user will input the AAA_Number that will first be validated (seven numbers [1 – 9]) and if it’s a valid number it will be checked on both <TABLE A> and <TABLE B> [NB: AAA_Number = BBBNumber] and if there is a DOCID on <TABLE B> it will be displayed together with some info on <TABLE A>

Now if for some reason if the DOCID is not available then there should be an option to create/ generate one. Now on the new DOCID that will be created it will be based on the current year coz they have different sequences, (which implies they will be changing dependinding on which year they are created) but the general rule will be the +1 increment

So in short it’s a stored procedure that gets input to get a the DOCID + the other info, and if the DOCID is not existent then the other info [Surname,Initials,Sex] will be dispalayed but an option to generate one will be given, and the new DOCID will be based on the current year (the 1st one then everytime a new one is created the +1 increment is applied till the year is over and start again) the date that DOCID is generated will be recoded in the ALLOCATIONDATE field. Below are the tables. Hope I v managed to answer or write clearly wot I intent to do. [at the moment I m just working on the stored procedures not the frontend]

<TABLE A>
Surname
Initials
AAA_NUMBER

<TABLE B>
BBBNUMBER
DOCID
ALLOCATIONDATE

<TABLE C>
CURRENT_YEAR
Sequence
Last_Num

[i will post the table structure again trying to format it]
Last edited by wchitamb; 22 Days Ago at 6:12 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 27
Reputation: wchitamb is an unknown quantity at this point 
Solved Threads: 0
wchitamb wchitamb is offline Offline
Light Poster
 
0
  #4
22 Days Ago
In simple terms

If DocID on <TABLE B>
Select Surname, Initials, and Sex from <TABLE A>
Select DocID,AllocationDate from <TABLE B>

If DocID not on <TABLE B>
Create new DocID (insert on <TABLE B>)
Select Surname, Initials, and Sex from <TABLE A> (These will be displayed too)
The new DocID structure is controlled by the current year – since diff years have diff sequences
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,187
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
1
  #5
22 Days Ago
Are you wanting a condition result set or a case to handle this?

  1. IF EXISTS(SELECT * FROM TableB WHERE DocId = 12345)
  2. BEGIN
  3. PRINT 'Select Surname, Initials, and Sex from <TABLE A>'
  4. PRINT 'Select DocID,AllocationDate from <TABLE B>'
  5. END ELSE
  6. BEGIN
  7. PRINT 'Create new DocID (insert on <TABLE B>)'
  8. PRINT 'Select Surname, Initials, and Sex from <TABLE A> (These will be displayed too)'
  9. PRINT 'The new DocID structure is controlled by the current year – since diff years have diff sequences '
  10. END

>> Now if for some reason if the DOCID is not available then there should be an option to create/ generate one
I think you're mixing the database layer and user interface layer here. Options should be given at the application level for the most part unless you want to add a parameter to the sproc?
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 27
Reputation: wchitamb is an unknown quantity at this point 
Solved Threads: 0
wchitamb wchitamb is offline Offline
Light Poster
 
0
  #6
21 Days Ago
Thanks. Yeah! I would like to add a parameter to the stored proc, and also for now if possible I would like to be able to do the whole process of generating the DocIds without a frontend.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 27
Reputation: wchitamb is an unknown quantity at this point 
Solved Threads: 0
wchitamb wchitamb is offline Offline
Light Poster
 
0
  #7
20 Days Ago
Thanks sknake i have now managed to solve it. Once again Thanks
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the MS SQL Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC