Sequence Number Help

Nov 17, 2005

I have the following stored procedure:

CREATE PROCEDURE dbo.ABR_HDR_INSERT

@id int output,
@status int,
@mode int,
@sessid varchar(100)

AS

declare @ay char(4)
declare @ddo char(4)
declare @abrid varchar(50)
declare @seq_no int

SELECT @ddo = a.DDO_DSCR_SHORT
FROM dbo.DIM_DDO a
JOIN dbo.Temp_ABR_HDR b
ON a.DDO_ID = b.DDO
WHERE b.SESSIONID = @sessid

SELECT @ay = AY
FROM dbo.Temp_ABR_HDR
WHERE SESSIONID = @sessid


-- set the default seq_no
SELECT @seq_no = 1
-- get the max abrid. if no record return the seq_no will be 1
SELECT @seq_no = convert(integer, max(right(abrid, 4)))
FROM dbo.ABR_HDR
WHERE left(abrid, 7) = @ay + @ddo

-- convert @seq_no to string prefix by 0
SELECT @abrid = @ay + @ddo + right('0000' + rtrim(convert(char(4), @seq_no)), 4)


Insert into dbo.ABR_HDR (ABRID, HDR_MODE, HDR_DDO, HDR_AY, HDR_REQUESTOR, HDR_DT, HDR_SUBJECT, HDR_DESCRIPTION, HDR_STATUS)

SELECT

@abrid,
@mode,
DDO,
AY,
REQUESTOR,
DT,
SUBJECT,
DESCRIPTION,
@status

FROM dbo.Temp_ABR_HDR


SELECT @id = @@identity

return @id
GO

ABRID gets inserted as a <NULL> value. I can't figure out why? If I comment out the following then ABRID will insert without the sequence number:

CREATE PROCEDURE dbo.ABR_HDR_INSERT

@id int output,
@status int,
@mode int,
@sessid varchar(100)

AS

declare @ay char(4)
declare @ddo char(4)
declare @abrid varchar(50)
declare @seq_no int

SELECT @ddo = a.DDO_DSCR_SHORT
FROM dbo.DIM_DDO a
JOIN dbo.Temp_ABR_HDR b
ON a.DDO_ID = b.DDO
WHERE b.SESSIONID = @sessid

SELECT @ay = AY
FROM dbo.Temp_ABR_HDR
WHERE SESSIONID = @sessid


-- set the default seq_no
--SELECT @seq_no = 1
-- get the max abrid. if no record return the seq_no will be 1
--SELECT @seq_no = convert(integer, max(right(abrid, 4)))
--FROM dbo.ABR_HDR
--WHERE left(abrid, 7) = @ay + @ddo

-- convert @seq_no to string prefix by 0
--SELECT @abrid = @ay + @ddo + right('0000' + rtrim(convert(char(4),@seq_no)), 4)

SELECT @abrid = @ay + UPPER(@ddo)

Insert into dbo.ABR_HDR (ABRID, HDR_MODE, HDR_DDO, HDR_AY, HDR_REQUESTOR, HDR_DT, HDR_SUBJECT, HDR_DESCRIPTION, HDR_STATUS)

SELECT

@abrid,
@mode,
DDO,
AY,
REQUESTOR,
DT,
SUBJECT,
DESCRIPTION,
@status

FROM dbo.Temp_ABR_HDR


SELECT @id = @@identity

return @id
GO

So, the code that sets the sequence number is what is causing the <NULL> value.

Any help is appreciated.
Thanks,
-D-

View 4 Replies


ADVERTISEMENT

Log Sequence Number

Apr 16, 2002

Hi, I've a question, if I've the LSN (Log Sequence Number) of a transaction, keep with the program "Log Explorer", can I know which is the IP of the user that have do the transaction (perhaps serching in a log file of the Win 2000 Server) ?
10x,
Clara

View 1 Replies View Related

Sequence Number

Dec 29, 1998

In SQL 6.5 object dependencies window, what does the sequence number means?

Thanks,

Sam

View 1 Replies View Related

Sequence Number

Oct 3, 2007

suppose I have the following table grouped by memid

memiddx
3455
3322
3232
433
43434

I want to attach sequence number for each unique value of dx per memid as
below

memiddxSEQ
34551
33222
32323
4331
434342

I am using a cursor right now and it takes a lot of time if my table is large.
Is there a more efficient way of doing this.

