hey guys,
i've jus started working with procedures.. I'm having a form with several controls say, date, name, adress,time.. etc.. etc.. once i eneter values in these fields n press save, all the values get saved in the database..a procedure has been written for doing this n everything is fine till now..
But ther's one more field called 'customer no', which shud auto-generate the value :CN1 for 1st customer, n CN2,CN3 so on for successive customers.. how shud i go abt this?? somebody plzz help :(
3 2114
hey guys,
i've jus started working with procedures.. I'm having a form with several controls say, date, name, adress,time.. etc.. etc.. once i eneter values in these fields n press save, all the values get saved in the database..a procedure has been written for doing this n everything is fine till now..
But ther's one more field called 'customer no', which shud auto-generate the value :CN1 for 1st customer, n CN2,CN3 so on for successive customers.. how shud i go abt this?? somebody plzz help :(
hi there,
CREATE Procedure Sp_InserCustomerDetails
@Custname varchar (20)
as
declare
@Result int,
@count int,
@CustId varchar (20)
select @count=count(subrowid)+1 from TblCustomers
set @CustId='CN'+RIGHT(CONVERT(varchar(10),@count),1)
IF EXISTS ( SELECT CustId FROM TblCustomers where CustId=@CustId )
BEGIN
SET @Result = 1
RETURN @Result
END
Begin
insert into TblCustomers(CustId,Custname) values(@CustId,@Custname)
End
SET @Result = 0
RETURN @Result
Go
here 'subrowid' is the identity field of the table.
Try this StoredProcedure,it doesthe work of auto-generation of CustomerID's. The Right() function extracts the string from the starting position specified by its 2nd paramerter.
RIGHT ( character_expression , integer_expression ). Refer SQL Server Books Online. GudLuck!
Cheers,
jai
To create automatically generated ID do following
1. Create table with identity column
[PHP]Create table CustomerInformation(
CusID int identity(1,1),
Name varchar(50) null,
Address varchar(100) null,
Date datetime default getdate())[/PHP]
2. Create stored procedure to accept parameters.
Don’t mention identity column in insert statement it will be generated automatically.
[PHP]Create proc InsertCustomerInformation
@Name varchar(50),
@Address varchar(100),
@Date datetime
AS
Insert into CustomerInformation ( Name, Address, Date)
values (@Name, @Address, @Date)
go[/PHP]
3. You can omit datetime as well it will be automatic as well the way I set it.
[PHP]Create proc InsertCustomerInformation
@Name varchar(50),
@Address varchar(100),
@Date datetime
AS
Insert into CustomerInformation ( Name, Address)
values (@Name, @Address)
go[/PHP]
3. Execute stored proc and see results
[PHP]Execute CustomerInformation 'ayshonscripts', '111 Some address', '02/11/07')[/PHP]
hey guys,
i've jus started working with procedures.. I'm having a form with several controls say, date, name, adress,time.. etc.. etc.. once i eneter values in these fields n press save, all the values get saved in the database..a procedure has been written for doing this n everything is fine till now..
But ther's one more field called 'customer no', which shud auto-generate the value :CN1 for 1st customer, n CN2,CN3 so on for successive customers.. how shud i go abt this?? somebody plzz help :(
To create automatically generated ID do following
1. Create table with identity column
[PHP]Create table CustomerInformation(
CusID int identity(1,1),
Name varchar(50) null,
Address varchar(100) null,
Date datetime default getdate())[/PHP]
2. Create stored procedure to accept parameters.
Don’t mention identity column in insert statement it will be generated automatically.
[PHP]Create proc InsertCustomerInformation
@Name varchar(50),
@Address varchar(100),
@Date datetime
AS
Insert into CustomerInformation ( Name, Address, Date)
values (@Name, @Address, @Date)
go[/PHP]
3. You can omit datetime as well it will be automatic as well the way I set it.
[PHP]Create proc InsertCustomerInformation
@Name varchar(50),
@Address varchar(100),
@Date datetime
AS
Insert into CustomerInformation ( Name, Address)
values (@Name, @Address)
go[/PHP]
3. Execute stored proc and see results
[PHP]Execute CustomerInformation 'ayshonscripts', '111 Some address', '02/11/07')[/PHP]
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Kent Lewandowski |
last post by:
hi all,
Recently I wrote some stored procedures using java jdbc code
(admittedly my first stab) and then tried to implement the same within
java packages (for code reuse). I encountered...
|
by: Tim Marshall |
last post by:
I was following the thread "Re: Access Treeview - Is it Safe Yet?" with
interest and on reading the post describing Lauren Quantrell's
SmartTree, I've run into something I don't understand: Stored...
|
by: John |
last post by:
Hi
When developing vb.bet winform apps bound to sql server datasource, is it
preferable to use SELECTs or stored procedure to read and write data from/to
SQL Server? Why?
Thanks
Regards
|
by: mooreit |
last post by:
The purpose for my questions is accessing these technologies from
applications. I develop both applications and databases. Working with
Microsoft C#.NET and Microsoft SQL Server 2000 Production and...
|
by: peter |
last post by:
I am trying to get a SQL stored procedure to use user maintained MQT
implicitly which raises questions on when they are used or not used.
In theory you would expect the stored procedure to pick up...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |