在存储过程中没有得到任何结果 [英] Not getting any results in stored procedure

查看:35
本文介绍了在存储过程中没有得到任何结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,每次我运行 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.

这篇关于在存储过程中没有得到任何结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