I have a SP with Dynamic Sql that until now worked fine. But I made longer the Dynamic Sql code, and now the variable that holds the Dynamic code has not enough space to generate all the code. I mean that the variable @sql set to nvarchar(4000) isn’ t big enough to store/generate all the code. How can I solve this problem?Here is a sample of my SP: USE marketGOALTER PROCEDURE test_dyn6@TotalRegisters nvarchar(100) outputAsBEGIN SET NOCOUNT ON DECLARE @sql nvarchar(4000), @param_list nvarchar(4000) DECLARE @offerDate As nvarchar(100) DECLARE @Id As nvarchar(1000) SELECT @sql = ' SELECT @offerDate = offe.offerDate, @Id = offe.Offer_id From Offers As offe WHERE 1 = 1' SELECT @sql = @sql + ' Order by offe.offerDate Desc, offe.Offer_id Desc' SELECT @sql = @sql + ' SELECT offe.Offer_id, offe.User_num, offe.offerDate, offe.Offer_title From Offers As offe WHERE 1 = 1 AND ((offe.offerDate < @Data) Or (offe.offerDate = @Data And offe.Offer_id <= @Id))' SELECT @sql = @sql + ' Order by offe.offerDate Desc, offe.Offer_id Desc' SELECT @sql = @sql + ' SELECT @TotalRegisters = COUNT(*) FROM Offers As offe WHERE 1 = 1' SELECT @param_list = '@offerDate datetime output, @Id nvarchar(1000) output, @TotalRegisters nvarchar(100) output' EXEC sp_executesql @sql, @param_list, @offerDate output, @Id output, @TotalRegisters output SELECT @Data, @Id SELECT @TotalRegisters SET NOCOUNT OFF END GO
Thank you,Cesar