1、下面是创建存储过程SP_TFishSuperControl的脚本
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
use NewHn2015
go
if exists(select * from dbo.sysobjects where id=object_id('dbo.SP_TFishSuperControl'))--判断存储过程是否存在
begin
drop procedure dbo.SP_TFishSuperControl
end
go
create procedure dbo.SP_TFishSuperControl
@Flag tinyint, --操作属性
@UserID int, --用户ID
@UserName varchar(50), --用户昵称
@Degree decimal(2,1), --难度系数
@Pond bigint, --奖池额
@FindUserID int, --查询ID
@FindUserName varchar(50) output, --查询昵称
@FindDegree decimal(2,1) output, --查询难度系数
@FindPond bigint output --查询奖池额 --
as
begin
if @Flag=0--写入记录
begin
if exists(select * from TUsers where UserID=@UserID)--判断用户是否存在
begin
if not exists(select * from TFishSuperControl where UserID=@UserID)
begin
select @UserName=UserName from TUsers where UserID=@UserID--获得昵称
insert into TFishSuperControl(UserID,UserName,Degree,Pond) values(@UserID,@UserName,@Degree,@Pond)--添加记录
return 1--添加记录成功
end
else
begin
update TFishSuperControl set Degree=@Degree,Pond=@Pond--修改记录
return 0--修改记录成功
end
end
else
begin
return 2--用户不存在
end
end
else if @Flag=1--读记录
begin
if exists(select * from TUsers where UserID=@FindUserID)
begin
if exists(select * from TFishSuperControl where UserID=@FindUserID)
begin
select @FindUserName=UserName,@FindDegree=Degree,@FindPond=Pond
from TFishSuperControl where UserID=@FindUserID
return 10--获得记录
end
else
begin
return 11--获得记录失败
end
end
else
begin
return 12--用户不存在
end
end
end
2、执行存储过程
use NewHn2015
go
declare @Flag tinyint --声明调用变量
declare @UserID int
declare @UserName varchar(50)
declare @Degree decimal(2,1)
declare @Pond bigint
declare @FindUserID int
declare @FindUserName varchar(50)
declare @FindDegree decimal(2,1)
declare @FindPond bigint
declare @Result int
set @Flag=1 --赋值调用变量
set @UserID=10002
set @UserName=''
set @Degree=0.8
set @Pond=5000000
set @FindUserID=100022
set @FindUserName=''
set @FindDegree=0.0
set @FindPond=0
set @Result=-1
execute @Result=dbo.SP_TFishSuperControl @Flag,@UserID,@UserName,@Degree,@Pond,@FindUserID,@FindUserName output,@FindDegree output,@FindPond output
print @Result--输出存储过程返回值
print @FindUserName
print @FindDegree
print @FindPond
结论1:if语句判断是否相等用"="而不是"=="
结论2:浮点数使用decimal(2,1)声明,2表示有2位数字,其中小数占1位
结论3:判断数据库是否存在
if exists(select * from sys.databases where name='数据库名')
drop database 数据库名
结论4:判断表是否存在
if exists(select * from dbo.sysobjects where id = object_id('表名'))
drop table 表名
结论5:判断存储过程是否存在
if exists(select * from dbo.sysobjects where id = object_id('存储过程名'))
drop procedure 存储过程名
结论6:判断函数是否存在
if object_id('函数名') is null
drop function 函数名
结论7:判断不相等用"<>"
结论8:判断表中某一字段是否存在
IF COL_LENGTH('TGameRoomInfo', 'PrivateEnable') IS NULL
BEGIN
ALTER TABLE TGameRoomInfo ADD PrivateEnable TINYINT DEFAULT 0
END
结论9:加密存储过程
CREATE PROC SP_AAAAA
@dwUserID INT
WITH ENCRYPTION--加上加密属性
AS
BEGIN
-- 属性设置
SET NOCOUNT ON
END