SQL stored procedure and change the field data in text form

SQL stored procedures

Stored Procedures SyntaxThe three forms:
no return no arguments , no return has parameters , there are parameters back there .
Stored Procedures integrated case

Stored Procedures "no return no arguments."

note: If the stored procedure is always present there is no need to determine whether there is a stored procedure <Follows Similarly>

//存储过程	《无返无参》
//判断存储过程是否存在  储存过程名称(sp_或xp_)
if exists(select * from sysobjects where name='sp_heros')
//存在删除
drop proc sp_heros

go

//创建存储过程
create proc sp_heros
as
	//SQL执行语句
	select * from UserInfo s1,Sutclass s2 where s1.UserID=s2.UserID order by s1.UserID
go

//调用存储过程  (exec 或 execute)
exec sp_heros

Stored Procedures "has no return parameters"

//存储过程《无返有参》
//判断存储过程是否存在
if exists(select * from sysobjects where name='xp_peroscan')
//存在删除
drop proc xp_peroscan

go

//创建存储过程
create proc xp_peroscan
//创建一个返回参数变量
@sum varchar(50)
as
	//SQL执行语句
	select * from UserInfo s1,Sutclass s2 where s1.UserID=s2.UserID and UserName like ''+@sum+'%'  order by s1.UserID
go

//调用存储过程
exec xp_peroscan '赵'

Stored procedures "have returned to have the Senate"

//存储过程 《有返有参》
if exists(select * from sysobjects where name='sp_terop') 
//存在删除
drop proc sp_terop

go

create proc  sp_terop
//创建一个返回参数变量
@num int output,
//创建一个参数的变量
@ken varchar(10)
as
	//SQL执行语句
	select @num=count(*) from UserInfo  where Addres like ''+@ken+'%'
go

//声明一个变量
declare @hun int  //(可自选或者可和返回参数的参数名一样)
exec sp_terop  @hun output,'湖北'
print '个数:'+convert(varchar(5),@hun)

Stored Procedures integrated case

//创建一个逻辑判断的存储过程 有返有参
if exists(select * from sysobjects where name='sp_jisuanji')
drop proc sp_jisuanji

go

create proc sp_jisuanji
@sum int output,
@num1 int,
@bnt varchar(10),
@num2 int
as 
	if(@bnt='+') 
	begin
		set @sum=@num1+@num2
	end
	else if(@bnt='-') 
	begin
		set @sum=@num1-@num2
	end
	else if(@bnt='*') 
	begin
		set @sum=@num1*@num2
	end
	else if(@bnt='/') 
	begin
		set @sum=@num1/@num2
	end
	else
	begin
		set @sum=404
	end
go

declare @kun int 
exec sp_jisuanji @kun output,2,'+',1
print '结果为:'+convert(varchar(5),@kun)

Example two

// 逻辑判断并添加 (查询数据库是否存在张三,是则添加,否则返回404)
if exists(select * from sysobjects where name='sp_youer')
drop proc sp_youer

go

create proc sp_youer

@num01 int output,
@UserName varchar(50),
@UserPwd varchar(50),
@Sex varchar(2),
@Birthday datetime,
@Addres varchar(200),
@Remarks varchar(200)
as
	declare @sum int
	select @sum=count(1) from UserInfo where UserName=@UserName
	if(@sum>0)
	begin
		set  @num01=404
	end
	else
	begin
		insert into UserInfo values(@UserName,@UserPwd,@Sex,@Birthday,@Addres,@Remarks)
		set @num01=ISNULL( @@IDENTITY,0)
	end
go

declare @num02 int
exec sp_youer @num02 output,'张三','123456','女','1998/04/08','湖北省咸宁市通山县','我是臭弟弟06'
if(@num02=404)
begin
	print '添加失败'+convert(varchar(50),@num02)
end
else
begin
	print '添加成功'+convert(varchar(50),@num02)
end
select * from UserInfo

//查询表中有几条数据
select ISNULL(@@IDENTITY,0)  from UserInfo

Field data as text instead

Generally men and women will be converted to text in the form of a char (2) type, we will usually [0/1].

// 例子		0--男   1--女   《男女问题》
select sex=case when sex=0 then '男' when sex=1 then '女'end from UserInfo
Published an original article · won praise 0 · Views 20

Guess you like

Origin blog.csdn.net/m0_46253449/article/details/104774621