重要参考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吗?查询指定节点及其所有父节 ...
随机推荐
- 模仿虎牙App 导航栏切换
昨天看虎牙直播,发现导航栏挺有意思,自己也做个玩玩 <view class="tab_list row"> <view class="tab_item ...
- Kubernetes的service资源介绍
service 三种工作模式:userspace.iptables.ipvs 删除手动创建的service [root@master ~]# kubectl delete svc redis serv ...
- JSP页面中关于<c:if test="${...}"><c:if>标签的用法
代码如下: <td class="showTd_HK" align="center"> <c:if test="${(rwyy01. ...
- 「Luogu P1975 [国家集训队]排队」
题目大意 给出一个序列 \(h\),支持交换其中的两数,求出每一时刻的逆序对个数. 分析 求逆序对是 \(O(N\log_2N)\) 的,有 \(M\) 个操作,如果暴力求的话时间复杂度就是 \(O( ...
- css简单整理
style1.css /*统一设置h1.h2.a标签的样式*/ h1,h2,a{ color: aqua; font-size: 50px; } /*如果h1.h2没有上面的单独设置样式那么就会继承b ...
- maven热部署
1.启动tomcat 2.修改 tomat/conf/tomcat-users.xml 配置用户名.密码.角色 manager-gui:图形界面的权限(调试时配置) man ...
- Mockito 中文文档 ( 2.0.26 beta )
Mockito 中文文档 ( 2.0.26 beta ) 由于缺乏校对,难免有谬误之处,如果发现任何语句不通顺.翻译错误,都可以在github中的项目提出issue.谢谢~ Mockito框架官方地址 ...
- js 字符串相关函数
https://www.jb51.net/article/74614.htm
- [转]使用Struts 2防止表单重复提交
转自 用户重复提交表单在某些场合将会造成非常严重的后果.例如,在使用信用卡进行在线支付的时候,如果服务器的响应速度太慢,用户有可能会多次点击提交按钮,而这可能导致那张信用卡上的金额被消费了多次.因此, ...
- Mac. 修改bash_file
https://www.cnblogs.com/mokey/p/3542389.html