SQL To Generate Incrementing Values?

Jul 31, 2007

Hi. Is there such thing as a way to write SQL to generate a column of increasing integers?

Expression1
-----------
1
2
3
4
5
6
7
8
9
10
11

View 6 Replies


ADVERTISEMENT

Auto Incrementing-&>Updating Values In Two Related Tables :Help!!!

Nov 11, 2005

hi there,
 i am new to sql server database.i am doing small projects right
now using asp.net and sql to create webpages (very basic webpages)
My problem is:

Problem :
 
   i have two tables .....table 1 and table 2.
                   
                   
                   
    Table 1 has following fields: studentid,student name,student address.
                                                               
Table 2 has following fields:studentid and course .
  table1 student id is the primary key refrencing table 2 student id.
Now i delete a record in table 1 which will in turn also get deleted in
table 2 . so for eg  if i have three records 1 ,2 and 3 ....then i
delete 2 in table 1 ...i will have 1 and 3 in both table 1 and table
2....now i want 3 to become 2 in both table 1 and table 2...so that i
dont have empty space between two student id's 1 and 3. so this is my
problem....if any one can help me out with suggestions please
do.  
                                                                                 
thank you all........
                                                                                                           
     ahmed_ind

View 4 Replies View Related

Generate Values Between A Starting And Stopping Point (time Values).

Apr 23, 2007

[RS 2005]

Given the starting and stopping points (time values), how do I generate values between these points.

For example, if I have 08 (representing Hour) as a starting point and 12 as a stopping point.

From this I would like to generate a data sequence like 08, 09, 10, 11, and 12.

So how do I accomplish this? In SQL or in the RS?

The only thing I can think of is using a WHILE loop and a temporary table in SQL (not to keen on doing this).



//HÃ¥kan

View 7 Replies View Related

Incrementing And Concatentating Non-Auto Incrementing Fields

Mar 11, 2008

My SQL table has the
following columns:













ID – Auto IncrementingNumber_of_Records – IntAccount_Number – Varchar
(Format 1234)Account_Number_Instance  – Varchar 
(Format e.g. 1234-01)Other_Field1Other_Field2…etc.



When the Account_Number was
initially inserted into the table, a Stored procedure (not written be me)  was used to concatenate the Account_Number
and Number_of_Records.  As a result
the  Account_Number_Instance became,
e.g. 1234-01, etc.



From the ASPX form we
retrieve each of the fields above, and based on the Number_of_Records an
appropriate number of records were inserted for the Account_Number. 

 

For Example:

 



Account_Number = 12345Number_of_Records = 4



The result inserted into the
table were as follows:



ID        # of Records            Acct_Number  
New_Num            Acct_Number_Instance   â€¦

1          4                                              12345                                     12345-01

2          4                                              12345                                     12345-02

3          4                                              12345                                     12345-03

4          4                                              12345                                     12345-04

 

Now, I would like to UPDATE
this table, and based on the original Number_of_ Records (4) I want to ADD, for
Account_Number (1234) add an appropriate Number_of_Instances, beginning with
the next incremental number and adding what the New Number of Records input is…




For Example:





            Account_Number = 12345            New_Num(ber_Of Records) = 3

ID        # of Records              Acct_Number    New_Num             Acct_Number_Instance   …

1          4                                              12345                                     12345-01

2          4                                              12345                                     12345-02

3          4                                              12345                                     12345-03

4          4                                              12345                                     12345-04

5                                                         12345             3                      12345-05

6                                                         12345             3                      12345-06

7                                                         12345             3                      12345-07

 

I
do realize that the next time I have to update this Account_Number I am also
going to have to somehow ADD only one of the #_of_Records and New_Num for the
Acct_Number instances to obtain the next starting number, which in the example
above would be (8)…



Any
and all suggestions on how to accomplish this would be greatly appreciated!

Thanks

View 2 Replies View Related

SQL 2012 :: Generate Unique Values

Apr 23, 2014

I am working on an ASP.net web application which inserts new record into an underlying table.

It is actually a ConfirmationNumber and should be unique. I have used abs(checksum(newid()))

For this purpose. Is there a better way to accomplice this?

View 9 Replies View Related

