• . SQL多条件查询存储过程


    编辑器加SQL多条件查询存储过程 2010-05-13 17:06:29| 分类: SQL | 标签: |字号大中小 订阅 . 例一、 ALTER proc SelectProduct @ProdID varchar(10), @ProdName nvarchar(30), @CategoryID varchar(5), @MinPrice decimal(10,2), @MaxPrice decimal(10,2) as declare @sql varchar(2000) set @sql='select * from Product where 1=1' if @ProdID<>'' set @sql=@sql+' and ProdID like ''%'+@ProdID+'%''' if @ProdName<>'' set @sql=@sql+' and ProdName like ''%'+@ProdName+'%''' if @CategoryID<>'ALL' set @sql=@sql+' and CategoryID='''+@CategoryID+'''' if @MinPrice<>-1 set @sql=@sql+' and Price>='+cast(@MinPrice as varchar) if @MaxPrice<>-1 set @sql=@sql+' and Price<='+cast(@MaxPrice as varchar) set @sql=@sql+' order by AddTime desc' --print @sql exec(@sql) 例二、 CREATE PROC KY_UsedCar_AdvancedSearch ( @pinpai varchar(35)=null, --1、品牌 @chexing varchar(30)=null, --2、车型 @nianfen varchar(20)=null, --3、车龄 @cheliangsuozaidi varchar(20)=null, --4、省份 @chengshi varchar(20)=null, --5、城市 @xingshigonglishu int=null, --6、行驶里程 @biansuxiang varchar(20)=null, --7、变速箱类型 @jiage int=null, --8、价格区间 @yanse varchar(10)=null --9、颜色 --10、关键字 ) AS declare @sqlStr varchar(500) ----品牌 if @pinpai <>'' begin if @sqlStr <>'' set @sqlStr=@sqlStr+' and pinpai='+''''+@pinpai+'''' else set @sqlStr=' where pinpai='+''''+@pinpai+'''' end /*begin set @sqlStr=' where pinpai='+''''+@pinpai+'''' end*/ ----车型 if @chexing <>'' begin if @sqlStr <>'' set @sqlStr=@sqlStr+' and chexing='+''''+@chexing+'''' else set @sqlStr=' where chexing='+''''+@chexing+'''' end ----车龄 if @nianfen <>'' begin if @sqlStr <>'' set @sqlStr=@sqlStr+' and nianfen='+''''+@nianfen+'''' else set @sqlStr=' where nianfen='+''''+@nianfen+'''' end ----省份 if @cheliangsuozaidi <>'' begin if @sqlStr <>'' set @sqlStr=@sqlStr+' and cheliangsuozaidi='+''''+@cheliangsuozaidi+'''' else set @sqlStr=' where cheliangsuozaidi='+''''+@cheliangsuozaidi+'''' end ----城市 if @chengshi <>'' begin if @sqlStr <>'' set @sqlStr=@sqlStr+' and nianfen='+''''+@chengshi+'''' else set @sqlStr=' where nianfen='+''''+@chengshi+'''' end ----行驶里程 if @xingshigonglishu <>'' begin if @sqlStr <>'' set @sqlStr=@sqlStr+' and xingshigonglishu='+''''+@xingshigonglishu+'''' else set @sqlStr=' where xingshigonglishu='+''''+@xingshigonglishu+'''' end ----变速箱类型 if @biansuxiang <>'' begin if @sqlStr <>'' set @sqlStr=@sqlStr+' and biansuxiang='+''''+@biansuxiang+'''' else set @sqlStr=' where biansuxiang='+''''+@biansuxiang+'''' end ----区间价格 if @jiage <>'' begin if @sqlStr <>'' set @sqlStr=@sqlStr+' and jiage='+''''+@jiage+'''' else set @sqlStr=' where jiage='+''''+@jiage+'''' end ----颜色 if @yanse <>'' begin if @sqlStr <>'' set @sqlStr=@sqlStr+' and yanse='+''''+@yanse+'''' else set @sqlStr=' where yanse='+''''+@yanse+'''' end EXEC('select * from Ky_U_UsedCarResource'+@sqlStr) GO 例三:http://topic.csdn.net/u/20090313/09/8e851d7b-00a6-4ce0-9797-fffe19845f43.html 我表结构如下: IF EXISTS (SELECT * FROM sysobjects WHERE [NAME]='Employees') DROP TABLE Employees /******************************************************************************** -- --Action:创建员工信息表(Employees) --Author:123 --Create-Date:2009-3-13 *********************************************************************************/ CREATE TABLE Employees ( [Id] int IDENTITY(1,1) CONSTRAINT PK_EE_Id PRIMARY KEY([Id]) not null, --员工信息主键列,自动标识主键列 E_no nvarchar(20) not null, --员工身份编号 E_name nvarchar(20) not null, --员工名称 E_six varchar(10) not null, --员工性别 E_Did int CONSTRAINT FK_E_DId FOREIGN KEY(E_DId) REFERENCES Depts([Id]) not null, --员工部门编号,Depts表[Id]的编号 E_born Datetime not null, --员工出生年月 E_native nvarchar(100)null, --员工籍贯 E_address nvarchar(100) null, --员工现住地址 E_email nvarchar(20) null, --员工Email E_phone nvarchar(30) null, --联系电话,多个电话“;”号隔开 E_telphone nvarchar(30) null, --联系人移动电话,多个电话用“;”号隔开 E_qq nvarchar(15) null, --员工QQ E_datetime datetime not null, --员工入职日期 E_picpath nvarchar(30) null, --员工相片路径 E_work nvarchar(30) null, --员工职位 E_login nvarchar(20) not null, --员工账号 E_pwd nvarchar(20) not null, --员工密码 E_Rid int CONSTRAINT FK_E_RId FOREIGN KEY(E_RId) REFERENCES Roles([Id])not null,--员工权限编号,Roles表[Id]的外键 E_remark nvarchar(200) null, --备注 ) 我要实现根据 E_no、E_name、E_Did、E_work、E_Rid 这5个条件来查询, CREATE PROCEDURE employees_select_SQL_proc @name nvarchar(20), @did nvarchar(3), @work nvarchar(10), @rid nvarchar(3) AS DECLARE @sql nvarchar(1000) SET @sql='SELECT * FROM employees WHERE 1=1' IF(@name IS NOT NULL) BEGIN SET @sql=@sql+' AND E_name LIKE ''%'+@name+'%'' ' END IF(@did IS NOT NULL) BEGIN SET @sql=@sql+' AND E_Did ='+@did END IF(@work IS NOT NULL) BEGIN SET @sql=@sql+' AND E_work LIKE ''%'+@work+'%'' ' END IF(@rid IS NOT NULL) BEGIN SET @sql=@sql+' AND E_Rid ='+@rid END EXEC(@sql) GO 用 print(@sql) 看一下你最後的SQL語句是否正確. 当将非数字类型的 char、nchar、varchar 或 nvarchar 数据转换为 int、float、numeric 或 decimal 时,SQL Server 将返回错误信息。” 具体位置可以用CONVERT关键字在丛书中搜 载中...
  • 相关阅读:
    26. 删除排序数组中的重复项
    巧记“指针数组”与“数组指针”
    在 VC 下清空键盘缓冲区的方法
    负数、取模与取余
    任意键开始、暂停
    int 越界处理
    防闪屏批量绘图
    VC 下如何正确的创建及管理项目
    CSDN博客步骤:
    61 扑克牌中的顺子
  • 原文地址:https://www.cnblogs.com/rc727512646/p/2575461.html
Copyright © 2020-2023  润新知