本文介绍了如何为两个不同查询中包含不同where条件的select查询创建存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想执行以下两个查询: I want to execute two queries as below:string str = "select tblPersonalDetails.Email,tblPersonalDetails.FirstName,tblPersonalDetails.LastName,tblPersonalDetails.Gender,tblPersonalDetails.Mobile,tblPersonalDetails.Email,tblPersonalDetails.ValidPassport,tblPersonalDetails.Sources,tblPersonalDetails.JoinDate,tblEducationalDetails.XthPercentage,tblEducationalDetails.XIIthPercentage,tblEducationalDetails.GCourse,tblEducationalDetails.GPercentage,tblEducationalDetails.PGCourse,tblEducationalDetails.PGPercentage from tblPersonalDetails Inner Join tblEducationalDetails on tblPersonalDetails.ID=tblEducationalDetails.ID where (tblPersonalDetails.FirstName like '%' + @search + '%' )"; sting str1 ="select tblPersonalDetails.ID,tblPersonalDetails.FirstName,tblPersonalDetails.LastName,tblPersonalDetails.Gender,tblPersonalDetails.Mobile,tblPersonalDetails.Email,tblPersonalDetails.ValidPassport,tblPersonalDetails.Sources,tblPersonalDetails.JoinDate,tblEducationalDetails.XthPercentage,tblEducationalDetails.XIIthPercentage,tblEducationalDetails.GCourse,tblEducationalDetails.GPercentage,tblEducationalDetails.PGCourse,tblEducationalDetails.PGPercentage from tblPersonalDetails Inner Join tblEducationalDetails on tblPersonalDetails.ID=tblEducationalDetails.ID where (tblPersonalDetails.Gender like '%' + @search + '%' )";如何通过创建存储过程来实现?How to do it by creating stored procedure?推荐答案 CREATE PROCEDURE SearchBy2Fields @search NVARCHAR(500)ASBEGINSELECT pd.ID, pd.FirstName, pd.LastName, pd.Gender, pd.Mobile, pd.Email, pd.ValidPassport, pd.Sources, pd.JoinDate, ed.XthPercentage, ed.XIIthPercentage, ed.GCourse, ed.GPercentage, ed.PGCourse, ed.PGPercentageFROM tblPersonalDetails AS pd Inner Join tblEducationalDetails AS ed on pd.ID=ed.IDWHERE (pd.FirstName like '%@search%') OR (pd.Gender like '%@search%') END 进一步信息,请参阅:创建过程(Transact-SQL) [ ^ ] DECLARE @dynSql as varchar(max)SET @dynSql = 'select A.ID, A.FirstName,A.LastName,A.Gender,A.Mobile,A.Email,A.ValidPassport,A.Sources,'SET @dynSql = @dynSql + 'A.JoinDate,B.XthPercentage,B.XIIthPercentage,B.GCourse, B.GPercentage,B.PGCourse,B.PGPercentage 'SET @dynSql = @dynSql + 'from tblPersonalDetails A Inner Join tblEducationalDetails B on A.ID=B.ID 'SET @dynSql = @dynSql + 'where A.' + @colName + ' like ''%' + @search + '%''' 然后你可以 sp_execute [ ^ ]声明。 注意我还使用表的别名将查询缩短为可读状态then you can sp_execute[^] the statement.Note I've also used aliases for the tables to shorten the query into a readable state 这篇关于如何为两个不同查询中包含不同where条件的select查询创建存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-26 19:32