How Can I Add A Unique Key Column To A Table And Generate Its Values Automatically?

May 2, 2006

Hi, all,

I have a question about adding a unique key column to an existing table.

what i trying to do is that: I have already created a table, now i wanna add a ID column to this table, and generate the values for ID column from 1 to the existing row number. How can I get this done?

Thanks a lot in advance for any guidance.

View 6 Replies View Related

SQL Server 2012 :: Generate Months Based On Previous Values

Jul 7, 2015

I have a data that with month values ranging from jan 2012 till july 2013 with some values associated with it.

I want to generate months automatically after july 2013 till december 2013 in sql something like the below one:

Is there a way in sql to do this?

View 2 Replies View Related

How To Not Auto Generate A Report, How To Use A Null Checkbox On A Param With Available Values, How To Add Back/forward Buttons?

Apr 3, 2008

Hey all,

1) I have a report with many parameters that I want users to be able to pick from. Allow them to pick 1, many or all to build their report dynamically. I'm all set on the TSQL side, but on the Reporting Services side I have to allow each parameter to be null with a default of NULL. In by doing this, the report will auto run, which I do not want to happen. The only resolution I've found thus far was by adding a parameter that does nothing, with a NULL default value. Yet It sticks out like a sore thumb on the report and I want to get rid of it. If I check in "Hidden" in the parameter options, my report errors out stating that the parameter requires a value.

2) Is it possible to have a parameter that has available values from a dataset have a NULL checkbox like those of parameters that do not have available values?


3) Is it possible to add back/forward buttons inside of a report instead of just at the report header by default?


Thanks!

View 8 Replies View Related

Incrementing

Nov 21, 2007

Hi,

I currently have a table which has around 60000 records, within the table there is a field which is acting as the Primary but the numbers are not in consecutive order any more (due to deletion), and i need a SQL Script that will start at the beginning of the records and UPDATE (STK_LOCATION) and SET the field (LOC_PRIMARY) to 1 on the first record and then go through the rest of the table setting the same field to +1 of the record before it.

Any help and advice will be very grateful

Thanks in advance

View 3 Replies View Related

Incrementing A Value

Apr 27, 2007

I have just started using SSIS this week. I am experienced with a different ETL tool (DataStage).



What I am trying to accomplish is the following:

Read the MaxValue of a Key from a Table and match to a stream of data using a LookUp stage.
Increment the value of each row for insertion to the table where I obtained the MaxValue.

What would seem to be a very simple procedure (I could knock this out in 10 minutes with DS...of course I have more experience with that toolset) is problematic for me. I would just use a Transform stage and add the RowNumber variable to the MaxValue for each row. The stream would be ready for Insertion to the target table.



I am currently trying to use a Script Component in the Data Flow. Since my VB is rusty, does anyone have a script for incrementing a ReadWrite Variable for insertion to the stream? All I want to do is take the MaxValue and add the RowNumber or increment by 1 each time the script is performed.



If anyone has a simpler method, please let me know. Also, before it starts, yes we are using the Identity feature on the table, however due to processing constraints we need to preassign the Key in our Data Flow. Hence the incrementing of rows....



Cheers.

View 3 Replies View Related

Incrementing Id Field

Mar 21, 2004

Hi

Does anyone know why an error occurs (constraints violated) when I try to have a table with an automated id incrementing by 1. I am uploading the data into the table from dts, so ignore the id field in the dts transformation.

Any ideas or help would really be appreciated, do I need to reference the id field in an activeX script?

Thanks

View 9 Replies View Related

Incrementing Primary Key

May 13, 2004

hi am new in this forum and am designind a database in SQLserver and i want to make the primary key auto incremented
can anyone help

View 1 Replies View Related

Incrementing Primary Key

May 13, 2004

hi am new in this forum and am designind a database in SQLserver and i want to make the primary key auto incremented
can anyone help

View 2 Replies View Related

Incrementing A Field In SQL

Jun 29, 2004

Hello,
I am making adjustments to a table in my SQL Server Database.
I'm adding a collumn to my database of numeric type.
I wish to write a SQL script that will go through every record in the database and give it a unique number (key) by simply incrementing each number by 1.
I've inherited this database and as you have probably guessed i'm trying to add a primary key to this table as currently there isn't one!