Thanks much.

View 3 Replies View Related

Number Sequence

Jan 11, 2008

is it possible to generate a number sequence in a query (without using loop). I want the output to look as
-------
ID
-------
1
2
3
4
5
6
7
8
9
....
upto the last number I give in the query

View 13 Replies View Related

Row Sequence Number

Apr 4, 2006

Hello all,Iīm currently using a SQL Serve 2K. Would like to do a selectwhich returns the row number - this should not be physically stored inthe database. So for example, I would like to do a query against theCUSTOMER table and receive:* rowID || name1 Evander2 Ron3 Scoth4 JaneI donīt want to store the ID, because if I change the order byclause, the sequence may modifiy, and, for another example, having thesame set of data, I would receive:* rowID || name1 Scoth2 Ron3 Jane4 Evander could someone help me ?best regards,Evandro

View 3 Replies View Related

Generating Sequence Number

Aug 29, 2001

Hello,

I need to know how to generating a sequence number, for example, from 300,000 to 900,000 without skipping any number due to failure. For example, if user 1 request a number then he/she will get 300000 in a transaction. User 2 will get 300001. How ever user 1's transaction fails, then the next request should get 300000. Is it possible to do this in SQL2K? If so, how do I create a table that and stored procedure that can do this.

Thank you so much.

NK

View 3 Replies View Related

Generate Sequence Number In Sql???

Mar 15, 2005

Hi,
I have written the following StoredProcedure





Code:

create Procedure spCreateQuestion(
@QuestionName varchar(30)
)

as

declare @newAnswerId int
declare @newQuestionId int

set @QuestionName='New Question'

BEGIN TRANSACTION Q1

--Creates New QuestionId with AnswerId 0
INSERT INTO Questions(QuestionId,Name,AnswerId)
SELECT 1 + COALESCE(MAX(QuestionId), 0),RTRIM(@QuestionName),0
FROM Questions

--QuestionId just now created
SELECT @newQuestionId=QuestionId FROM Questions WHERE Name=@QuestionName

BEGIN TRANSACTION QA1

--Create an AnswerId
INSERT INTO Answers(AnswerId)
SELECT 1 + COALESCE(MAX(AnswerId), 0)
FROM Answers

--AnswerId just now created(I hope not the best way to do like this)
SELECT @newAnswerId=MAX(AnswerId) from Answers --is it the best way to call statement like this or any other way better than this

--update Questions Table with this new Answerid
UPDATE Questions
set
AnswerId=@newAnswerId
where QuestionId=@newQuestionId

COMMIT TRANSACTION QA1
COMMIT TRANSACTION Q1



I think the second Transaction is not locking the table.so some how i should be
able to get the newly create AnswerId

i can't use the identity column in my tables

Can some one please have a look at it and suggest me how do we go about it..

View 8 Replies View Related

Sequence Number Generation

Feb 3, 2005

Does anyone know an efficient method for generating a sequence number in the following form?

Starting with 2 columns
1 2
----
A X
A Y
B X
B Y
B Z
C X

I want to then generate a third column as follows:
1 2 3
-------
A X 1
A Y 2
B X 1
B Y 2
B Z 3
C X 1



The purpose being so that I can easily identify the previous row within a Column1 group. So given column1=A and column2=Y I know that the previous row is Column3 - 1 where column1 = A. Therefore I will be able to join to the previous result of any row within any group quickly for future calculations.

Any ideas? Thanks.

View 10 Replies View Related

Generating Sequence Number....

Oct 3, 2007

Hi,
I got stuck with a trivial issue. I have a table named T1 having column as Col1 (INT). I have another table named T2 having columns Col1(Int), COl2(Int). Following are the reacords in my table T1.


Col1
----
1
1
1
1
2
2
3
3
3
3
3
4
5
6
6
7

And I want to migrate the data from T1 to T2 where the Col1 data of T1 will get migrated to Col1 of T1 and after the migration is done the T2 should have the data like this.........

Col1 Col2
---- -----
1 1
1 2
1 3
1 4
2 1
2 2
3 1
3 2
3 3
3 4
3 5
4 1
5 1
6 1
6 2
7 1


Thanks In Advance,
Rahul Jha

View 14 Replies View Related

Generating Sequence Number....

Nov 14, 2007

Hello folks,
I have a table where the records are like followings.

