用存储过程实现用员工姓名查询员工信息我的思路是用创建存储过程建立视图再对视图进行查询操作,这涉及到了多个表的信息!而且存储过程运行后确没有创建视图!求解!!!代码如下CREATEPR
用存储过程实现用员工姓名查询员工信息
我的思路是用创建存储过程建立视图再对视图进行查询操作,这涉及到了多个表的信息!而且存储过程运行后确没有创建视图!
求解!!!
代码如下
CREATE PROCEDURE dbo.proc_search_test
@employee_name varchar(20) --定义存储过程输入参数变量
as
----检查要创建的视图是否存在,若存在请先清空
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[search_test]') and OBJECTPROPERTY(id, N'IsView') = 1)
drop view [dbo].[search_test]
--定义创建视图语句变量
declare @sql varchar(5000)
/*******************
组装SQL语句,创建变量, 其中含输入参数
********************/
set @sql='CREATE VIEW dbo.search_test
AS
SELECT dbo.accumulation_fund.presonal_bank_account, dbo.accumulation_fund.fund_place, dbo.accumulation_fund.salary_anomalies, '+
'dbo.basic_information_table.employee_id, dbo.basic_information_table.employee_name, dbo.basic_information_table.employee_sex,dbo.basic_information_table.idcard, dbo.basic_information_table.nation, dbo.basic_information_table.native_place, dbo.basic_information_table.domicile_place,dbo.basic_information_table.marriage, dbo.basic_information_table.tel, dbo.basic_information_table.political_place, '+ 'dbo.education_information.education_experiences, dbo.education_information.documents, dbo.education_information.highest_education,
dbo.education_information.specialty, dbo.education_information.graduate_institutions, dbo.education_information.graduate_time, dbo.education_information.degree,
dbo.education_information.english_level, '+
'dbo.employee_inherence_information.hobbies, dbo.employee_inherence_information.skill, dbo.employee_inherence_information.deficiency, '+
'dbo.recommendation_information.introducer, dbo.recommendation_information.department_id, '+
'dbo.employee_train_history.training_unit, dbo.employee_train_history.training_way, dbo.employee_train_history.training_time,
dbo.employee_train_history.class_hour, dbo.employee_train_history.content, dbo.employee_train_history.teacher, dbo.employee_train_history.cost, '+
'dbo.labor_contract.contract_num, dbo.labor_contract.start_time, dbo.labor_contract.end_time, dbo.labor_contract.employment_type,
dbo.labor_contract.employment_form, dbo.labor_contract.renew, '+
'dbo.social_insurance.insurance, dbo.social_insurance.insured_time, dbo.social_insurance.base_pay, dbo.social_insurance.social_security_num,
dbo.social_insurance.expend_scale, '+
'dbo.rewards_punishment_record.rew_pun_reasons, dbo.rewards_punishment_record.rew_pun_time, dbo.rewards_punishment_record.issuing_department,
dbo.rewards_punishment_record.issuing_time, dbo.rewards_punishment_record.issuing_man, '+
'dbo.performance_evaluation.appraisal_result,
dbo.performance_evaluation.appraisal_department,dbo.performance_evaluation.appraisal_time,dbo.performance_evaluation.appraisal_person '+
'dbo.departure_record.quit_time, dbo.departure_record.quit_department, dbo.departure_record.quit_reason, dbo.departure_record.blacklist,
dbo.departure_record.blacklist_reason, dbo.departure_record.blacklist_time, '+
'dbo.probation_period.should_be_regular_time, dbo.probation_period.actual_be_regular_time, dbo.probation_period.auditor, dbo.probation_period.audit_opinion,
'+
'FROM dbo.accumulation_fund '+
'dbo.basic_informao.tion_table '+
'dbo.education_information '+
'dbo.employee_inherence_information '+
'dbo.employee_train_history '+
'dbo.labor_contract '+
'dbo.social_insurance '+
'dbo.rewards_punishment_record '+
'dbo.performance_evaluation '+
'dbo.departure_record '+
'dbo.probation_period '+
'dbo.recommendation_information '+
'WHERE (dbo.basic_information_table.employee_id = ' + 'dbo.accumulation_fund.employee_id' + ') AND (dbo.basic_information_table.employee_id = ' +
'dbo.education_information.employee_id' + ') AND (dbo.basic_information_table.employee_id = ' + 'dbo.employee_inherence_information.employee_id' + ') AND
(dbo.basic_information_table.employee_id = ' + 'dbo.recommendation_information.employee_id' + ') AND (dbo.basic_information_table.employee_id = ' +
'dbo.employee_train_history.employee_id' + ') AND (dbo.basic_information_table.employee_id = ' + 'dbo.labor_contract.employee_id' + ') AND
(dbo.basic_information_table.employee_id = ' + 'dbo.social_insurance.employee_id' + ') AND (dbo.basic_information_table.employee_id = ' +
'dbo.rewards_punishment_record.employee_id' + ') AND (dbo.basic_information_table.employee_id = ' + 'dbo.performance_evaluation.employee_id' + ') AND
(dbo.basic_information_table.employee_id = ' + 'dbo.departure_record.employee_id' + ') AND (dbo.basic_information_table.employee_id = ' +
'dbo.probation_period.employee_id' + ') '+
'(dbo.accumulation_fund.employee_name = ' + @employee_name + ') '
--执行该语句创建视图
exec(@sql)
GO
/********************************************
代码结束
********************************************/
16 个解决方案
你的代码自己看了不头痛吗?
可以用表不是可以用别名吗。
你可以把 @sql 先print出来,你的代码肯定有问题
probation_period.audit_
opinion,
'+
'FROM dbo.accumulation_fund '+
而且from 多个表之间都没有,号。
另外你为什么不直接建个视图,然后存储过程中调用就可以了。
不建议每次执行存储过程时都删除/重建视图,
并发执行时很可能报视图对象不存在的错误.
我觉得不需要创建视图,然后再来查询这么麻烦。
既然你的语句都是动态生成的,直接运行语句不就可以了吗
动态脚本可以在exec之前先print出来,然后拿到外面执行一下看有没有问题,你的这个写的太凌乱了,没法看,
另外要注意定义变量的长度,长度不够的话,拼出来的sql是不完整的,在print前加一个select len(@sql)
你图上的“命令执行完成”,指的是你的这个 SP(存储过程) 创建成功了,你还得手工执行一下这个 SP 才可以。 参考 9# 的语法。