2016-02-19 27 views
0

我有一個是這樣的存儲過程不返回結果,而是把它作爲查詢確實

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'" 

它沒有返回任何結果,但如果我用實際值替換參數並將其作爲查詢運行(意味着只需獲得選擇部分並在管理工作室中單獨運行),它會給出正確的結果。 我要瘋了。 請任何幫助

+3

你真的應該使用變量用於存儲日期的日期字段,而不是varchar。 –

+0

如果你要堅持使用NOLOCK提示,那麼你至少要包含WITH關鍵字。忽略它已被棄用。我會爭辯說,除非你真的知道它的作用,否則你不應該用這個提示來清理你的數據庫。它不像只讀髒兮兮的簡單。 http://blogs.sqlsentry.com/aaronbertrand/bad-habits-nolock-everywhere/ –

回答

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等

相關問題