ID Value
---------------
1 aa
1 aa
1 aa
1 bb
1 bb
1 bb
1 bb
1 cc
2 pp
2 dd
2 dd
3 qq
4 aa



I need to include one column "SeqId" which will be having value based on the 1st 2 columns (ID + Value). I am struglling with this since last few days. Can I request you guys to help me out in this reagard.

ID Value SeqId
-----------------------------
1 aa 1
1 aa 2
1 aa 3
1 bb 1
1 bb 2
1 bb 3
1 bb 4
1 cc 1
2 pp 1
2 dd 1
2 dd 2
3 qq 1
4 aa 1




Thanks,
Rahul Jha

View 14 Replies View Related

Add Sequence Number Within Group?

Oct 13, 2013

I have the following data:

POL# POL_EFF_DATE
123 1-1-2012
123 1-1-2012
123 1-1-2012
123 1-1-2013
123 1-1-2013
456 1-1-2012
456 1-1-2012
456 1-1-2013
456 1-1-2013

I want to add sequence number to each group with same Pol# and Effective Date like this:

POL# POL_EFF_DATE Seq
123 1-1-2012 1
123 1-1-2012 1
123 1-1-2012 1
123 1-1-2013 2
123 1-1-2013 2
456 1-1-2012 1
456 1-1-2012 1
456 1-1-2013 2
456 1-1-2013 2

is there a set based function to accomplish this in SQL 2008?

View 2 Replies View Related

Getting Sequence Number In 2000

Apr 18, 2008

I have a table like this

create table
#invoice (invoice_no varchar(5), invoice_amount int)

INSERT INTO #invoice
SELECT 'A', 100 UNION
SELECT 'A', 200 UNION
SELECT 'A', 300 UNION
SELECT 'B', 400 UNION
SELECT 'B', 500 UNION
SELECT 'B', 600 UNION
SELECT 'B', 700


Now I want a output like this

INVOICE_NO AMOUNT SEQUENCE_NO
A 100 1
A 200 2
A 300 3
B 400 1
B 500 2
B 600 3
B 700 4

The sequence_no should set back to 1 on change of invoice_no
these are items in a an invoice. they just want a sequence_no

I can do this using cursors, but if anyone can help with WHILE LOOP
would be great

THANKS






-----------------------------------------------------------------------------------------------
Ashley Rhodes

View 10 Replies View Related

Getting Sequence Number Generations

May 31, 2006

I am newbie to SQL Server 2005.

Can any experts please give me code for how to generate automatic sequence numbers in sql server 2005?

I want to use this generated sequence number as a primary key to update the records.

Thanks in advance,
Vani

View 2 Replies View Related

Sequence Number Generation

Jan 25, 2008

Hi,
I have an error table that is to be updated by more than one package.
There is a sequence number generated in the error table. It is generated by using the max value of the previous data present in the table.
When more than one package runs parallely, conflict occurs in generating the sequence number.
How can this be handled?

View 1 Replies View Related

Unique Number/sequence

Jul 23, 2005

Hallo,Hot to get unique, sequential number during executionof stored procedure ?I can create table with autoincrement column,add record, get ident_current and delete recordeach time i need the number.However its not elegant i guess.best regardspluton

View 3 Replies View Related

Auto Number Sequence

Oct 6, 2006

How can I create a number sequence starting at a certain number and continue on for the number of records I have.

For example I have 3000 records in my table and a field named I created called RecordId which I'd like to start at number 1 and goto 3000 (or maybe even start at 9000 and goto 12000 or however many records there are).

In my pseudo SQL code Im guessing it would be something like...

select * from Incident

update Incident
set RecordId( i=9000; i<=Number of Records in Table; i++)



Whats the easiest way to do this?

View 7 Replies View Related

Mirroring :: Log Sequence Number

Jun 29, 2015

Why log sequence number gets broken in SQL server?

View 4 Replies View Related

Sequence Number For Records

Nov 15, 2007

Hi...

I have Sql statement more like this

SELECT row_number() over (ORDER by a.employeeID) as rec_num, a.* FROM EmployeeA a
UNION
SELECT row_number() over (ORDER by a.employeeID) as rec_num, a.* FROM EmployeeB a


rec_num employeeID employeeName employeeDepartment


1 777 Mike HR
2 888 Susy HR
1 111 Smith TECH
2 222 John TECH
3 333 Lenny TECH



