How to use primary key value in another column!

Posted by Coolbharat under Sql Server on 5/6/2015 | Points: 10 | Views : 374 | Status : [Member] | Replies : 3
Hi,
I am asked to create barcode for books of library.Each book will have unique barcode.So what i think is to have a column that will have unique values for every book.So for that i think if we can use the primary key value for example "stock serial no" of each book and append it with some stringnlike "BC001","BC002"....

Please let me know how to achieve this!

Thanks in advance




Responses

Posted by: Sriharim on: 5/6/2015 [Member] Starter | Points: 25

Up
0
Down
Integer data type can't store string. We can use varchar data type to store integer+string.

create table book_temp (barcode varchar(20) primary key ,name varchar(20))
create table book (stocknumber varchar(50) primary key ,bookname varchar(20))

insert into book_temp
select '001','AAA' union select '002','AAA' union
select '003','AAA' union select '004','AAA' union select '001','BBB'


insert into book(stocknumber,bookname) (select name+CAST(barcode as varchar(9)),name from #book1)

select * from book

AAA001 AAA
AAA002 AAA
AAA003 AAA
AAA004 AAA
BBB001 BBB




---
Srihari

Coolbharat, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 5/6/2015 [Member] [MVP] Platinum | Points: 25

Up
0
Down
refer
http://www.sqlservercentral.com/Forums/Topic1062489-1291-1.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Coolbharat, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 5/8/2015 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi
we can use multiple primary after sql server 2008 we can use
I have attached images below post check and let me know.I have attached zip file check now.
 Download source file

Mark as Answer if its helpful to you

Coolbharat, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response