-1

请查看以下存储过程:

ALTER PROCEDURE [dbo].[SP_GetList]
                             @fname nvarchar(50),
                             @lname nvarchar(50),
                             @dob   nvarchar(50),
                             @txtGroup varchar(50),
                             @Msg  VARCHAR(1000) OUT

AS
SET NOCOUNT ON;
-- added to prevent extra result sets from interfering with SELECT statements.

IF exists(SELECT 'AKRON' AS CodeName, a.voter_id AS voter_id_vre, a.cnty_Group, a.district_combo, a.voter_fname, a.voter_mname, a.voter_lname, a.voter_suffix, COALESCE(a.str_number,'')+' '+COALESCE(a.str_name,'')+' '+COALESCE(a.str_suffix,'')+' '+COALESCE(a.str_apt,'') AS res_addr,
                a.str_city, 'OH' AS str_state, a.str_zip, COALESCE(a.mail_str_num,'')+'   '+COALESCE(a.mail_str_name,'')+' '+COALESCE(a.mail_srt_suff,'')+' '+COALESCE(a.mail_apt,'')+' '+COALESCE(a.mail_city,'')+'   '+COALESCE(a.mail_state,'')+'   '+COALESCE(a.mail_zip,'') AS mail_address, a.dob, (SELECT COUNT(*) FROM dbo.voter_record_supp WHERE dbo.voter_record_supp.voter_ID = a.voter_ID) AS [exists], b.* FROM dbo.voter_record a LEFT OUTER JOIN dbo.voter_record_supp b ON a.voter_id=b.voter_id
                WHERE a.voter_fname Like '%fname%' And a.voter_lname Like '%@lname%' And a.dob = @dob 
                AND voter_status IN ('A','I') AND cnty_Group NOT LIKE 'ML%' AND  cnty_Group NOT LIKE 'PA%' AND cnty_Group NOT LIKE 'MP%' AND cnty_Group NOT LIKE 'CH%' AND cnty_Group NOT LIKE 'SC%' AND cnty_Group NOT LIKE 'CP%')
 BEGIN
     SET @Msg = Participants in Group' + @txtGroup + 'are not eligible to participate in this event'
  SELECT @Msg AS 'User'
  RETURN
END

当我尝试使用以下代码执行上述存储过程时,它始终返回 NULL 值。

DECLARE @Msg varchar(1000)

EXEC    [dbo].[SP_GetList]
        @fname = N'Jenny',
        @lname = N'Craig',
        @dob = N'04/02/1956',
        @txtGroup = N'DD06A',
        @Msg = @Msg OUTPUT

SELECT  @Msg as N'@Msg'


GO

我们期望看到以下消息:

团体参加DD06A者无资格参加本次活动

或有资格参与的用户。

任何想法我做错了什么?

提前致谢

4

2 回答 2

0

我认为这里不需要关键字RETURN .....尝试完全相同的代码,但没有 RETURN 关键字。

ALTER PROCEDURE [dbo].[SP_GetList]
                             @fname nvarchar(50),
                             @lname nvarchar(50),
                             @dob   nvarchar(50),
                             @txtGroup varchar(50),
                             @Msg  VARCHAR(1000) OUT

AS
SET NOCOUNT ON;
-- added to prevent extra result sets from interfering with SELECT statements.

IF exists(SELECT 'AKRON' AS CodeName, a.voter_id AS voter_id_vre, a.cnty_Group, a.district_combo, a.voter_fname, a.voter_mname, a.voter_lname, a.voter_suffix, COALESCE(a.str_number,'')+' '+COALESCE(a.str_name,'')+' '+COALESCE(a.str_suffix,'')+' '+COALESCE(a.str_apt,'') AS res_addr,
                a.str_city, 'OH' AS str_state, a.str_zip, COALESCE(a.mail_str_num,'')+'   '+COALESCE(a.mail_str_name,'')+' '+COALESCE(a.mail_srt_suff,'')+' '+COALESCE(a.mail_apt,'')+' '+COALESCE(a.mail_city,'')+'   '+COALESCE(a.mail_state,'')+'   '+COALESCE(a.mail_zip,'') AS mail_address, a.dob, (SELECT COUNT(*) FROM dbo.voter_record_supp WHERE dbo.voter_record_supp.voter_ID = a.voter_ID) AS [exists], b.* FROM dbo.voter_record a LEFT OUTER JOIN dbo.voter_record_supp b ON a.voter_id=b.voter_id
                WHERE a.voter_fname Like '%fname%' And a.voter_lname Like '%@lname%' And a.dob = @dob 
                AND voter_status IN ('A','I') AND cnty_Group NOT LIKE 'ML%' AND  cnty_Group NOT LIKE 'PA%' AND cnty_Group NOT LIKE 'MP%' AND cnty_Group NOT LIKE 'CH%' AND cnty_Group NOT LIKE 'SC%' AND cnty_Group NOT LIKE 'CP%')
 BEGIN
     SET @Msg = Participants in Group' + @txtGroup + 'are not eligible to participate in this event
END

关键字RETURN用于终止代码的执行

于 2013-10-13T20:38:42.883 回答
0

打印您的查询,这些条件似乎没有结果。

like '%@lname%'似乎不正确。你应该使用like '%' + @lname + '%'. 与其余参数相同。

ALTER PROCEDURE [dbo].[SP_GetList]
                             @fname nvarchar(50),
                             @lname nvarchar(50),
                             @dob   nvarchar(50),
                             @txtGroup varchar(50),
                             @Msg  VARCHAR(1000) OUT

AS
SET NOCOUNT ON;
-- added to prevent extra result sets from interfering with SELECT statements.

IF exists(SELECT 'AKRON' AS CodeName, a.voter_id AS voter_id_vre, a.cnty_Group, a.district_combo, a.voter_fname, a.voter_mname, a.voter_lname, a.voter_suffix, COALESCE(a.str_number,'')+' '+COALESCE(a.str_name,'')+' '+COALESCE(a.str_suffix,'')+' '+COALESCE(a.str_apt,'') AS res_addr,
                a.str_city, 'OH' AS str_state, a.str_zip, COALESCE(a.mail_str_num,'')+'   '+COALESCE(a.mail_str_name,'')+' '+COALESCE(a.mail_srt_suff,'')+' '+COALESCE(a.mail_apt,'')+' '+COALESCE(a.mail_city,'')+'   '+COALESCE(a.mail_state,'')+'   '+COALESCE(a.mail_zip,'') AS mail_address, a.dob, (SELECT COUNT(*) FROM dbo.voter_record_supp WHERE dbo.voter_record_supp.voter_ID = a.voter_ID) AS [exists], b.* FROM dbo.voter_record a LEFT OUTER JOIN dbo.voter_record_supp b ON a.voter_id=b.voter_id
                WHERE a.voter_fname Like '%' + @fname + '%' And a.voter_lname Like '%' + @lname + '%' And a.dob = @dob 
                AND voter_status IN ('A','I') AND cnty_Group NOT LIKE 'ML%' AND  cnty_Group NOT LIKE 'PA%' AND cnty_Group NOT LIKE 'MP%' AND cnty_Group NOT LIKE 'CH%' AND cnty_Group NOT LIKE 'SC%' AND cnty_Group NOT LIKE 'CP%')
 BEGIN
     SET @Msg = 'Participants in Group' + @txtGroup + 'are not eligible to participate in this event'
  SELECT @Msg AS 'User'
  RETURN
END
于 2013-10-13T20:08:43.843 回答