存储过程不返回结果,但将其作为查询

时间:2016-02-19 18:37:42

标签: sql-server tsql stored-procedures

我有一个类似这样的存储过程

ALTER PROCEDURE [dbo].[solar_zip_affiliate_export]
  @affiliate_id int,
  @tier_date varchar
AS
BEGIN
SET NOCOUNT ON;

select   zip, MAX(state) as state from solar_zip_tier_mapping_view sztm (nolock) 
join solar_zip_tier_acl acl (nolock) on acl.tier_id = sztm.tier_id and (acl.buyer_id = sztm.buyer_id or acl.buyer_id = 0)
join buyers b (nolock) on b.buyer_id = sztm.buyer_id
join solar_zip_tiers szt (nolock) on szt.tier_id = sztm.tier_id
where sztm.tier_date = @tier_date and acl.affiliate_id = @affiliate_id and sztm.active > 0 and b.active > 0 and szt.active > 0
group by zip 

END

现在我将此存储过程称为

 exec solar_zip_affiliate_export 150, '021516'"

它不会返回任何结果,但是如果我将参数替换为实际值并将其作为查询运行(意味着只需获取选择部分并在管理工作室中单独运行),它会给出正确的结果。 我疯了。 请任何帮助

1 个答案:

答案 0 :(得分:4)

ALTER PROCEDURE [dbo].[solar_zip_affiliate_export]
@affiliate_id int,
@tier_date varchar(20)  --<-- you need to define the length here 
AS
BEGIN
SET NOCOUNT ON;

select   zip, MAX(state) as state from solar_zip_tier_mapping_view sztm (nolock) 
join solar_zip_tier_acl acl (nolock) on acl.tier_id = sztm.tier_id and (acl.buyer_id = sztm.buyer_id or acl.buyer_id = 0)
join buyers b (nolock) on b.buyer_id = sztm.buyer_id
join solar_zip_tiers szt (nolock) on szt.tier_id = sztm.tier_id
where sztm.tier_date = @tier_date and acl.affiliate_id = @affiliate_id and sztm.active > 0 and b.active > 0 and szt.active > 0
group by zip

如果您没有定义长度,则默认为1,因此始终明确定义数据类型为varchar,nvarchar,char,nchar等的变量的长度。