I have no experience what so ever with T-SQL (which I presume i will need to use) as my expertise lie in web development with fairly simple SQL so go easy on me :)

Thanks in advance for your help

View 4 Replies View Related

Incrementing An Id Field

Mar 21, 2004

Hi

Does anyone know why an error occurs (constraints violated) when I try to have a table with an automated id incrementing by 1. I am uploading the data into the table from dts, so ignore the id field in the dts transformation.

Any ideas or help would really be appreciated, do I need to reference the id field in an activeX script?

Thanks

View 3 Replies View Related

Incrementing Value Query

Sep 23, 2014

I have two columns in a table right now. My query is inserting data into this table from another so that non technical users can view this database value via excel. One column shows the date and the other shows a numeric value. This value grows daily and is used to determine when a reseed of the database is needed. I would like to add a third column to this table that shows the difference in growth. For example if 9/19 had a value of 1000 and 9/20 had a value of 1100, the third column would have a value of 100. My plan is to create a column in excel that calculates the number of days until the reseed is required based on average growth. It would be much easier to get this difference in growth from the database than through an excel formula.

View 4 Replies View Related

Incrementing Column

Feb 28, 2008

Hi,

Im not sure if this can/should be done through sql...but i have a sql statement and i need the first column returned to start at 1 - and then increment for each row.

I am then using the returned values in another app.

I tried using count(columnname) but this just stays at 1 for each row.

Thanks for an feedback...

View 5 Replies View Related

Auto Incrementing

Nov 14, 2007

Hi

I'm using a insert into fuction kind of like below

INSERT INTO table1(

col1
col2
col3
col4
)
SELECT

col2
col3
col4

FROM ...............

I'd like to auto-increment col1 so it counts how many rows are being added


any help is very appreciated

View 5 Replies View Related

How To Set Up Auto Incrementing For A Field

Aug 10, 2006

 
I have a database table. I need to set a particular data field to allow for Auto Incrementing. Basically I need to set up the initial integer value to start at. Then to specify the amount to increment by.
What I have done so far is to right click on the field name in SQL Server 2005. Then, on the pop-up menu I select the menu option for properties.
When the properties are displayed I see some fields listed in dark grey but I am not able to edit these fields. In other words, if I try to type text in this field, nothing happens. The fields I am talking about are:
Identity Increment
Identity Seed
Is Identity
Does anyone know what the problem is? What do I have to do to edit these fields.
 

View 2 Replies View Related

Question About Incrementing A Field

Jul 17, 2005

Hi everyone. I'm a new user to ASP.NET using VB.NET and SQL Server.

I have one question.

I am currently creating a website for my dissertation whereby whenever a
customer purchases a product, a number is incremented within the
'Product' table. The column that needs to be incremented is called
'ProductSold'.

I have got an Insert query to store the orders in the 'Orders' table
already within my webpage. Is there a SQL Query that I could use to
increment the number in the 'Product' Table straight after my Insert
Query?

I really appreciate if anyone could help me in this matter!

View 4 Replies View Related

ID Field Not Incrementing Correctly

Sep 14, 2001

I have a field called contact_id in a table. Datatype is integer and has an identity seed of 1 and an identity increment of 1. When new records are created, the increment sometimes skips numbers. (ex. 12,13,18,21). What's going on here? Am I losing records somewhere?

Also, this is a cold fusion application. Thanks!!

View 1 Replies View Related

ID Field Not Incrementing Correctly (again)

Sep 17, 2001

I have a field called contact_id in a table. Datatype is integer and has an identity seed of 1 and an identity increment of 1. When new records are created, the increment sometimes skips numbers. (ex. 12,13,18,21). There is absolutely no deletion of any kind going on. (i.e. My application (cold fusion) has no code whatsoever that is deleting records.) What's going on here? Am I losing records somewhere?

View 1 Replies View Related

How Do I Update A Column To Be Incrementing Each Row By 1

Apr 2, 2008

I have a temp tbl and in col1 row1 i would like to have 1, col1 row2 value 2, col1 row3 value 3 and so on. how can i do this?

View 3 Replies View Related

Unwanted Auto-incrementing

Aug 17, 2006

