If you use .NetTiers as an Application Framework, you'll have used custom stored procedures in cases where the generic Entity Service Find methods do not offer enough granularity. Specifically, if you have the need to join across database tables, you'll find that a custom stored procedure is certainly the way to go. However, one of the biggest challenges facing developers is how to develop custom stored procedures for .NetTiers that support paging and sorting.
- Company: Contain company information
- Address: Contains address information
1: IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = '_Company_CustomSearch')
2: BEGIN
3: DROP Procedure [dbo].[_Company_CustomSearch]
4: END
5: 6: GO
7: 8: CREATE PROCEDURE [dbo].[_Company_CustomSearch]
9: (10: @CompanyId int = null ,
11: @CompanyName nvarchar (150) = null ,
12: @CompanyAddressTownCity nvarchar (200) = null ,
13: @CompanyAddressState nvarchar (200) = null ,
14: @CompanyAddressPostcode nvarchar (20) = null ,
15: @OrderBy nvarchar (2000) = null ,
16: @PageIndex int = null ,
17: @PageSize int = null ,
18: @TotalRows int OUTPUT
19: )20: AS
21: 22: BEGIN
23: 24: --
25: IF @PageIndex IS NULL
26: SET @PageIndex = 0
27: 28: IF @PageSize IS NULL
29: SET @PageSize = 400
30: --
31: SET DATEFORMAT dmy
32: 33: -- Build WHERE Clause
34: DECLARE @WhereClause [nvarchar] (2000)
35: 36: SET @WhereClause = '1=1'
37: 38: IF @CompanyId IS NOT NULL AND @CompanyId <> 0
39: SET @WhereClause = @WhereClause + ' AND [Company].CompanyId = ' + CAST(@CompanyId AS NVARCHAR(20))
40: 41: IF @CompanyName IS NOT NULL AND LEN(@CompanyName) > 0
42: SET @WhereClause = @WhereClause + ' AND [Company].CompanyName LIKE ''' + @CompanyName + '%'''
43: 44: IF @CompanyAddressTownCity IS NOT NULL AND LEN(@CompanyAddressTownCity) > 0
45: SET @WhereClause = @WhereClause + ' AND [Address].TownCity LIKE ''' + @CompanyAddressTownCity + '%'''
46: 47: IF @CompanyAddressState IS NOT NULL AND LEN(@CompanyAddressState) > 0
48: SET @WhereClause = @WhereClause + ' AND [Address].State LIKE ''' + @CompanyAddressState + '%'''
49: 50: IF @CompanyAddressPostcode IS NOT NULL AND LEN(@CompanyAddressPostcode) > 0
51: SET @WhereClause = @WhereClause + ' AND [Address].Postcode LIKE ''' + @CompanyAddressPostcode + '%'''
52: 53: DECLARE @PageLowerBound int
54: DECLARE @PageUpperBound int
55: 56: -- Set the page bounds
57: SET @PageLowerBound = @PageSize * @PageIndex
58: SET @PageUpperBound = @PageLowerBound + @PageSize
59: 60: IF (@OrderBy is null or LEN(@OrderBy) <>61: BEGIN62: -- default order by to first column63: SET @OrderBy = '[CompanyId]'64: END65:66: -- SQL Server 2005 Paging67: declare @SQL as nvarchar(max)68:69: -- get row count70: SET @SQL = 'SELECT @TotalRows = COUNT(DISTINCT Company.CompanyId)'71: SET @SQL = @SQL + ' FROM dbo.[Company]'72: SET @SQL = @SQL + ' INNER JOIN [Address] ON [Address].AddressId = [Company].AddressId'73:74: IF LEN(@WhereClause) > 075: BEGIN76: SET @SQL = @SQL + ' WHERE ' + @WhereClause77: END78:79: EXEC sp_executesql @SQL, N'@TotalRows int out', @TotalRows out80:81: -- Get Data82: SET @SQL = 'WITH PageIndex AS ('83: SET @SQL = @SQL + ' SELECT'84: IF @PageSize > 085: BEGIN86: SET @SQL = @SQL + ' TOP ' + convert(nvarchar, @PageUpperBound)87: END88:89: SET @SQL = @SQL + ' ROW_NUMBER() OVER (ORDER BY [Company].' + @OrderBy + ') as RowIndex'90:91: SET @SQL = @SQL + ', [Company].[CompanyId]'92: SET @SQL = @SQL + ', [Company].[CompanyName]'93: SET @SQL = @SQL + ', [Company].[AddressId]'94: SET @SQL = @SQL + ', [Company].[CompanyUrl]'95: SET @SQL = @SQL + ', [Company].[Telephone]'96:97: SET @SQL = @SQL + ' FROM dbo.[Company]'98: SET @SQL = @SQL + ' INNER JOIN [Address] ON [Address].AddressId = [Company].AddressId'99:100: IF LEN(@WhereClause) > 0101: BEGIN102: SET @SQL = @SQL + ' WHERE ' + @WhereClause103: END104:105: -- Group By106: SET @SQL = @SQL + ' GROUP BY '107: SET @SQL = @SQL + ' [Company].[CompanyId]'108: SET @SQL = @SQL + ', [Company].[CompanyName]'109: SET @SQL = @SQL + ', [Company].[AddressId]'110: SET @SQL = @SQL + ', [Company].[CompanyUrl]'111: SET @SQL = @SQL + ', [Company].[Telephone]'112:113: SET @SQL = @SQL + ' ) SELECT'114: SET @SQL = @SQL + ' [CompanyId]'115: SET @SQL = @SQL + ', [CompanyName]'116: SET @SQL = @SQL + ', [AddressId]'117: SET @SQL = @SQL + ', [CompanyUrl]'118: SET @SQL = @SQL + ', [Telephone]'119: SET @SQL = @SQL + ' FROM PageIndex'120: SET @SQL = @SQL + ' WHERE RowIndex > ' + convert(nvarchar, @PageLowerBound)121:122: IF @PageSize > 0123: BEGIN124: SET @SQL = @SQL + ' AND RowIndex <= ' + convert(nvarchar, @PageUpperBound)125: END126:127: SET @SQL = @SQL + ' ORDER BY ' + @OrderBy128:129: -- Get Data130: --PRINT @SQL131: EXEC sp_executesql @SQL132: PRINT @SQL133:134: -- HACK (to get .NetTiers to gen the TList135: IF USER_NAME() IS NULL136: BEGIN137: SELECT * FROM Company WHERE 1=0138: RETURN139: END140:141: END142:143: GO
0 comments:
Post a Comment