473,382 Members | 1,424 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Creating custom Primary Key in SQL Server 2005

I currently have a website which is using ASP.NET 2.0, C#, and SQL
Server 2005. The website will be used to enter grants for a
university. When a new grant is entered, I need to generate a primary
key. The primary key will need to follow the format: Two digit for
fiscal year, then number of the grant for that year. Example:

Year 08 and 14th grant of the year would be: 0814

How can I implement this. Right now, I have a "New Grant.aspx" page
with a Submit button. I am guessing the date is going to be formatted
in C#. How can I check what the last primary key in the database is?
Also, it seems to me that SQL Server insists that the primary key be
32 bits long, however my primary key will only be 4. How can I
override this? Thanks.
Mar 11 '08 #1
3 3631
AMD_GAMER (am********@gmail.com) writes:
I currently have a website which is using ASP.NET 2.0, C#, and SQL
Server 2005. The website will be used to enter grants for a
university. When a new grant is entered, I need to generate a primary
key. The primary key will need to follow the format: Two digit for
fiscal year, then number of the grant for that year. Example:

Year 08 and 14th grant of the year would be: 0814

How can I implement this. Right now, I have a "New Grant.aspx" page
with a Submit button. I am guessing the date is going to be formatted
in C#. How can I check what the last primary key in the database is?
Also, it seems to me that SQL Server insists that the primary key be
32 bits long, however my primary key will only be 4. How can I
override this? Thanks.
SQL Server accept any length of the PK up to 900 bytes.

0814 appears a tad short to me. What if there are more than 99 grants
in one year?

It would be better to make the PK a two-column key:

Year char(4) NOT NULL,
grantno int NOT NULL,

You find the new grantno with:

BEGIN TRANSACTION

SELECT @nextgrantno = coalesce(MAX(granttno), 0) + 1
FROM tbl WITH (UPDLOCK)
WHERE Year = @year

INSERT ...

For display you add a computed column:

displaykey as substring(Year, 3, 2) +
right(ltrim(str(grantno + 100))), 2) PERSISTED UNIQUE

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Mar 11 '08 #2
Ok, so I should use the 32 bit auto-generated key as the SQL PK, and
then use my own calculated GrantID just as an identifier.
Mar 11 '08 #3
>I should use the 32 bit auto-generated key as the SQL PK, and then use my own calculated GrantID just as an identifier. <<

No. You ought to visit the accounting department and find out what
your university uses for a grant identifier. Cowboy coders love to re-
invent the wheel and avoid research; professional actually gather
specs BEFORE they code anything.

Mar 12 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: pmud | last post by:
Hi, I am using a compare validator in asp.net application(c# code). This Custom validator is used for comparing a value enterd by the user against the primary key in the SQL database. IF the...
5
by: | last post by:
Trying to learn about manipulating collections of objects, and populating these objects dynamically from datasources. Could someone post a code sample that shows the following: Instantiating a...
8
by: Mike Kelly | last post by:
I've chosen to implement the "optimistic concurrency" model in my application. To assist in that, I've added a ROWVERSION (TIMESTAMP) column to my main tables. I read the value of the column in my...
12
by: Mats Lycken | last post by:
Hi, I'm creating a CMS that I would like to be plug-in based with different plugins handling different kinds of content. What I really want is to be able to load/unload plugins on the fly without...
8
by: bryan | last post by:
I've got a custom HttpHandler to process all requests for a given extension. It gets invoked OK, but if I try to do a Server.Transfer I get an HttpException. A Response.Redirect works, but I really...
7
by: rcamarda | last post by:
I wish to build a table based on values from another table. I need to populate a table between two dates from another table. Using the START_DT and END_DT, create records between those dates. I...
3
by: Jeff Kish | last post by:
Hi. I am between beginner and intermediate level of knowledge of sql server. I am dealing with an sql server 2005 situation (oracle also but thats another thread/story). I need to assemble a...
2
by: astolpho | last post by:
I am using a slightly outdated reference book on J2EE programming. It gives 2 methods of creating a database used in its casestudies. The first is an ANT script that gives the following output: ...
2
by: Danny | last post by:
Hello, We imported a bunch of tables from a database and realized that the primary keys weren't copied to the destination db. In order to re- create the keys, we need to know which tables have...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.