SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 help in auto generating item code
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mercury76
Starting Member

7 Posts

Posted - 08/28/2007 :  23:52:39  Show Profile  Reply with Quote
Hi guys,

I am new to programming and sqlserver 2000. i use sql2000 as my database server...

I have a problem on auto-generating medicine codes. and i hope someone would help me with my script.

as you noticed i declared two variables (@id and @code). I am having a problem in generating @code... i would just like to create an autogenerated medicines_code from another field from another table (hosp_counter.counter_id)

here is how my medicines_code should look like ('MEDS34') "where 34 is from a counter_id in hosp_counter table...

but it only creates a medicines_code = 'M' (below is the result)

34(this is from counter_id) M(this is the autogenerated code) AUTOGENERATE1 1 1 1 1 0 1 0 1 1 0 0 1 2 1 100 100 0 0 0 0 0 0 1 8/29/2007 11:29:46 AM 1/1/1900 1/1/1900

here is my script in autogenerating medicine code...

set xact_abort on
begin transaction
declare @Code as varchar
select @Code =
(select 'MEDS' + CAST
(Counter_ID + 1 as varchar (50))
from Hosp_Counter where table_description = 'Medicines')
declare @ID as integer
select @ID = Counter_ID + 1
from Hosp_Counter
where table_description = 'Medicines'
select @ID
INSERT INTO Hosp_Medicines
(medicines_id,
medicines_code,
medicines_description,
medicines_specification,
medicines_genericname,
medicines_drugclass,
PhicCategory,
medpreparation,
medicines_prohibited,
medicines_trade,
medicines_NoneTrade,
medicines_taxable,
medicines_PhicCovered,
medicines_AmountCovered,
medicines_Discountable,
medicines_SmallUnit,
medicines_BigUnit,
medicines_AverageCostSmall,
medicines_AverageCostBig,
medicines_WeightedAverageCost,
medicines_weight,
medicines_conversion,
medicines_LowStockLevel,
medicines_OverStockLevel,
medicines_LowSalesReference,
AllowStat,
status,
CreateBy,
UpdateBy,
DeleteBy,
CreateDate,
UpdateDate,
DeleteDate)
VALUES
(@ID,
@code,
'AUTOGENERATE2',
'',
1,
1,
1,
1,
0,
1,
0,
1,
1,
0,
0,
1,
2,
1,
100,
100,
0,
0,
0,
0,
0,
0,
1,
'',
'',
'',
'8/29/2007 11:31:25 AM',
'',
'')
select @ID
Update Hosp_Counter set Counter_ID = @ID
where Table_Description = 'Medicines'
commit transaction

any help is very much appreciated!

'confused'
Joel

PeterNeo
Constraint Violating Yak Guru

357 Posts

Posted - 08/29/2007 :  01:18:21  Show Profile  Reply with Quote
Hi Joel,

Change the "declare @Code as varchar"
to "declare @Code as varchar(50)"

and to get the code u need only this
select @Code = 'MEDS' + CAST( @ID AS VARCHAR(50))

do the modifications, and let me know if any thing goes wrong...
Go to Top of Page

mercury76
Starting Member

7 Posts

Posted - 08/29/2007 :  01:36:22  Show Profile  Reply with Quote
Hi PeterNeo,

I really appreciate the help! it generated as i want it. Thanks a lot!!!!
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22769 Posts

Posted - 08/29/2007 :  02:44:40  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
<<
declare @Code as varchar
>>
If you dont specify the width then by default it would take 30

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000