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 2005 Forums
 Transact-SQL (2005)
 Increment a value to insert manually

Author  Topic 

lols
Posting Yak Master

174 Posts

Posted - 2007-08-28 : 07:06:53
Hi,

I have a table

CustomerDetail
customerid
customername
status
app_no
deskno

Now everytime a new customer is created, i allocate the deskno. The logic I use is

create proc spoc_incrementdesk
as
DECLARE @deskno int
select @deskno = (SELECT max(deskno) from customerdetail)

Insert into customers(1,'rick',1,23232,@deskno)

If there are already records present in the customer table, it works well. However if the very first record is getting inserted it inserts null in the deskno because when no records are present (max(deskno) = null).

At time time of insertion, how do i check if @deskno = null and allot it 1.

thanks

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-08-28 : 07:14:13
why aren't you using identity column for this?


DECLARE @deskno int
select @deskno = isnull(max(deskno), 1) from customerdetail


_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

lols
Posting Yak Master

174 Posts

Posted - 2007-08-28 : 07:23:47
thanks :)
Go to Top of Page
   

- Advertisement -