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.
| Author |
Topic |
|
zorlice
Starting Member
2 Posts |
Posted - 2007-04-15 : 13:56:18
|
| I know this may be in the wrong forum, but I have a question. I am working on a system for a video store. I have rentals and sales for videos. I have set the ID fields for rentals and sales to be autonumbers and increment by 1, but I would like to have an S in front of sales IDs and R in front of rental IDs at all times. It is kind of like a constant in all autonumber ID fields. I want the S and R to be in every ID field, but the number to change. Thanks. |
|
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
Posted - 2007-04-15 : 14:39:49
|
You can't do this with an identity column. identity columns have to be integer types. www.elsasoft.org |
 |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2007-04-15 : 16:09:38
|
You can do this with a second varchar column. After a Insert, you would update the second column to be 's' or 'r' + identcol depending on the type of sale. create table tb1(SaleID int identity(1,1) not null,SaleType varchar(20) not null ,SaleTypeID varchar(20) nulldeclare @SaleType varchar(20),@SaleTypeID vachar(20),@ID intset @SaleType = 'S'insert into Tb1(SaleType)Values(@SaleType)set @ID = Scope_IDentity()set @SaleTypeID = @SaleType + cast(@ID as varchar(10))update tb1set SaleTypeID = @SaleTypeIDwhere SaleID = @ID |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2007-04-15 : 20:02:55
|
It is not a good idea to do this. It is difficult to program, and would limit the scalability of your system.If you really need to display data this way, for example, in reports, you can add a simple conversion in a view and use that.create table T_RENTAL ( RENTAL_ID int not null identity(1,1) primary key clustered, COL_1 int)gocreate view V_RENTALasselect RENTAL_REPORT_ID = 'R'+right('0000000'+convert(varchar,RENTAL_ID),7), RENTAL_ID, COL_1from T_RENTALgoinsert into T_RENTAL (COL_1)select COL_1 =NUMBERfrom F_TABLE_NUMBER_RANGE(101,110)select * from V_RENTALgodrop table T_RENTALdrop view V_RENTALResults:(10 row(s) affected)RENTAL_REPORT_ID RENTAL_ID COL_1 ---------------- ----------- ----------- R0000001 1 101R0000002 2 102R0000003 3 103R0000004 4 104R0000005 5 105R0000006 6 106R0000007 7 107R0000008 8 108R0000009 9 109R0000010 10 110(10 row(s) affected)CODO ERGO SUM |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-04-16 : 00:01:40
|
| If you want to show data in front end, you can easily append itex in VB6Format(column,"R0000000")MadhivananFailing to plan is Planning to fail |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-04-16 : 00:04:29
|
And F_TABLE_NUMBER_RANGE function, you can see it in Script Library MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|