How do i get sequence number for all of this records. The rec_num reset for every statement. I want the records numbering for second statement continue from first statement so that it can be like this :

rec_num employeeID employeeName employeeDepartment


1 777 Mike HR
2 888 Susy HR
3 111 Smith TECH
4 222 John TECH
5 333 Lenny TECH

View 4 Replies View Related

SQL Server Sequence Number

Mar 27, 2008

Hi,

Does any one have information about how 'large' the SQL Server Sequence Number generator can go,
like 1 to 1 billion, or to 12 zeros, etc?

Thanks.

--John

View 1 Replies View Related

Add Sequence Number To Data

Sep 17, 2007



Hi all, I need some your help.
I want to add sequence number to data flow below:

Cust_Name | Month
=======================
Chonnathan | 5
Chonnathan | 4
Chonnathan | 7
Derec | 3
Derec | 9

and the result i need is:

SEQ_nbr | Cust_Name | Month
================================
1 | Chonnathan | 4
2 | Chonnathan | 5
3 | Chonnathan | 7
1 | Derec | 3
2 | Derec | 9

How can I do it in Integration Service to show like the above?

Thank you for your respones,
Chonnathan

View 1 Replies View Related

Chalenge Returning The First Available Number In A Sequence

Aug 8, 2007

 Hi all,Ive been trying to figure this out and has proven to be quite difficult for me. Lets say i do a select on all client numbers from a clients table i would want the first available number returned.Given the client table below my query would return 4 because its the lowest number availeble. Thanks. client tableclnum12367 

View 4 Replies View Related

Creating Alphanumberic Sequence Number

Oct 12, 2007

Database in:SqlServer builtin interface with VS05
Using:ASP.NET VISUAL STUDIO 2005
I want to create a unique ID that identifies a row.
Id:STU+(SeqNumber) for Student and
Id:EMP+(SeqNumber) for Employee
[Question]:But how can i create sequence number and store it for use like that of ORACLE

Current Idea:Based on registration info if type is student generate STU+seq num and insert to table along with username and other details and if employee like wise.
So a procedure will receive this from user and append that with seqnumber and stores that in the table

View 1 Replies View Related

How To Get The Sequence Number Without Using Temp Table

May 18, 2005

Here is my problem:

I have a table with following columns:

PersonID    FirstName    LastName
102                  John             Ben
103                  Josh              Parker
104                  Mark            Ben

Now if I type SELECT * FROM Person WHERE LastName = 'Ben' these two records will be displayed

PersonID    FirstName   LastName
102                   John            Ben
104                   Mark           Ben

But I want this to return with one additional Sequence column like this:

New Column   PersonID    FirstName   LastName
          1
              
102                   John
           Ben
          2
             
104                   Mark
          Ben

How can I add this so called "New Column" ?
  

View 2 Replies View Related

Generating Unique Sequence Number

Jul 19, 2000

Is there wa way to generate unique sequence numbers in SQL server?
(just like the way it is in Oracle i.e. seqeuence and then use nextval)

View 2 Replies View Related

Sequence Number Generating With A Twist

Apr 9, 2008

:eek:

I'm having a brain not functioning day - well who am I kidding - more like a year :rolleyes:

I need some help with some sequence numbering and cannot even get my head around the logic I want to use, let alone the actual code.

I have a dataset with 3 fields:

Area
ID
RefNo

This table contains a list of employee ID's by Area and each employee has a RefNo (counter) in each area.

The data comes from 2 different sources and is combined in this table. Some employees had no RefNo already assigned to them so I have entered their RefNo as 10000 in order to ensure they are sorted at the bottom of the list.

The ID's that have RefNo's have to keep the one they have. Therefore, I need to create RefNo's for the ones that currently have RefNo 10000.

These numbers I create have to follow on from the highest RefNo for the Area.

For example:

Area ID RefNo
A Z 1
A Y 2
A X 3
A W 10000
A V 10000
B N 1
B O 10000
B P 10000

So, for Area A, ID's W and V would have to be assigned RefNo 4 and 5, and for Area B, ID's O and P would have to be assigned RefNo 2 and 3.


Hope this makes sense to all.

BTW, am using SQL 2000 at the moment.

Thanks in advance for any help!

View 11 Replies View Related

How To Insert Sequence Number With Group

Jun 11, 2008

SOURCE TABLE
ID________COMMENT
123_______I am joe
123_______I am programmer
124_______I am Wang
124_______I am programmer
124_______I like cricket

