SQL Code: 'Sample3' 'Sample4'

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

SQL CODE

create database sample


alter database sample modify name=sample3
sp_renamedb 'sample3','sample4'

--3-Creating and working with tables

--4 adding and removing constraints
--add constraint
alter table tbl_gender
add constraint cons_name
default 'unknown' for gender

--remove constraint
alter table tbl_gender
drop constraint const_name

--v-6-Adding a check constraint
alter table tbl_gender
add constraint const_name check(age>0 and age<100)
insert into tbl_gender (Id,Gender,age) values(4,'male',10)
select * from tbl_gender

--v7-Identity column
create table identity_table
(id int identity(1,1),name varchar(30))
insert into identity_table values('a')
insert into identity_table values('b')
insert into identity_table values('c')

delete from identity_table where id =2
set identity_insert identity_table off
insert into identity_table (id ,name) values(2,'b')


select * from identity_table
delete from identity_table
dbcc checkident('identity_table', reseed,0)
ALTER PROCEDURE LASTIDENT
AS
BEGIN

select max(id) from identity_table
END

-- v-11-Group by in sql server
use sample
create table tblemployee
(id int identity,name varchar(20),salary int,city varchar(20))
insert into tblemployee values('a',1000,'mumbai')
insert into tblemployee values('b',3400,'pune')
insert into tblemployee values('c',4000,'mumbai')
insert into tblemployee values('d',4500,'pune')

select city, sum(salary) as totalsalary, count(city)as cityyy from
tblemployee
group by city
select * from tblemployee
select * from tblemployee having sum(salary)>2000


--V-18-Stored procedures in sql server
create procedure spgetdetails
@vari nvarchar (20)
as
begin
select * from tblemployee where id=@vari
end

alter procedure spgetdetails --modify store procedure used alter keyword
@vari nvarchar (20)
with encryption -- this line encrypt the store procedure we cant view the
text of store procedure
as
begin
select * from tblemployee where id=@vari
end

exec spgetdetails 1
sp_helptext spgetdetails
drop procedure spgetdetails

--V-19-Stored procedures with output parameters
alter procedure spdetailsoutput
@varcity nvarchar(30) ,
@varid int output
as
begin
select @varid= COUNT(id) from tblemployee where city=@varcity
end

declare @totalid int
execute spdetailsoutput 'pune',@totalid output
print @totalid

--get last generated id base on output
create proc splastid
@id int output
as
begin
select @id= MAX(id)+1 from tblemployee
end
declare @lastid int
exec splastid @lastid output
print @lastid

--V-20-Stored procedure output parameters or return values
use sample
select * from tblemployee
--v-20.1
create procedure spreturntotalid
as
begin
return(select COUNT (id) from tblemployee)
end
declare @idresult int
exec @idresult=spreturntotalid
print @idresult

--v.20.2
create proc spreturnnameout
@id int,
@name varchar(20) output
as
begin
select @name= name from tblemployee where id=@id
end

declare @returnnamevar varchar(20)
exec spreturnnameout 1,@returnnamevar output
print @returnnamevar

--v.20.3
create proc spreturnname
@id int
as
begin
return(select name from tblemployee where id=@id) --name is string type and
it give error because return keyword give only integer if it is salary than
it success.

end
declare @namevar varchar(20)
exec @namevar=spreturnname 1
print @namevar

use sample
select ASCII('a')
select CHAR(65)

declare @straz int
set @straz=65
while (@straz<=90)
begin
print char( @straz)
set @straz=@straz+1
end

select upper( city) as city from tblemployee

--V-30 Inline function
use sample
select * from tblemployee
create function inlinefun(@paracity nvarchar(30))
returns table
as
return(select * from tblemployee where city=@paracity)
--execute function
select * from inlinefun('mumbai')
update inlinefun('mumbai') set name='a1' where id=1
insert into inlinefun('mumbai') values('f',3232,'delhi')
--inline function is updated,inderted but multistatement function not
updated,inserted
create function multistafun()
returns @table table (id int ,name nvarchar(20),city nvarchar(20))
as
begin
insert into @table

select id,name,city from tblemployee
return
end

--39-Views in sql server
use sample
create table a(id int ,name varchar(20),lastname varchar(20),bid int)
insert into a (id,name,lastname,bid) values(1,'an','aln',1)
insert into a (id,name,lastname,bid) values(2,'bn','bln',2)
insert into a (id,name,lastname,bid) values(3,'cn','cln',3)
drop table b
create view vwa
as
select * from a
--execute view
select * from vwa
--update,delete ,insert through view and it also change in base(underlying)
table i.e table a

update vwa set name='aaaaaa' where id=1
delete from vwa where id=1
insert into vwa values(1,'an','aln',1)

--V.43-DML triggers in sql server

You might also like