473,406 Members | 2,843 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,406 software developers and data experts.

Creating a sustaining counter as in work orders - unique

31
Hi Guys,

Im still new to this SQL stuff and have a question about creating a counter that does not reset if I drop the temp table. Is this possible? I need to add new work order number (counter) to a daily orders table/view/procedure and I can make it work for one day but then when I drop the temp table, it resets the counter back. How can I keep the Max(orderno) going forward to the next day?

I am a little versed in stored procedures too. We are using SQL 2000 at the moment in the office.

Any ideas for this simple minded gal?

Thanks!
Jo
Aug 22 '07 #1
16 2305
ck9663
2,878 Expert 2GB
Hi Guys,

Im still new to this SQL stuff and have a question about creating a counter that does not reset if I drop the temp table. Is this possible? I need to add new work order number (counter) to a daily orders table/view/procedure and I can make it work for one day but then when I drop the temp table, it resets the counter back. How can I keep the Max(orderno) going forward to the next day?

I am a little versed in stored procedures too. We are using SQL 2000 at the moment in the office.

Any ideas for this simple minded gal?

Thanks!
Jo
try the IDENTITY column. it may not be sequential, but it's unique and it will not reset
Aug 22 '07 #2
Joell
31
try the IDENTITY column. it may not be sequential, but it's unique and it will not reset
Thank you so much for responding! I did try the Identity column but I dont know how to make it not reset when pulling the stored procedure tomorrow. Should I send you my code? Maybe I shouldnt create table? How can you put an identity column in a SELECT stmt?
Aug 22 '07 #3
ck9663
2,878 Expert 2GB
Thank you so much for responding! I did try the Identity column but I dont know how to make it not reset when pulling the stored procedure tomorrow. Should I send you my code? Maybe I shouldnt create table? How can you put an identity column in a SELECT stmt?
try sending your code... just those that will be needed
Aug 23 '07 #4
Joell
31
Hello Again,

Here is my cutdown code. There is a lot more to the Select stmt but I cut it down just to show you around it. I would like to put the end result into a procedure so that I can push it to crystal reports and my end users can then export to Excel. Maybe even schedule the proc to run as a DTS package?



create procedure @businessunit varchar(30), @datepulled datetime

as

create table #UPSDaily(
Location_no varchar(20) not null,
OrderNo int Identity(100000,1) not null
)

Insert into #UPSDaily

Select c_id_alpha Location_No,
-- Dont I need a placeholder here somehow for orderno from the creation of the table above?
from cust inner join rxrf on cust.c_id = rxrf.c_id
where rxrf.next_date = getdate()

-- then I need to keep the last value of the OrderNo for the next day's pull of data.

declare @intCounter int
select @intCounter = coalesce(max(orderno), 1) from #UPSDaily
declare @when datetime
set @when = getDate()

update #UPSDaily
set @intCounter = OrderNO = @intCounter + 1

drop table #UPSDaily -- but this removes my orderno value for the beginning of the next day.
--I need the next sequential number to start off the next day's pull of data.


Thanks so much for your help again! - JOELL
Aug 23 '07 #5
ck9663
2,878 Expert 2GB
Hello Again,

Here is my cutdown code. There is a lot more to the Select stmt but I cut it down just to show you around it. I would like to put the end result into a procedure so that I can push it to crystal reports and my end users can then export to Excel. Maybe even schedule the proc to run as a DTS package?



create procedure @businessunit varchar(30), @datepulled datetime

as

create table #UPSDaily(
Location_no varchar(20) not null,
OrderNo int Identity(100000,1) not null
)

Insert into #UPSDaily

Select c_id_alpha Location_No,
-- Dont I need a placeholder here somehow for orderno from the creation of the table above?
from cust inner join rxrf on cust.c_id = rxrf.c_id
where rxrf.next_date = getdate()

-- then I need to keep the last value of the OrderNo for the next day's pull of data.