Here is a simplified version of my problem:
I am inserting data into a table using a stored procedure. The table has an identity column that increments with each insert. When I use erroneous data in the other fields the insert fails….no surprises there! But when the next insert occurs with valid data I find that my identity field has increased even with those inserts that failed, so my sequence has jumped a few numbers. How do I get the identity inserts to roll back if the rest of the data in a row doesn’t insert successfully?
Marcha x

View 9 Replies View Related

Auto-incrementing A Field

Nov 9, 2006

I am porting a database from Informix to SQL Server 2000. Several fields in the informix database are specified as SERIAL, which means that they auto-increment auto-magically if you insert a 0 into them. Well...I was looking for an equivalent in SQL Server 2000, and thought I had found it in the IDENTITY column concept.

With further investigation into IDENTITY columns, however, I discovered that only one column per table can be given the IDENTITY property. Why is this? Is there a way around that? If not, is there another way to achieve this feature in SQL Server 2000? If not, any suggestions on how I can do it?

Thanks!

Lisa

View 5 Replies View Related

Build ID W/Incrementing Digit

Jul 23, 2005

Help, I'm stuck! I have a Customer table:ID Name-- ---------------------1234 Christopher's Surf Boards4321 Christina's NailsI have to build a Account table:ID GPID-- --------------------1234 CHRIST004321 CHRIST01I've built a function that gives me the six alphas and concatenates thetwo digits. The GPID in the account table is the first six alphas ofthe company name plus two digits to ensure uniqueness. (Don't ask why Ican't use the existing id -- the story is long and boring.) I built afunction that creates the alpha part and assigns the first 00. I builta second function that I thought would check the Account table as I wasinserting and increment the numbers if the account number alreadyexisted. It doesn't work. In the above table, both IDs come out asCHRIST00. Here's the second function:ALTER FUNCTION fnValidateID(@mString varchar(15))RETURNS varchar(15)ASBEGINDECLARE@sTemp varchar(15),@sInc varchar(2),@iInc intBEGINset @sTemp = @mStringwhile exists (select GPID from dbo.tempID where GPID = @sTemp)beginset @iInc = convert(int,right(@mString,2)) + 1set @sInc = convert(varchar(2),@iInc)if len(@sInc) < 2 set @sInc = '0' + @sIncset @sTemp = substring(@sTemp,1,len(@sTemp) - 2) + @sIncend--NEXTENDRETURN @sTempENDMy first guess is that the code is okay, so far as it goes, but that Ican't check the values in the table at the same time I'm inserting intoit. (The Account table is empty, initially.) In any case, I can'tfigure out where I've gone wrong.Thanks.

View 8 Replies View Related

Incrementing Within A Group/Subset

Oct 6, 2006

I could do this even with my limited knowledge of SQL but I'm surethere's a slick way that might be dead easy. My way hardly seems worththe effort . If not thanks anywayCol1Col2Col3QWQWQWQAQBQBQXWQXWQXWQXAQXBQXBEvery time there is a change of group (Col1 and Col2), I want to startand increment Col3 so I end up with;Col1Col2Col3QW001QW002QW003QA001QB001QB002QXW001QXW002QXW003QXA001QXB001QXB002

View 1 Replies View Related

Incrementing Value And CONSTRAINT Issue

Nov 15, 2007



Very new to SQL sorry.

What if we needed to enforce a constraint of "01" and not just a "1" single digit. It's more than a display issue. I need the value to always be two digits regardless of value.

View 5 Replies View Related

Problem With Incrementing Variable

May 2, 2006

I want create a Incrementing row through a variable in Derived Column Transmition,For that i created a variable called 'Increment'..in the derived column iam creating a new column with this expression

@[User::INCREMENT] == @[User::INCREMENT] + 1

Is anything wrong with this statement..bcoz iam getting 0 or the value which i assigned (1000)to all the rows.

Can you pls suggest some solution ?


Niru

View 4 Replies View Related

Incrementing Rows Manually

Aug 28, 2007



Hi,

I have a table



Code Snippet
CustomerDetail
customerid
customername
status
app_no
deskno





Now everytime a new customer is created, i allocate the deskno. The logic I use is




