Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 need help to generate number from running no table

Author  Topic 

Idyana
Yak Posting Veteran

96 Posts

Posted - 2011-07-31 : 01:34:33
I'm really sorry. my mistake on previous question.

I re-write my question as following,

I've table and data as following,

CREATE TABLE [dbo].[myRunnNum](
[idx] [int] IDENTITY(1,1) NOT NULL,
[myCode] [varchar](10) NOT NULL,
[runnNum1] [int] NOT NULL,
CONSTRAINT [PK_myRunnNum] PRIMARY KEY CLUSTERED
(
[idx] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY];

insert into myRunnNum values('trnxNo',9);



Let's say, my transaction as following,

declare @tItem table(idx int identity, trnxNo int, 
payer int, amt decimal(10,2));
/*trnxNo is unique*/

declare@data xml set@data='<data> <paymentItem><payer>1</payer><amt>10.00</amt></paymentItem>
<paymentItem><payer>2</payer><amt>16.00</amt></paymentItem>
<paymentItem><payer>6</payer><amt>35.00</amt></paymentItem>
<paymentItem><payer>23</payer><amt>40.00</amt></paymentItem></data>'


i need to insert into @tItem(trnxNo, .....).

my TrnxNo is generated from myRunnNum(runnNum1) where myCode='trnxNo', which is myRunnNum is running number table

We've to make sure, there's no duplicate value is generated for trnxNo

I'm stuck, how to generate the runnNum1 in myRunnNum and insert into @tItem(trnxNo) with XML data

My expected result as following,

@tItem
idx | TrnxNo | payer | amt
-------------------------------
1 10 1 10.00
2 11 2 16.00
3 12 6 35.00
4 13 23 40.00

myRunnNum
idx | code | runnNum1
---------------------------
1 trnxNo | 13

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-07-31 : 01:46:26
use ROW_NUMBER() as suggested earlier. that should generate unique number each time.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Idyana
Yak Posting Veteran

96 Posts

Posted - 2011-07-31 : 02:00:35
sir,

how do I update myRunnNum table once my transaction is finish? As a result, myRunnNum as following

myRunnNum
idx | code | runnNum1
---------------------------
1 trnxNo | 13
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-07-31 : 02:32:34
use

UPDATE myRunnNum
SET runnNum1 = (SELECT MAX(trnxNo) FROM @tTem)
WHERE code='trnxNo'


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2011-07-31 : 02:50:06
Also asked and answered here http://www.sqlservercentral.com/Forums/Topic1151505-392-1.aspx



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

Idyana
Yak Posting Veteran

96 Posts

Posted - 2011-07-31 : 03:03:25
tq sir

before this, I'm really stuck
Go to Top of Page
   

- Advertisement -