declare @intCounter int
select @intCounter = coalesce(max(orderno), 1) from #UPSDaily
declare @when datetime
set @when = getDate()

update #UPSDaily
set @intCounter = OrderNO = @intCounter + 1

drop table #UPSDaily -- but this removes my orderno value for the beginning of the next day.
--I need the next sequential number to start off the next day's pull of data.


Thanks so much for your help again! - JOELL

by the looks of this, you're just getting the last OrderNo? coz you're dropping the table anyway...
Aug 23 '07 #6
Joell
31
by the looks of this, you're just getting the last OrderNo? coz you're dropping the table anyway...
Is there a better way to structure (maybe some kind of while loop) to run my select while grabbing the next incremental value? If so, how do I code that? Its just not working as is. I need a sequential number to restart over each day that the query runs. If I use a table, then doesnt that make the database larger when it is not necessary? All I am trying to do is create a seq number for the work orders each day and not duplicate any number. The seq number needs to be in a column of the select statement that I am running. Does that make better sense than before?

HELP!
Aug 28 '07 #7
Purple
404 Expert 256MB
Hi Jo,

do you have a field in the table with todays date in it ?

If you do, select the max date and compare with today, if the date part of today is bigger, reset your counter..

If not then post the table structure for the table and the temp table..

Regards Purple
Aug 28 '07 #8
Joell
31
Hi Jo,

do you have a field in the table with todays date in it ?

If you do, select the max date and compare with today, if the date part of today is bigger, reset your counter..

If not then post the table structure for the table and the temp table..

Regards Purple
I think I had a typo in my question. What I need to have is a counter that does NOT reset each day. So for my last order on 8/27 is 833230, then the first order on 8/28 should be 833231. Does that make better sense? sorry for all of my confusion.

I do not know how to write the code for it.

Help please.

Jo
Aug 28 '07 #9
Purple
404 Expert 256MB
Hi Jo,

am I missing something, cant you just use an auto increment field ?

Purple
Aug 28 '07 #10
Joell
31
Hi Jo,

am I missing something, cant you just use an auto increment field ?

Purple
That sounds logical but I dont know how to do that. I have tried the Identity counter but then if you use a temp table, it resets the next time you run the query. I dont want it to reset. I need unique values every time I run the query and to never reset. I also do not want to add tables to my database. I just need to pull existing data and add an sequential number that will not reset each day I run the query.

Isnt there another way? Maybe could you tell me about the auto increment?
Aug 28 '07 #11
Purple
404 Expert 256MB
Jo,

Why are you using a temp table ?

Purple
Aug 28 '07 #12
Joell
31
Jo,

Why are you using a temp table ?

Purple
I thought creating a temp table would be better than to have a table created for 11 separate business divisions of work orders that need to be pulled every day and imported into another system. Wouldnt it make the database very large in a small amount of time?

how else could I pull data from one system, attach a sequential number and then import it into another system? system = database
Aug 28 '07 #13
Purple
404 Expert 256MB
Hi Jo,

It is often difficult to analyse the problem when somewhat distant from the basic requirements. From what you have described, I think I would look again at the database structure.

Maybe add an int column with a foreign key to a business unit table and have all of the 11 business units work orders in one table,

I have no idea of what you perceive large is, MSSQL will be fine with multiple million rows in a table if it is appropriately indexed and running on a server with enough grunt.

If you use an autoincrement field for the work order number you will automatically get unique incremental work order numbers.

Does this help or have I missed the point ?

Regards Purple
Aug 28 '07 #14
Joell
31
Hi Jo,

It is often difficult to analyse the problem when somewhat distant from the basic requirements. From what you have described, I think I would look again at the database structure.

Maybe add an int column with a foreign key to a business unit table and have all of the 11 business units work orders in one table,

I have no idea of what you perceive large is, MSSQL will be fine with multiple million rows in a table if it is appropriately indexed and running on a server with enough grunt.

If you use an autoincrement field for the work order number you will automatically get unique incremental work order numbers.