Code Snippet
create proc spoc_incrementdesk
as
DECLARE @deskno int

select @deskno = (SELECT max(deskno) from customerdetail)

Insert into customers(1,'rick',1,23232,@deskno)




If there are already records present in the customer table, it works well. However if the very first record is getting inserted it inserts null in the deskno because when no records are present (max(deskno) = null).

At time time of insertion, how do i check if @deskno = null and allot it 1.

Vidkshi

View 4 Replies View Related

Incrementing An Invoice Number

May 2, 2008

Hi there

I need a bit of generic advice about a project I am currently working on which is slightly out of my comfort zone. I€™m using SQL 2005 and VB.Net to develop an application that produces invoices for a number of different countries. The issue is that the data is arriving from an AS400 system so the tables I am working from are a little strange! This results in a fair degree of work tweaking the data into a format that is useful for my application.

To simplify the explanation each invoice in its basic form consists of the invoice data and the line items (so there is nothing new here). What is unusual is that the invoice data has an invoice number but then an additional invoice number specific to the country it is destined for. The initial invoice number arrives with the data from the AS400 but the secondary number is generated according to a number of business rules.

I have written an update query in a stored procedure which updates the invoice data with a number of values from a secondary table and a number of calculated values. There are several functions, one in particular which looks up the new invoice number from a secondary table based on specific criteria (country of destination being one of them) and I had intended to write an update query, called from within this function which then increments the invoice number but I have since found out that you cant do an update/delete from a function?

I can€™t add an update query to increment the value after the initial update query as this results in the stored procedure running through and adding all the invoice numbers (which are the same) and then the number being incremented at the end.

I assume I can€™t call an update query from within the initial update query so that the increment takes place after each loop of the initial update (if I can what would the syntax be?).

I have tried writing a trigger which completes the increment when the invoice data is updated. I haven€™t used triggers before but once again it appears as if the first update query is running through and completing all the updates and then the trigger fires incrementing the number. Or have I just got this wrong?

I guess what I need is a trigger which fires after each row of the table is updated which was how I assumed it should work!

Ironically I could write this in VB.Net with ease but for speed we need it within the database.

What is the best way to increment my invoice number?

I€™m sorry of this is general in nature but im really looking for some advice as to the best approach to deal with this issue.

Martyn Fewtrell

View 19 Replies View Related

Identity Value Not Auto Incrementing

Feb 19, 2008

My Identity value column is not autoincrementing, its seed is set to 1, and increment set to 1.
yet when i click the auto generated 'Add New Record' button in the BindingNavigator control set (in a form where it is populated with databound text boxes), this Identity column is not auto incremented?


And if i try to write a value to this column, i get the error telling me this column cannot be written to. Yet if i do not write to it, then i get "This column does not allow nulls"

To load my data i am using:




Code Snippet
'clear dataset
Me.DsQuote.Clear()
'fill the datagrid with data from tblQuoteID
Me.TblQuoteIDTableAdapter.Fill(Me.DsQuote.tblQuoteID)






and to save data im using:




Code Snippet
Me.Validate()
'end data edit
Me.TblQuoteIDBindingSource.EndEdit()
'update the dataset with table data
Me.TblQuoteIDTableAdapter.Update(DsQuote.tblQuoteID)


anything im doing wrong? this data is being viewed in a datagrid btw.

cheers

View 1 Replies View Related

Incrementing Based On Condition

Aug 16, 2007

Hi,

I am facing a challenge and hope some one can help me with the query.


I have a school. School have classrooms. Classrooms are divided into various sections (Section A, Section B and so on) . Sections have subsections. Every student is allocated a rollnumber in that section.(Subsection is just for dividng the sections. it has no other use.) Now the student is given a choice to specify his own roll( DesiredRoll) in that section. If two children select the same rollno, then the system internally allocates a trackingno.(There can be multiple roll no as these are allocated manually by admin). Let me demonstrate this:


So when the first entry is made in the db, and let us say that the section to be allocated is A, RollNo is 1 and the DesiredRoll is 1

Student 1 Section A Subsection 1 Roll No 1 DesiredRoll 1 TrackingNo 0


The second entry is made in the db and let us say the section to be allocated is A, RollNo is 2 and the DesiredRoll is 2

