- Back to Home »
- sql server »
- sql server stored procedure variables parameters and if condition
Posted by :
Sudhir Chekuri
Thursday, 26 September 2013
sql queries in ms sql server for creating a table used to store registration data using stored procedure which takes inputs using input parameters.
Before inserting the email id given as input is checked wheather it is already existing in the table or not.
To check select command which returns the count of rows existing with that emailid.
The count returned by select command is stored in a variable named as u.
Variable u is created using a keywork declare.
In the below example i also used if condition to check the value present in variable u.
sql queries in ms sql server
create database db_connect
use db_connect
create table tbl_Register(sno bigint identity primary key,emailid varchar(50) unique,pwd varchar(15),gender varchar(10))
insert into tbl_Register(emailid,pwd,gender) values('sudhir@gmail.com','sudhir','Male')
select * from tbl_Register
alter procedure sp_register
@emailid varchar(50),
@pwd varchar(15),
@gender varchar(10)
as begin
--check emailid is unique
declare @u int=0
select @u=(select count(emailid) from tbl_Register where emailid=@emailid )
--register with unique emailid only
if(@u=0)
begin
insert into tbl_Register values(@emailid,@pwd,@gender )
end
end
exec sp_register 'govind@gmail.com','rani','Female'
Before inserting the email id given as input is checked wheather it is already existing in the table or not.
To check select command which returns the count of rows existing with that emailid.
The count returned by select command is stored in a variable named as u.
Variable u is created using a keywork declare.
In the below example i also used if condition to check the value present in variable u.
sql queries in ms sql server
create database db_connect
use db_connect
create table tbl_Register(sno bigint identity primary key,emailid varchar(50) unique,pwd varchar(15),gender varchar(10))
insert into tbl_Register(emailid,pwd,gender) values('sudhir@gmail.com','sudhir','Male')
select * from tbl_Register
alter procedure sp_register
@emailid varchar(50),
@pwd varchar(15),
@gender varchar(10)
as begin
--check emailid is unique
declare @u int=0
select @u=(select count(emailid) from tbl_Register where emailid=@emailid )
--register with unique emailid only
if(@u=0)
begin
insert into tbl_Register values(@emailid,@pwd,@gender )
end
end
exec sp_register 'govind@gmail.com','rani','Female'
MS Dotnet ninja |