Does this help or have I missed the point ?

Regards Purple
You are awesome and I appreciate your help. I am not very versed in SQL lingo and so defining the problem is a bit of a challenge for me.

I am using proprietary software and trying to pull from it into another package without incrementing the values in the proprietary software.

If I create a new table and add a int column, then on day 2, how do I get the counter to NOT reset? My logic is not such that I can remove the previous day's work orders yet so I will be building and building this table adding new work orders every day. Maybe a better question would be if I create a new table on day 1, how do I add to it on day 2, day 3, keeping the counter int going?

Jo
Aug 28 '07 #15
ck9663
2,878 Expert 2GB
You are awesome and I appreciate your help. I am not very versed in SQL lingo and so defining the problem is a bit of a challenge for me.

I am using proprietary software and trying to pull from it into another package without incrementing the values in the proprietary software.

If I create a new table and add a int column, then on day 2, how do I get the counter to NOT reset? My logic is not such that I can remove the previous day's work orders yet so I will be building and building this table adding new work orders every day. Maybe a better question would be if I create a new table on day 1, how do I add to it on day 2, day 3, keeping the counter int going?

Jo
Jo...

In order to keep this value, you have to store it somewhere. What do you with the daily tables? Do you have a one master table that contain it all? if you do, you can take the max(counter)+1 on that master table as the starting counter on your daily table. for this, you might need a trigger to handle the counter on your daily table. make sure that your counter is the PK on both table to ensure uniqueness.
Aug 29 '07 #16
Purple
404 Expert 256MB
Hi Jo,

I suggest you have a master workorder table which holds all of the work orders created for all of the business areas and this a permanent table not a temp table..

When you create the table workOrder add a field as the primary key and set it as an auto increment field, this will be the work order id.

Now when you insert rows into the table the work order id field is automatically incremented by one for every new row. (dont try to set a value for this field on the insert, if you want the numbers to start from a specific value, ie other than 1 specify a seed value)

Also create a field to represent the business unit as an int and use a join to the business unit table where you may have columns like

buId buName buContact etc...

I would also reiterate my suggestion to take some time out of the coding work to reconsider the database structure - Mary (one of the site administrators) has written this article which you may find helpful..

Regards Purple
Aug 29 '07 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Iain Miller | last post by:
Now this shouldn't be hard but I've been struggling on the best way as to how to do this one for a day or 3 so I thought I'd ask the assembled company..... I'm writing an application that tracks...
0
by: Mark Broadbent | last post by:
can someone help me? After creating an xml schema file in VS 2003, I drag and drop a table onto it to create its schema. However what I am getting is a document element encapsulating the actual...
0
by: Mark Broadbent | last post by:
(firstly sorry Ive cross posted this in C# forum also -I forgot to include this ng) can someone help me? After creating an xml schema file in VS 2003, I drag and drop a table onto it to create...
48
by: Jimmy | last post by:
thanks to everyone that helped, unfortunately the code samples people gave me don't work. here is what i have so far: <% Dim oConn, oRS, randNum Randomize() randNum = (CInt(1000 * Rnd) + 1) *...
6
by: lawpoop | last post by:
Hello! I am working on a map of a rather large php project that I've been working on. I hope to create a map of the files of the project that would look like the output of the unix 'tree'...
5
by: apartain | last post by:
My database is based on Work Order Numbers. If an employee enters labor, they must include the WO Number for it. Equipment, subcontractors, per diem and materials all must also have a WO number. ...
8
by: DaTurk | last post by:
Hi, I was just curious how you would go about creating a unique identifier with 3 ints.
12
by: badvoc | last post by:
Hi, I have had some good fortune on this site so I am back and I must iterate I am a beginer. I am having some problems getting to grips with the right technique to manage variables and...
3
by: DavidPr | last post by:
I made a website for a group of people and they want a visitor counter on it. I don't like hit counters because if the site is dead everyone will know it. But, it's their website. I did an...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.