Student 2 Section A Subsection 1 Roll No 2 DesiredRoll 2 TrackingNo 0


Now let us say there is a 3rd entry the section to be allocated is A, RollNo is 3 but the DesiredRoll is 1. Now since the DesiredRoll has already been taken, we will allocate the DesiredRoll 1, however now the trackingNo will be 1


Let us say there is a 4th entry is made, the section to be allocated is A, RollNo is 4 but the DesiredRoll is again 1. Now since the DesiredRoll has already been taken, we will allocate the DesiredRoll 1, however now the trackingNo will be 2


Similarly this logic will work for different sections. How will I write a query so that I can detect this scenario and increment the tracking no or allocate a tracking no of 0 if there is a new entry made in that section


The structure of the table is as follows:


IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Student]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Student](
[RID] [int] NOT NULL,
[Class] [int] NULL,
[Section] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[SubSection] [int] NULL,
[RollNo] [int] NULL,
[DesiredRoll] [int] NULL,
[TrackingNo] [int] NULL
)
END
GO
INSERT [dbo].[Student] ([RID], [Class], [Section], [SubSection], [RollNo], [DesiredRoll], [TrackingNo]) VALUES (1, 1, N'A', 1, 1, 1, 0)
INSERT [dbo].[Student] ([RID], [Class], [Section], [SubSection], [RollNo], [DesiredRoll], [TrackingNo]) VALUES (2, 1, N'A', 1, 2, 2, 0)
INSERT [dbo].[Student] ([RID], [Class], [Section], [SubSection], [RollNo], [DesiredRoll], [TrackingNo]) VALUES (3, 1, N'A', 1, 3, 1, 1)
INSERT [dbo].[Student] ([RID], [Class], [Section], [SubSection], [RollNo], [DesiredRoll], [TrackingNo]) VALUES (4, 1, N'A', 1, 4, 1, 2)
INSERT [dbo].[Student] ([RID], [Class], [Section], [SubSection], [RollNo], [DesiredRoll], [TrackingNo]) VALUES (5, 1, N'A', 12, 3, 0, 0)
INSERT [dbo].[Student] ([RID], [Class], [Section], [SubSection], [RollNo], [DesiredRoll], [TrackingNo]) VALUES (6, 1, N'A', 12, 4, 0, 0)
INSERT [dbo].[Student] ([RID], [Class], [Section], [SubSection], [RollNo], [DesiredRoll], [TrackingNo]) VALUES (7, 1, N'B', 5, 1, 0, 0)
INSERT [dbo].[Student] ([RID], [Class], [Section], [SubSection], [RollNo], [DesiredRoll], [TrackingNo]) VALUES (8, 1, N'B', 5, 2, 0, 0)
INSERT [dbo].[Student] ([RID], [Class], [Section], [SubSection], [RollNo], [DesiredRoll], [TrackingNo]) VALUES (9, 1, N'B', 5, 3, 0, 0)
INSERT [dbo].[Student] ([RID], [Class], [Section], [SubSection], [RollNo], [DesiredRoll], [TrackingNo]) VALUES (10, 1, N'B', 10, 1, 0, 0)
INSERT [dbo].[Student] ([RID], [Class], [Section], [SubSection], [RollNo], [DesiredRoll], [TrackingNo]) VALUES (11, 1, N'B', 10, 2, 0, 0)
INSERT [dbo].[Student] ([RID], [Class], [Section], [SubSection], [RollNo], [DesiredRoll], [TrackingNo]) VALUES (12, 1, N'B', 10, 3, 0, 0)
INSERT [dbo].[Student] ([RID], [Class], [Section], [SubSection], [RollNo], [DesiredRoll], [TrackingNo]) VALUES (13, 1, N'B', 11, 1, 0, 0)
INSERT [dbo].[Student] ([RID], [Class], [Section], [SubSection], [RollNo], [DesiredRoll], [TrackingNo]) VALUES (14, 1, N'B', 11, 2, 0, 0)
INSERT [dbo].[Student] ([RID], [Class], [Section], [SubSection], [RollNo], [DesiredRoll], [TrackingNo]) VALUES (15, 1, N'B', 11, 3, 0, 0)




Thanks

View 4 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved