获取客户记录集.sql 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PROCE_GetCustomerRecord]') AND type in (N'P', N'PC'))
  2. DROP PROCEDURE [dbo].[PROCE_GetCustomerRecord]
  3. GO
  4. SET QUOTED_IDENTIFIER ON
  5. GO
  6. -- =============================================
  7. -- Author: 刘超
  8. -- Create date: 2015-07-16
  9. -- Description: 获取客户记录集
  10. -- =============================================
  11. CREATE PROCEDURE [dbo].[PROCE_GetCustomerRecord]
  12. (
  13. @AggregationCustomerFieldsStr varchar(4000) = '*', --聚合客户表字段名(全部字段为*)
  14. @GroupMembersAndCustomerFieldsStr varchar(4000) = '*', --组成员视图字段名(全部字段为*)
  15. @OrderString varchar(2000), --排序字段(必须!支持多字段不用加order by)
  16. @WhereString varchar(2000) =N'', --条件语句(不用加where)
  17. @PageSize int, --每页多少条记录
  18. @PageIndex int = 1 , --指定当前为第几页
  19. @TotalRecord int output --返回总记录数 --返回更新记录数
  20. )
  21. AS
  22. BEGIN
  23. --处理开始点和结束点
  24. Declare @AG_TableName nvarchar(2000);
  25. Declare @GM_TableName nvarchar(2000);
  26. set @AG_TableName='[tempTB_GroupByAggregationCustomer]'--'[tempTB_AggregationCustomer]'
  27. -- set @GM_TableName='dbo.View_CustomerGroupMembersAndErpCustomer'
  28. Declare @GM_SqlString nvarchar(4000);
  29. set @GM_SqlString='
  30. DECLARE @TotalCount int
  31. DECLARE @tabRecord TABLE
  32. (
  33. GP_CustomerGroupID NVARCHAR(50) NOT NULL
  34. )
  35. insert into @tabRecord exec [dbo].[PROCE_SQL2005PAGECHANGE]
  36. @TableName = '''+@AG_TableName+''',
  37. @ReFieldsStr = ''GP_CustomerGroupID'',
  38. @OrderString = '''+@OrderString+''',
  39. @WhereString = '''+Replace(@WhereString,'''','''''')+''',
  40. @PageSize = '+CONVERT(varchar(200), @PageSize)+',
  41. @PageIndex = '+CONVERT(varchar(200), @PageIndex)+',
  42. @TotalRecord = @TotalCount OUTPUT
  43. select '+@GroupMembersAndCustomerFieldsStr+'
  44. FROM [dbo].[View_CustomerGroupMembersAndErpCustomer]
  45. where GM_CustomerGroupID in(select [GP_CustomerGroupID] from @tabRecord)
  46. order by GM_CustomerGroupID asc, GM_ProtagonistCustomer desc, GM_Master desc'
  47. --select @GM_SqlString
  48. exec(@GM_SqlString)
  49. exec [dbo].[PROCE_SQL2005PAGECHANGE]
  50. @TableName = @AG_TableName,
  51. @ReFieldsStr = @AggregationCustomerFieldsStr,
  52. @OrderString = @OrderString,
  53. @WhereString = @WhereString,
  54. @PageSize = @PageSize,
  55. @PageIndex = @PageIndex,
  56. @TotalRecord = @TotalRecord OUTPUT
  57. END
  58. GO