在存储过程中没有得到任何结果
出于某种原因,每次我运行 exec
For some reason, every time I run exec
communications_getCode @telCode='MX'
我得到空结果.我知道我错过了一些东西,因为如果我跑
I get empty results. I know I am missing something because if I run
Select * from tbl_telCode where code = 'MX'
我得到了结果(准确地说是 1).但是如果我用这个程序尝试它,我得到的结果是空白
I get results (1 to be precise). But if I try it with the procedure, I get blank results
CREATE PROCEDURE dbo.communications_getCode
@telcode varchar
AS
SELECT
id, code, detail
FROM
tbl_telCode
WHERE
[code] = @telcode;
我不知道我错过了什么.
I do not know what am I missing.
声明中的varchar
默认为varchar(1)
.当您传递更长的字符串时,它会被截断为一个字符.
The varchar
in the declaration defaults to varchar(1)
. When you pass a longer string, it gets truncated to one character.
在 SQL Server 中,总是使用字符串定义的长度:
In SQL Server, always user a length with string definitions:
CREATE PROCEDURE dbo.communications_getCode (
@telcode varchar(255)
) AS
BEGIN
SELECT id, code, detail
FROM tbl_telCode
WHERE [code] = @telcode;
END;
请注意,存储过程的主体包含在 BEGIN
/END
中.我发现这是一个有用的做法.
Note that the body of the stored procedure is wrapped in a BEGIN
/END
. I find this to be a useful practice.
此外,没有理由为此定义存储过程.在我看来,将其定义为函数会更好.
Also, there is no reason to define a stored procedure for this. In my opinion, this would be better defined as a function.