我在SQL Server 2014中有以下存储过程:
CREATE PROCEDURE [dbo].[GetOperatorNamesByComputer]
(@computer nvarchar,
@startdate datetime,
@enddate datetime)
AS
BEGIN
SELECT
ROW_NUMBER() OVER (ORDER BY MachineID) AS ID,
MachineID, User, [Datetime]
FROM
MyTable
WHERE
MachineID LIKE @computer + '%'
AND ([Datetime] BETWEEN @startdate AND @enddate)
AND Description = 'Interesting Data'
END
我在MyTable
:
MachineID User Datetime Description
------------------------------------------------------------------
F8;rweoi32234 person1 2017-10-31 00:01:01 Interesting Data
Test;fjdkkk11 person2 2017-10-20 00:02:00 Interesting Data
当我执行这样的存储过程时:
EXEC [dbo].[GetOperatorNamesByComputer]
@computer = N'F7',
@startdate = N'2017/10/01', @enddate = N'2017/10/31'
GO
它返回以F8开头的行,这似乎违反了我的搜索参数。实际上,输入以@computer
param中的字母F开头的任何内容都将返回以F开头的所有行,尽管param中也包含哪些字符。
但是当我执行常规查询时:
SELECT
ROW_NUMBER() OVER (ORDER BY MachineID) AS ID,
MachineID, User, [Datetime]
FROM
MyTable
WHERE
MachineID LIKE 'F7%'
AND ([Datetime] BETWEEN '2017/10/01' AND '2017/10/31')
AND Description = 'Interesting Data'
它没有按预期返回任何行,当将MachineID部件更改为“F8%”时,它将返回一行。
这是存储过程中LIKE子句的预期行为,还是我在将查询转换为存储过程时所做的错误?
感谢。
答案 0 :(得分:1)
存储过程声明中的abstract
参数nvarchar
需要长度。与@computer