重要参考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吗?查询指定节点及其所有父节 ...
随机推荐
- python __双划线 参数
''' >>> Class1.__doc__ # 类型帮助信息 'Class1 Doc.' >>> Class1.__name__ # 类型名称 'Class1' ...
- 20141110的alltosun面试
今天周一,是校招的第一天,心情有点紧张,不过可以和很多同学一起去,是我紧张的心情变得稍微安静些.面试进行的时候,是学长2哥面的我,总体感觉自己的表现很糟糕,在公共场合发表言论或者演讲,一直是我的一个弱 ...
- POJ-2891 Strange Way to Express Integers(拓展中国剩余定理)
放一个写的不错的博客:https://www.cnblogs.com/zwfymqz/p/8425731.html POJ好像不能用__int128. #include <iostream> ...
- idea 启动java项目报错 java: 程序包javax.servlet.http不存在
File -- Project Structure
- hash路由
class HashRouter{ constructor(){ //用于存储不同hash值对应的回调函数 this.routers = {}; window.addEventListener('ha ...
- 2016-2017学年第三次测试赛 习题E 林喵喵算术
时间限制: 1 Sec 内存限制: 128 MB 提交: 70 解决: 25 提交统计讨论版 题目描述 给你两个八进制数,你需要在八进制计数法的情况下计算a-b. 如果结果为负数,你应该使用负号代 ...
- ubuntu 16.04 XDRP实现Windows远程访问
如何通过XDRP实现Windows远程访问 下面才是本文的重点,本文主要是讲xrdp在目前最新版Ubuntu 16.04下,如果实现Windows远程访问.网上也很多相关教程,但是都需要安装xfac4 ...
- 对FPM 模块进行参数优化!
Nginx 的 PHP 解析功能实现如果是交由 FPM 处理的,为了提高 PHP 的处理速度,可对FPM 模块进行参数跳转.FPM 优化参数:pm 使用哪种方式启动 fpm 进程,可以说 static ...
- WPF TreeGrid Binding 简易实现方式
在設計TreeView編輯狀況下,希望 TreeItemName 后续的编辑框 复选框 可以整齐排列. 参考微软提供的TREELISTVIEW,发现它是根据层级关系调整Margin 属性. 我这边按照 ...
- 工具,Linux - tree命令,显示程序树型结构
sudo apt-get install tree tree --help