重要参考SQL
--保存问卷调查信息
IF ISNULL(OBJECT_ID('P_WebSaveSQRecord'),0) > 0
DROP PROCEDURE P_WebSaveSQRecord
GO
create procedure P_WebSaveSQRecord
@C_CardNum varchar(20), --用户卡号
@I_RoomID int, --房间链号(没有指定时通过用户卡号检索)
@C_ChargeYear varchar(50),
@I_TitleID int, --问卷标题ID
@C_Remark varchar(100) = '',
@C_Operator varchar(50),
@I_QuestionID01 int = 0, --问卷问题ID01
@I_AnswerID01 int = 0, --问卷答案ID01
@C_UserAnswer01 varchar(1000) = '', --问卷答案01
@C_Remark01 varchar(100) = '', --备注01
@I_QuestionID02 int = 0, --问卷问题ID02
@I_AnswerID02 int = 0, --问卷答案ID02
@C_UserAnswer02 varchar(1000) = '', --问卷答案02
@C_Remark02 varchar(100) = '', --备注02
@I_QuestionID03 int = 0, --问卷问题ID03
@I_AnswerID03 int = 0, --问卷答案ID03
@C_UserAnswer03 varchar(1000) = '', --问卷答案03
@C_Remark03 varchar(100) = '', --备注03
@I_QuestionID04 int = 0, --问卷问题ID04
@I_AnswerID04 int = 0, --问卷答案ID04
@C_UserAnswer04 varchar(1000) = '', --问卷答案04
@C_Remark04 varchar(100) = '', --备注04
@I_QuestionID05 int = 0, --问卷问题ID05
@I_AnswerID05 int = 0, --问卷答案ID05
@C_UserAnswer05 varchar(1000) = '', --问卷答案05
@C_Remark05 varchar(100) = '', --备注05
@I_QuestionID06 int = 0, --问卷问题ID06
@I_AnswerID06 int = 0, --问卷答案ID06
@C_UserAnswer06 varchar(1000) = '', --问卷答案06
@C_Remark06 varchar(100) = '', --备注06
@Image1 image = null, --附件
@Ext1 varchar(10) = 'jpg', --附件扩展名
@I_RecordID int = 0 output, --ID
@ErrStr varchar(100) = '' output
as
begin
declare @C_CompanyName varchar(100) --公司名称
declare @I_VoucherID int
declare @TranCount int
declare @ErrIndex int
--附件变量
declare @ImageTableName varchar(50)
declare @ImageName varchar(50)
declare @SQL nvarchar(1000)
declare @PAM nvarchar(1000)
--特殊业务(手机号码)
declare @NewTouch varchar(50) --新手机号码
declare @I_QuestionIDTouch int --问卷问题ID(手机号码用)
declare @I_AnswerIDTouch int --问卷答案ID(手机号码用)
------------------------------------------------
--初期值
select @ErrStr = '', @NewTouch = '', @ErrIndex = -1
select @C_CompanyName = C_CompanyName from B_Company
if IsNull(@C_ChargeYear,'') = '' set @C_ChargeYear = dbo.GetYear() --年度
if IsNull(@C_Operator,'') = '' set @C_Operator = '系统管理员'
select
@I_QuestionID01=ISNULL(@I_QuestionID01,0), @I_AnswerID01=ISNULL(@I_AnswerID01,0),
@C_UserAnswer01=ISNULL(@C_UserAnswer01,''), @C_Remark01=IsNull(@C_Remark01,''),
@I_QuestionID02=ISNULL(@I_QuestionID02,0), @I_AnswerID02=ISNULL(@I_AnswerID02,0),
@C_UserAnswer02=ISNULL(@C_UserAnswer02,''), @C_Remark02=IsNull(@C_Remark02,''),
@I_QuestionID03=ISNULL(@I_QuestionID03,0), @I_AnswerID03=ISNULL(@I_AnswerID03,0),
@C_UserAnswer03=ISNULL(@C_UserAnswer03,''), @C_Remark03=IsNull(@C_Remark03,''),
@I_QuestionID04=ISNULL(@I_QuestionID04,0), @I_AnswerID04=ISNULL(@I_AnswerID04,0),
@C_UserAnswer04=ISNULL(@C_UserAnswer04,''), @C_Remark04=IsNull(@C_Remark04,''),
@I_QuestionID05=ISNULL(@I_QuestionID05,0), @I_AnswerID05=ISNULL(@I_AnswerID05,0),
@C_UserAnswer05=ISNULL(@C_UserAnswer05,''), @C_Remark05=IsNull(@C_Remark05,''),
@I_QuestionID06=ISNULL(@I_QuestionID06,0), @I_AnswerID06=ISNULL(@I_AnswerID06,0),
@C_UserAnswer06=ISNULL(@C_UserAnswer06,''), @C_Remark06=IsNull(@C_Remark06,'')
------------------------------------------------
--附件数据库名称
select @ImageTableName = isnull(C_Value,'')
from T_OtherConfig
where c_Name = '附件数据库名'
if @@ROWCOUNT>0 and @ImageTableName<>''
begin
set @ImageTableName = @ImageTableName + '.dbo.T_PubImage'
end else
begin
set @ImageTableName = 'T_PubImage'
end
------------------------------------------------
if (@C_CompanyName = '四平热力有限公司')
begin
select
@I_QuestionIDTouch = b.ID, --问卷问题ID(手机号码用)
@I_AnswerIDTouch = c.ID --问卷答案ID(手机号码用)
from T_SQTitle a
inner join T_SQQuestions b on a.ID = b.I_TitleID
inner join T_SQAnswer c on b.ID = c.I_Question
where (a.ID = @I_TitleID) and
(b.ID in (@I_QuestionID01,@I_QuestionID02,@I_QuestionID03,@I_QuestionID04,@I_QuestionID05,@I_QuestionID06)) and
(c.ID in (@I_AnswerID01,@I_AnswerID02,@I_AnswerID03,@I_AnswerID04,@I_AnswerID05,@I_AnswerID06)) and
(b.C_QuestionText = '您正确的手机号码') and
(c.C_AnswerNum = '手机号码')
end
select @I_QuestionIDTouch = IsNull(@I_QuestionIDTouch,0), @I_AnswerIDTouch = IsNull(@I_AnswerIDTouch,0)
------------------------------------------------
--事务开始
select @TranCount = @@TRANCOUNT
if @TranCount = 0
begin transaction
else
save transaction T_BAK
------------------------------------------------
--检查参数中的ID在数据库中是否都存在
if (@I_QuestionID01 > 0) or (@I_AnswerID01 > 0)
begin
if not exists(select 1
from T_SQTitle a
inner join T_SQQuestions b on a.ID = b.I_TitleID
inner join T_SQAnswer c on b.ID = c.I_Question
where (a.ID = @I_TitleID) and
(b.ID = @I_QuestionID01) and
(c.ID = @I_AnswerID01))
begin
select @ErrIndex = 1, @ErrStr = '调查问卷1参数错误'
goto Err
end
end
if (@I_QuestionID02 > 0) or (@I_AnswerID02 > 0)
begin
if not exists(select 1
from T_SQTitle a
inner join T_SQQuestions b on a.ID = b.I_TitleID
inner join T_SQAnswer c on b.ID = c.I_Question
where (a.ID = @I_TitleID) and
(b.ID = @I_QuestionID02) and
(c.ID = @I_AnswerID02))
begin
select @ErrIndex = 1, @ErrStr = '调查问卷2参数错误'
goto Err
end
end
if (@I_QuestionID03 > 0) or (@I_AnswerID03 > 0)
begin
if not exists(select 1
from T_SQTitle a
inner join T_SQQuestions b on a.ID = b.I_TitleID
inner join T_SQAnswer c on b.ID = c.I_Question
where (a.ID = @I_TitleID) and
(b.ID = @I_QuestionID03) and
(c.ID = @I_AnswerID03))
begin
select @ErrIndex = 1, @ErrStr = '调查问卷3参数错误'
goto Err
end
end
if (@I_QuestionID04 > 0) or (@I_AnswerID04 > 0)
begin
if not exists(select 1
from T_SQTitle a
inner join T_SQQuestions b on a.ID = b.I_TitleID
inner join T_SQAnswer c on b.ID = c.I_Question
where (a.ID = @I_TitleID) and
(b.ID = @I_QuestionID04) and
(c.ID = @I_AnswerID04))
begin
select @ErrIndex = 1, @ErrStr = '调查问卷4参数错误'
goto Err
end
end
if (@I_QuestionID05 > 0) or (@I_AnswerID05 > 0)
begin
if not exists(select 1
from T_SQTitle a
inner join T_SQQuestions b on a.ID = b.I_TitleID
inner join T_SQAnswer c on b.ID = c.I_Question
where (a.ID = @I_TitleID) and
(b.ID = @I_QuestionID05) and
(c.ID = @I_AnswerID05))
begin
select @ErrIndex = 1, @ErrStr = '调查问卷5参数错误'
goto Err
end
end
if (@I_QuestionID06 > 0) or (@I_AnswerID06 > 0)
begin
if not exists(select 1
from T_SQTitle a
inner join T_SQQuestions b on a.ID = b.I_TitleID
inner join T_SQAnswer c on b.ID = c.I_Question
where (a.ID = @I_TitleID) and
(b.ID = @I_QuestionID06) and
(c.ID = @I_AnswerID06))
begin
select @ErrIndex = 1, @ErrStr = '调查问卷6参数错误'
goto Err
end
end
------------------------------------------------
--没有指定房间链号的,根据用户卡号取得
if (IsNull(@I_RoomID,0) <= 0)
begin
select @I_RoomID = I_RoomID from B_Room where C_CardNum = @C_Cardnum and I_ISArchives = 0
if (@@ERROR <> 0) or (@@ROWCOUNT <> 1)
begin
select @ErrIndex = 8, @ErrStr = '没有找到的用户'
goto Err
end
end
------------------------------------------------
exec P_GetVoucherIDInProc @I_VoucherID output --凭号
if (@@ERROR <> 0) or (@I_VoucherID <= 0)
begin
set @ErrIndex = 1
set @ErrStr = '获取凭证号失败'
goto Err
end
------------------------------------------------
--01.插入数据
insert into T_SQRecord(I_RoomID, I_VoucherID, C_ChargeYear, D_CreateDate, I_TitleID, C_Remark)
values (@I_RoomID, @I_VoucherID, @C_ChargeYear, getdate(), @I_TitleID, @C_Remark)
if (@@ERROR <> 0) or (@@ROWCOUNT <> 1)
begin
select @ErrIndex = 1
set @ErrStr = '插入调查问卷数据失败'
goto Err
end
select @I_RecordID = max(id)
from T_SQRecord
where I_RoomID = @I_RoomID and C_ChargeYear = @C_ChargeYear
if IsNull(@I_RecordID,0) <= 0
begin
select @ErrIndex = 1
set @ErrStr = '获取调查问卷数据ID失败'
goto Err
end
------------------------------------------------
--02.插入各文字输入要保存的值
--02-01
if (@I_QuestionID01 > 0) and (@I_AnswerID01 > 0)
begin
insert into T_SQRecordDeatil(I_RecordID, I_QuestionID, I_AnswerID, C_UserAnswer, C_Remark)
values(@I_RecordID, @I_QuestionID01, @I_AnswerID01, @C_UserAnswer01, @C_Remark01)
if (@@ERROR <> 0) or (@@ROWCOUNT <> 1)
begin
select @ErrIndex = 1
set @ErrStr = '插入调查问卷明细失败01'
goto Err
end
if (@I_QuestionID01 = @I_QuestionIDTouch) and (@I_AnswerID01 = @I_AnswerIDTouch)
begin
set @NewTouch = @C_UserAnswer01 --新手机号码
end
end
--02-02
if (@I_QuestionID02 > 0) and (@I_AnswerID02 > 0)
begin
insert into T_SQRecordDeatil(I_RecordID, I_QuestionID, I_AnswerID, C_UserAnswer, C_Remark)
values(@I_RecordID, @I_QuestionID02, @I_AnswerID02, @C_UserAnswer02, @C_Remark02)
if (@@ERROR <> 0) or (@@ROWCOUNT <> 1)
begin
select @ErrIndex = 1
set @ErrStr = '插入调查问卷明细失败02'
goto Err
end
if (@I_QuestionID02 = @I_QuestionIDTouch) and (@I_AnswerID02 = @I_AnswerIDTouch)
begin
set @NewTouch = @C_UserAnswer02 --新手机号码
end
end
--02-03
if (@I_QuestionID03 > 0) and (@I_AnswerID03 > 0)
begin
insert into T_SQRecordDeatil(I_RecordID, I_QuestionID, I_AnswerID, C_UserAnswer, C_Remark)
values(@I_RecordID, @I_QuestionID03, @I_AnswerID03, @C_UserAnswer03, @C_Remark03)
if (@@ERROR <> 0) or (@@ROWCOUNT <> 1)
begin
select @ErrIndex = 1
set @ErrStr = '插入调查问卷明细失败03'
goto Err
end
if (@I_QuestionID03 = @I_QuestionIDTouch) and (@I_AnswerID03 = @I_AnswerIDTouch)
begin
set @NewTouch = @C_UserAnswer03 --新手机号码
end
end
--02-04
if (@I_QuestionID04 > 0) and (@I_AnswerID04 > 0)
begin
insert into T_SQRecordDeatil(I_RecordID, I_QuestionID, I_AnswerID, C_UserAnswer, C_Remark)
values(@I_RecordID, @I_QuestionID04, @I_AnswerID04, @C_UserAnswer04, @C_Remark04)
if (@@ERROR <> 0) or (@@ROWCOUNT <> 1)
begin
select @ErrIndex = 1
set @ErrStr = '插入调查问卷明细失败04'
goto Err
end
if (@I_QuestionID04 = @I_QuestionIDTouch) and (@I_AnswerID04 = @I_AnswerIDTouch)
begin
set @NewTouch = @C_UserAnswer04 --新手机号码
end
end
--02-05
if (@I_QuestionID05 > 0) and (@I_AnswerID05 > 0)
begin
insert into T_SQRecordDeatil(I_RecordID, I_QuestionID, I_AnswerID, C_UserAnswer, C_Remark)
values(@I_RecordID, @I_QuestionID05, @I_AnswerID05, @C_UserAnswer05, @C_Remark05)
if (@@ERROR <> 0) or (@@ROWCOUNT <> 1)
begin
select @ErrIndex = 1
set @ErrStr = '插入调查问卷明细失败05'
goto Err
end
if (@I_QuestionID05 = @I_QuestionIDTouch) and (@I_AnswerID05 = @I_AnswerIDTouch)
begin
set @NewTouch = @C_UserAnswer05 --新手机号码
end
end
--02-06
if (@I_QuestionID06 > 0) and (@I_AnswerID06 > 0)
begin
insert into T_SQRecordDeatil(I_RecordID, I_QuestionID, I_AnswerID, C_UserAnswer, C_Remark)
values(@I_RecordID, @I_QuestionID06, @I_AnswerID06, @C_UserAnswer06, @C_Remark06)
if (@@ERROR <> 0) or (@@ROWCOUNT <> 1)
begin
select @ErrIndex = 1
set @ErrStr = '插入调查问卷明细失败06'
goto Err
end
if (@I_QuestionID06 = @I_QuestionIDTouch) and (@I_AnswerID06 = @I_AnswerIDTouch)
begin
set @NewTouch = @C_UserAnswer06 --新手机号码
end
end
------------------------------------------------
--03.保存附件信息3
if (@Image1 is not null) and (DATALENGTH(@Image1) > 2)
begin
--附件名称
set @ImageName = '问卷调查附件'+CAST(@I_RecordID as varchar)
--插入附件
set @SQL = 'insert into '+@ImageTableName+'(c_type, c_ID, c_ImageName, c_ExName, D_AddDate, C_addBy, d_UpdateBy, d_update,i_RoomID, i_image) '+
' values (''问卷调查'',@ID,@C_ImageName,@ExName,getdate(),@C_Operator,@C_Operator,getdate(),@I_RoomID,@P1)'
set @PAM = N'@ID int, @C_ImageName varchar(50), @ExName varchar(20), @C_Operator varchar(50), @I_RoomID int, @P1 image'
exec sp_executesql @SQL,@PAM, @I_RecordID,@ImageName,@Ext1,@C_Operator,@I_RoomID,@Image1
if @@ROWCOUNT <> 1 or @@ERROR <> 0
begin
set @ErrIndex = 3
set @ErrStr = '保存附件失败'
goto Err
end
end
------------------------------------------------
--特殊业务(新手机号码)
if (@NewTouch <> '')
begin
declare @OldTouch varchar(50)
declare @c_num varchar(35)
select @OldTouch = C_Touch1
from B_Room
where I_RoomID=@I_RoomID
set @OldTouch = IsNull(@OldTouch,'')
if (@OldTouch <> @NewTouch)
begin
set @c_num = REPLACE(REPLACE(REPLACE(CONVERT(varchar(23),getdate(),20),'-',''),':',''),' ','')
-------------------------------------------
--触发器中用到了context_info,所以先设置,后清空
declare @Info varbinary(128)
set @Info = cast(@C_Operator as varbinary)
set context_info @Info
update B_BaseInfoSurvey_A set C_Touch2 = @NewTouch
where I_RoomID = @I_RoomID
if @@ERROR <> 0
begin
select @ErrIndex = 4, @ErrStr = '修改B_BaseInfoSurvey_A中手机号码失败'
goto Err
end
set @Info = cast('' as varbinary)
set context_info @Info
-------------------------------------------
update B_Room set C_Touch1 = @NewTouch
where I_RoomID=@I_RoomID
if @@ROWCOUNT <> 1 or @@ERROR <> 0
begin
select @ErrIndex = 4, @ErrStr = '修改B_Room中手机号码失败'
goto Err
end
insert into C_roommodirecord(I_RoomID,C_Operator,D_OperatDate,C_ChargeYear,C_ReMark,C_BZ,c_num)
values(@I_RoomID,@C_Operator,getdate(),dbo.GetYear(),'更改联系方式:'+@OldTouch+'→'+@NewTouch,'问卷调查修改',@c_num)
if @@ROWCOUNT < 1 or @@ERROR <> 0
begin
select @ErrIndex = 4, @ErrStr = '插入修改手机号码记录C_roommodirecord失败'
goto Err
end
delete from B_PhoneList where I_RoomID=@I_RoomID
if @@ERROR <> 0
begin
select @ErrIndex = 4, @ErrStr = '清空手机号码记录B_PhoneList失败'
goto Err
end
insert into B_PhoneList (C_PhoneNum,I_RoomID,I_UnitID,I_IsMain,I_IsMobile)
values(@NewTouch,@I_RoomID,'0','0',null)
if @@ROWCOUNT < 1 or @@ERROR <> 0
begin
select @ErrIndex = 4, @ErrStr = '插入手机号码记录B_PhoneList失败'
goto Err
end
insert into tmp_split_room(I_roomid, C_Num, C_ChargeYear, C_Operator, D_OperatDate,
C_Type, C_OldStr, C_NewStr, C_Remark)
values (@I_RoomID, @C_Num, dbo.GetYear(), @C_Operator, getdate(),
'联系方式', @OldTouch, @NewTouch, '问卷调查修改')
if @@ROWCOUNT < 1 or @@ERROR <> 0
begin
select @ErrIndex = 4, @ErrStr = '插入修改手机号码记录tmp_split_room失败'
goto Err
end
end
end
------------------------------------------------
if @TranCount = 0
commit transaction
select @ErrStr, @I_RecordID
return 0
ERR:
if @TranCount = 0
rollback transaction
else
rollback transaction T_BAK
select @ErrStr, @I_RecordID
return @ErrIndex
end
重要参考SQL的更多相关文章
- 性能检测参考SQL语句
/****** Object: StoredProcedure [dbo].[SP_CPU] Script Date: 12/09/2018 19:01:24 ******/ SET ANSI_NUL ...
- 数据库建表参考(SQL Server)
(1).字段设置为Not Null+Default Value.原因:减少三值判断,可为Null的字段要多判断null:另外,定长字段为null也占空间,变长字段为空字符串也是不占空间,所以设置成 ...
- ORACLE 检查数据库表中是否存在不规范字 段的语句参考.sql
--查看是否有除number,char,date,varchar2,clob/blob之外的类型,比如:NVARCHAR2,TIMESTAMP(6),FLOATSELECT DISTINCT a.DA ...
- SQL 语句快速参考
来自 W3CSchool 的 SQL 快速参考 SQL 语句 语法 AND / OR SELECT column_name(s)FROM table_nameWHERE conditionAND|OR ...
- SQL 快速参考
SQL 快速参考 SQL 语句 语法 AND / OR SELECT column_name(s)FROM table_nameWHERE conditionAND|OR condition ALTE ...
- Oracle数据库该如何着手优化一个SQL
这是个终极问题,因为优化本身的复杂性实在是难以总结的,很多时候优化的方法并不是用到了什么高深莫测的技术,而只是一个思想意识层面的差异,而这些都很可能连带导致性能表现上的巨大差异. 所以有时候我们应该先 ...
- 你真的会玩SQL吗?之逻辑查询处理阶段
你真的会玩SQL吗?系列目录 你真的会玩SQL吗?之逻辑查询处理阶段 你真的会玩SQL吗?和平大使 内连接.外连接 你真的会玩SQL吗?三范式.数据完整性 你真的会玩SQL吗?查询指定节点及其所有父节 ...
- 你真的会玩SQL吗?和平大使 内连接、外连接
你真的会玩SQL吗?系列目录 你真的会玩SQL吗?之逻辑查询处理阶段 你真的会玩SQL吗?和平大使 内连接.外连接 你真的会玩SQL吗?三范式.数据完整性 你真的会玩SQL吗?查询指定节点及其所有父节 ...
- 你真的会玩SQL吗?EXISTS和IN之间的区别
你真的会玩SQL吗?系列目录 你真的会玩SQL吗?之逻辑查询处理阶段 你真的会玩SQL吗?和平大使 内连接.外连接 你真的会玩SQL吗?三范式.数据完整性 你真的会玩SQL吗?查询指定节点及其所有父节 ...
随机推荐
- spark实验(四)--RDD编程(1)
一.实验目的 (1)熟悉 Spark 的 RDD 基本操作及键值对操作: (2)熟悉使用 RDD 编程解决实际具体问题的方法. 二.实验平台 操作系统:centos6.4 Spark 版本:1.5.0 ...
- 单点登录--sso系统
SSO英文全称Single Sign On,单点登录.SSO是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统.它包括可以将这次主要的登录映射到其他应用中用于同一个用户的登录的机制 ...
- MediaCreationTool制作WIN10安装U盘,安装纯净版win10的通用教程
注意: 1.准备8G或8G以上U盘. 2.安装系统前备份好个人需要数据(制作U盘会格式化U盘,U盘内有需要的数据也事先备份好) 3.有预装office的务必记住自己激活office账户和密码以免重装后 ...
- 吴裕雄--天生自然Numpy库学习笔记:NumPy 算术函数
NumPy 算术函数包含简单的加减乘除: add(),subtract(),multiply() 和 divide(). 需要注意的是数组必须具有相同的形状或符合数组广播规则. import nump ...
- 配置和验证AP功率
1.针对自主AP(Autonomous AP) 使用'power local'配置命令配置AP或Bridge Radio功率级别.在2.4 GHz,802.11g Radio上,您可以设置正交频分复用 ...
- ANSYS热分析简介1
目录 1. ANSYS热分析简介 1.1 传导 1.2 热载荷分类 1.2.1 载荷施加 1.3 热分析分类 1.3.1 稳态热分析 1.3.2 瞬态热分析 1.3.3 非线性分析综述 2. 热分析单 ...
- Myeclipse下PHP开发环境搭建及运行
外接CSDN链接 http://blog.csdn.net/yuxiangaaaaa/article/details/54948426 这是php初始设置,后面进行重新设置
- Python数据分析之Numpy操作大全
从头到尾都是手码的,文中的所有示例也都是在Pycharm中运行过的,自己整理笔记的最大好处在于可以按照自己的思路来构建矿建,等到将来在需要的时候能够以最快的速度看懂并应用=_= 注:为方便表述,本章设 ...
- component:(resolve) => require
resolve => require(['../pages/home.vue'], resolve)这种写法是异步模块获取,打包的时候每次访问这个路由的时候会单调单个文件,按需加载,不过这种写法 ...
- #P4770 [NOI2018]你的名字 的题解
题目背景 实力强大的小A 被选为了ION2018 的出题人,现在他需要解决题目的命名问题. 题目描述 小A 被选为了ION2018 的出题人,他精心准备了一道质量十分高的题目,且已经把除了题目命名以外 ...