动态 Sql- 活动字段搜索条件问题
Posted
技术标签:
【中文标题】动态 Sql- 活动字段搜索条件问题【英文标题】:Dynamic Sql- Issue with the active field search criteria 【发布时间】:2012-07-12 20:38:52 【问题描述】:我正在创建一个动态 sql 查询以在表 tblEmployees
中进行搜索。在tblEmployees
我有一个名为active
的字段,所以我希望如果我们将active as 1 与其他参数一起提供,那么它应该搜索active=1 的记录,如果我们没有为任何值提供active,我的意思既不是1也不是 0 那么它应该使用我在查询中定义的其他参数进行搜索。
CREATE TABLE tblEmployees2
(
EmployeeID SMALLINT IDENTITY(1001,1) NOT NULL,
EmployeeName NVARCHAR(100) NOT NULL,
Department NVARCHAR(50) NOT NULL,
Designation NVARCHAR(50) NOT NULL,
JoiningDate DATETIME NOT NULL,
Salary DECIMAL(10,2) NOT NULL,
[Description] NVARCHAR(1000) NULL,
active Tinyint NULL
)
INSERT INTO tblEmployees
(EmployeeName, Department, Designation,
JoiningDate, Salary, [Description],active)
VALUES
('John Smith', 'IT Research', 'Research Analyst',
'02/08/2005', 23000.00, 'Analyst since 2005',1)
INSERT INTO tblEmployees
(EmployeeName, Department, Designation,
JoiningDate, Salary, [Description],active)
VALUES
('John Micheal', 'IT Operations', 'Manager',
'07/15/2007', 15000.00, NULL,0)
INSERT INTO tblEmployees
(EmployeeName, Department, Designation,
JoiningDate, Salary, [Description],active)
VALUES
('Will Smith', 'IT Support', 'Manager',
'05/20/2006', 13000.00, 'Joined last year as IT Support Manager',1)
还有动态sql--
/* Input Parameters */
Declare
@EmployeeName NVarchar(100),
@Department NVarchar(50),
@Designation NVarchar(50),
@StartDate DateTime,
@EndDate DateTime,
@Salary Decimal(10,2),
@active tinyint
set @active=1--------------if active is 1 then it returns result where active=1 and if we are not providing
--any value neither 1 nor 0 then it should return both active=1 and active=0
set @EmployeeName='joh'
/* Variable Declaration */
Declare @SQLQuery AS NVarchar(4000)
Declare @ParamDefinition AS NVarchar(2000)
/* Build the Transact-SQL String with the input parameters */
Set @SQLQuery = 'Select * From tblEmployees where (1=1) '
/* check for the condition and build the WHERE clause accordingly */
if @active=1
set @SQLQuery=@active + ' And (active = @active)'
If @EmployeeName Is Not Null
Set @SQLQuery = @SQLQuery + ' And (EmployeeName LIKE '''+ '%' + @EmployeeName + '%' + ''')'
If @Department Is Not Null
Set @SQLQuery = @SQLQuery + ' And (Department = @Department)'
If @Designation Is Not Null
Set @SQLQuery = @SQLQuery + ' And (Designation = @Designation)'
If @Salary Is Not Null
Set @SQLQuery = @SQLQuery + ' And (Salary >= @Salary)'
If (@StartDate Is Not Null) AND (@EndDate Is Not Null)
Set @SQLQuery = @SQLQuery + ' And (JoiningDate
BETWEEN @StartDate AND @EndDate)'
/* Specify Parameter Format for all input parameters included
in the stmt */
Set @ParamDefinition = ' @EmployeeName NVarchar(100),
@Department NVarchar(50),
@Designation NVarchar(50),
@StartDate DateTime,
@EndDate DateTime,
@Salary Decimal(10,2),
@active tinyint'
/* Execute the Transact-SQL String with all parameter value's
Using sp_executesql Command */
Execute sp_Executesql @SQLQuery,
@ParamDefinition,
@EmployeeName,
@Department,
@Designation,
@StartDate,
@EndDate,
@Salary,
@active
print @SQLQuery
【问题讨论】:
你能解释一下这里出了什么问题吗?该代码似乎可以执行您想要的操作。 SQL 语句仅在@active = 1
时检查活动标志。你是不是想说if @active IN (0,1)
?这将允许您传递 NULL
并且该列不会被视为查询的一部分。实际上,如果您使用set @active = 0
,那么您将同时返回active = 0
和active = 1
,因为该子句未添加到查询中。但正如我所说,除了发布您的代码之外,请解释您在代码中遇到的实际问题。
@Aaron,是的,它似乎可以工作,但我得到这个异常'将 varchar 值'和(活动 = @活动)'转换为数据类型 tinyint 时转换失败。'
以后,请发布您收到的错误消息。如果您没有强迫我们解析您的所有代码并尝试猜测可能出了什么问题,您的问题将在大约 17 分钟前得到回答。 dba.blogoverflow.com/2012/06/help-us-help-you
是的@Aaron ..从那以后我会提供适当的信息..是的,这是我的错。很抱歉。
【参考方案1】:
改变这个:
set @SQLQuery=@active + ' And (active = @active)'
到这里:
set @SQLQuery = @SQLQuery + ' And (active = @active)'
为树造林?
【讨论】:
感谢@Aaron 现在它对我有用。非常感谢您的努力。以上是关于动态 Sql- 活动字段搜索条件问题的主要内容,如果未能解决你的问题,请参考以下文章
[转]使用exec和sp_executesql动态执行SQL语句
LINQ to SQL 可以查询 XML 字段 DB-serverside 吗?