DESTINATION TABLE

ID_____SEQ______COMMENT
123_____1_______I am joe
123_____2_______I am programmer
124_____1_______I am wang
124_____2_______I am programmer
124_____3_______I like cricket
can somebody please advise the easiest way to do this in sql 2000?

View 3 Replies View Related

Add Sequence Number To Existing Table

Nov 14, 2013

I have a table with no sequence column. I created a new column, but I need a way to add sequence number. This is not going to be a Primary key or indexed. I assume I will use update, but how do I do this?

Example

Before
Name | Seq
Paul | NULL
Tom | NULL
Grant | NULL

After
Name | Seq
Paul | 1
Tom | 2
Grant | 3

View 1 Replies View Related

Generate Sequence Number Basing On Each Id

Mar 28, 2008

Hi all
anbody can help me writing sql code for this. All i need is to generate sequence basing on id_no
Ex: if ID=ABC(twice) in seq_col as abc --1
abc ---2
Tables which I have
Uniques_No ID_NO SEQ
---------------------------------------------
1 ABC
2 ABC
3 ABC
4 BBC
5 BBC


Expected results as below :
------------------

Uniques_No ID_NO SEQ
---------------------------------------------
1 ABC 1
2 ABC 2
3 ABC 3
4 BBC 1
5 BBC 2

Thanks in advance

View 4 Replies View Related

Sequence Number In A Year Without Auxiliary Table

Aug 29, 2007

Hi,I've
this problem. I've a table to manage spedition with an identity column
(id), a year field and a numberInYear field. The identity is the pk of
the table and I want to make the numberInYear growing sequentially and
restart every change year. I don't want to make a table to manage this
sequence.I thought this alghoritm:1) Extract the max identity value (id) of the year before this, before the insert of the new record;2) Insert the new record;3) NumberInYear = the id just inserted minus the max id extract before.The
problem is the NumberInYear cannot be null, so I've to know the id of
the record inserting before the inserting itself... Is it possible?

View 7 Replies View Related

T-SQL (SS2K8) :: Programmatically Generate A Number Sequence?

Sep 3, 2014

I'm trying to do a simple insert into a table, something like this:

insert into sometable (ID, somecolumn)
select 'Task-ID', somevalue from SomeOtherTable
where something = 'someothervalue'
(or something to that effect)

So, the SELECT would generate something that looks like this:

ID somecolumn
-- ----------
Task-ID somevalue1
Task-ID somevalue2
Task-ID somevalue3
(etc.)

Here's where my problem comes in: ID is a PK, and needs to be unique. What I need it to do is this:

ID somecolumn
-- ----------
Task-ID.1 somevalue1
Task-ID.2 somevalue2
Task-ID.3 somevalue3
(etc.)

What I don't know is, how do I programatically generate the number sequence? Note: I do not have admin rights to the table, i.e. I cannot just change a column to IDENTITY.

Also the 'Task-ID' must remain part of the ID; in other words, I can't just generate a GUID, and it needs to be easily identifiable.

What I'm hoping to do is rewrite my SQL like this:

insert into sometable (ID, somecolumn)
select 'Task-ID.' + (generated seq #), somevalue from SomeOtherTable
where something = 'someothervalue'

Is there an easy way to do this?

View 9 Replies View Related

Update Column With Sequence Number Every Time

Sep 25, 2006

hello friends

i want to update one column from my table regularly on sequence number i.e. 0,1,2,3,4,5

i created procedure but it is not working as per my output

declare @IndexIDGen int
declare @ID int
set @ID = 0
update temp_test set indexid = NULL
declare IndexIDGen cursor for select indexid from temp_test
open IndexIDGen
FETCH Next from IndexIDGen into @IndexIDGen
while @@fetch_status = 0
begin
update temp_test set indexid = @ID where indexid is null

set @ID = @ID + 1
print @id
fetch next from IndexIDGen into @IndexIDGen
end

close IndexIDGen
deallocate IndexIDGen


where i am going in wrong direction ??????

T.I.A

View 8 Replies View Related

How To Create A Sequence Generator Number In SSIS

Mar 28, 2006

Hi,

I got 5000 rows in source and when i am sending the data to destination it has to create a sequence generator number for each row.

Can any one help me which transformation do i need to take for doing this in SSIS.













View 10 Replies View Related







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