| Author |
Topic |
|
darenkov
Yak Posting Veteran
90 Posts |
Posted - 2005-02-09 : 11:40:23
|
| Hi I recently started using Paul Wilson's sproc for paging. It is very good but I have tried modifying it to ouput the TotalRecord count in an OUTPUT PARM or RETURN value. When I execute it I get the following error:Server: Msg 245, Level 16, State 1, Procedure uspPaging2, Line 45Syntax error converting the varchar value 'SELEC' to a column of data type int.My code is below:CREATE PROCEDURE uspPaging( @TableName VARCHAR(50), @PrimaryKey VARCHAR(25), @SortField VARCHAR(100), @PageSize INT, @PageIndex INT = 1, @QueryFilter VARCHAR(100) = NULL, @TotalRecords VARCHAR(5) OUTPUT) ASSET NOCOUNT ONDECLARE @SizeString AS VARCHAR(5)DECLARE @PrevString AS VARCHAR(5)SET @SizeString = CONVERT(VARCHAR, @PageSize)SET @PrevString = CONVERT(VARCHAR, @PageSize * (@PageIndex - 1))IF @QueryFilter IS NULL OR @QueryFilter = ''BEGIN EXEC( 'SELECT * FROM ' + @TableName + ' WHERE ' + @PrimaryKey + ' IN (SELECT TOP ' + @SizeString + ' ' + @PrimaryKey + ' FROM ' + @TableName + ' WHERE ' + @PrimaryKey + ' NOT IN (SELECT TOP ' + @PrevString + ' ' + @PrimaryKey + ' FROM ' + @TableName + ' ORDER BY ' + @SortField + ') ORDER BY ' + @SortField + ') ORDER BY ' + @SortField ) SELECT @TotalRecords = ('SELECT (COUNT(*) - 1)/' + @SizeString + ' + 1 AS PageCount FROM ' + @TableName)ENDELSEBEGIN EXEC( 'SELECT * FROM ' + @TableName + ' WHERE ' + @PrimaryKey + ' IN (SELECT TOP ' + @SizeString + ' ' + @PrimaryKey + ' FROM ' + @TableName + ' WHERE ' + @QueryFilter + ' AND ' + @PrimaryKey + ' NOT IN (SELECT TOP ' + @PrevString + ' ' + @PrimaryKey + ' FROM ' + @TableName + ' WHERE ' + @QueryFilter + ' ORDER BY ' + @SortField + ') ORDER BY ' + @SortField + ') ORDER BY ' + @SortField ) SELECT @TotalRecords = ('SELECT (COUNT(*) - 1)/' + @SizeString + ' + 1 AS PageCount FROM ' + @TableName) ENDRETURN @TotalRecordsGOI know this is probably straight fwd but I have racked spent hours on this and am unable to get it going without the error. The exec code I have used is below also:DECLARE @TotalCount Varchar(5)EXEC uspPaging2 'business', 'business_id', 'rank', 10, 1, 'business_id between 1 AND 10', @TotalRecords = @Total OUTPUTSelect TotalCount = @TotalCountAny ideas where I am going wrong? |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2005-02-09 : 12:12:19
|
use sp_executeSql for this:declare @TotalRecordsintexec sp_executeSql N''SELECT @TotalRecords = (COUNT(*) - 1)/' + @SizeString + ' + 1 FROM ' + @TableName, N'@TotalRecords int output', @TotalRecords outputselect @TotalRecordsGo with the flow & have fun! Else fight the flow |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-02-09 : 12:13:29
|
I think your problem is with this line:SELECT @TotalRecords = ('SELECT (COUNT(*) - 1)/' + @SizeString + ' + 1 AS PageCount FROM ' + @TableName)You're trying to assing a local variable to the result of an exec'd string. you can't do it like that. You could, however use sp_executesql with output parameters.Here is an example:declare @stmnt nvarchar(2000) ,@parms nvarchar(500) ,@retVal varchar(20)--declare parameter as outputset @parms = N'@outVar varChar(20) output'--assign value to output parameter in dynamic sqlset @stmnt = N'Select @outVar = ''TG'''--exec sql, get return value from output variableexec sp_executesql @stmnt, @parms, @outVar=@retVal outputSelect @retVal Be One with the OptimizerTG |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-02-09 : 12:14:03
|
| Dang you Spirit1 !!edit: I've got to learn to hit refresh before I post :)Be One with the OptimizerTG |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2005-02-09 : 12:28:19
|
for this occasiong let me show you the picture, TG that's usually used when one finds himself in similar situation Go with the flow & have fun! Else fight the flow |
 |
|
|
darenkov
Yak Posting Veteran
90 Posts |
Posted - 2005-02-09 : 12:48:16
|
| Thanks for the tips guys. It looks a bit complicated. I think I'll have to go study-up on this 'sp_executesql'. In the mean time I might try and find a workaround of some sort.cheers |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2005-02-09 : 13:01:40
|
there's no workaround.it's not complicated at all.if you have a dynamic table name that's the only sane option... we can talk about insane ones if you wish... Go with the flow & have fun! Else fight the flow |
 |
|
|
darenkov
Yak Posting Veteran
90 Posts |
Posted - 2005-02-09 : 13:35:58
|
| Spirit1, I am just a little confused. I am not sure how it works. Do I have to call my normal proc "uspPaging", and then call the "sp_executesql" as well? Do I also have to specify somewhere in the sp_executesql statement which proc it is reffering to? That is what I am not sure of? Would it be too much trouble if you could put the code you wrote above next to my proc, just so I can get a better idea of where it fits in?Puno Hvala |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2005-02-09 : 14:01:39
|
put exec sp_executeSql N''SELECT @TotalRecords = (COUNT(*) - 1)/' + @SizeString + ' + 1 FROM ' + @TableName, N'@TotalRecords int output', @TotalRecords outputinstead of SELECT @TotalRecords = ('SELECT (COUNT(*) - 1)/' + @SizeString + ' + 1 AS PageCount FROM ' + @TableName)and that's it.Go with the flow & have fun! Else fight the flow |
 |
|
|
darenkov
Yak Posting Veteran
90 Posts |
Posted - 2005-02-09 : 14:18:47
|
| Hi again Spirit1.I tried as you suggested but am getting the following error:Server: Msg 170, Level 15, State 1, Procedure uspPaging2, Line 28Line 28: Incorrect syntax near ' + @TableName, N'.Server: Msg 170, Level 15, State 1, Procedure uspPaging2, Line 37Line 37: Incorrect syntax near ' + @TableName + '.Server: Msg 170, Level 15, State 1, Procedure uspPaging2, Line 38Line 38: Incorrect syntax near ' + @SizeString + '.Server: Msg 170, Level 15, State 1, Procedure uspPaging2, Line 39Line 39: Incorrect syntax near ' + @PrevString + '.-----------------------------------------------------CREATE PROCEDURE uspPaging( @TableName VARCHAR(50), @PrimaryKey VARCHAR(25), @SortField VARCHAR(100), @PageSize INT, @PageIndex INT = 1, @QueryFilter VARCHAR(100) = NULL, @TotalRecords VARCHAR(5) OUTPUT) ASSET NOCOUNT ONDECLARE @SizeString AS VARCHAR(5)DECLARE @PrevString AS VARCHAR(5)SET @SizeString = CONVERT(VARCHAR, @PageSize)SET @PrevString = CONVERT(VARCHAR, @PageSize * (@PageIndex - 1))IF @QueryFilter IS NULL OR @QueryFilter = ''BEGIN EXEC( 'SELECT * FROM ' + @TableName + ' WHERE ' + @PrimaryKey + ' IN (SELECT TOP ' + @SizeString + ' ' + @PrimaryKey + ' FROM ' + @TableName + ' WHERE ' + @PrimaryKey + ' NOT IN (SELECT TOP ' + @PrevString + ' ' + @PrimaryKey + ' FROM ' + @TableName + ' ORDER BY ' + @SortField + ') ORDER BY ' + @SortField + ') ORDER BY ' + @SortField ) exec sp_executeSql N''SELECT @TotalRecords = (COUNT(*) - 1)/' + @SizeString + ' + 1 FROM ' + @TableName, N'@TotalRecords int output', @TotalRecords outputENDELSEBEGIN EXEC( 'SELECT * FROM ' + @TableName + ' WHERE ' + @PrimaryKey + ' IN (SELECT TOP ' + @SizeString + ' ' + @PrimaryKey + ' FROM ' + @TableName + ' WHERE ' + @QueryFilter + ' AND ' + @PrimaryKey + ' NOT IN (SELECT TOP ' + @PrevString + ' ' + @PrimaryKey + ' FROM ' + @TableName + ' WHERE ' + @QueryFilter + ' ORDER BY ' + @SortField + ') ORDER BY ' + @SortField + ') ORDER BY ' + @SortField ) exec sp_executeSql N''SELECT @TotalRecords = (COUNT(*) - 1)/' + @SizeString + ' + 1 FROM ' + @TableName, N'@TotalRecords int output', @TotalRecords outputENDRETURN @TotalRecordsGO |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2005-02-09 : 14:26:44
|
had 2 quotes...changeexec sp_executeSql N''SELECT @TotalRecords = (COUNT(*) - 1)/' + @SizeString + ' + 1 FROM ' + @TableName, N'@TotalRecords int output', @TotalRecords outputtoexec sp_executeSql N'SELECT @TotalRecords = (COUNT(*) - 1)/' + @SizeString + ' + 1 FROM ' + @TableName, N'@TotalRecords int output', @TotalRecords outputGo with the flow & have fun! Else fight the flow |
 |
|
|
darenkov
Yak Posting Veteran
90 Posts |
Posted - 2005-02-09 : 14:32:10
|
| Spirit1, it is nearly there. I made the change as you suggested. I am only getting the two following errors now:Server: Msg 170, Level 15, State 1, Procedure uspPaging2, Line 28Line 28: Incorrect syntax near '+'.Server: Msg 170, Level 15, State 1, Procedure uspPaging2, Line 42Line 42: Incorrect syntax near '+'.I am not sure as nothing looks out of place to me but it is still not happy. |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2005-02-09 : 14:52:56
|
try this then:declare @sqltemp nvarchar(100)set @sqltemp = 'SELECT @TotalRecords = (COUNT(*) - 1)/' + @SizeString + ' + 1 FROM ' + @TableNameexec sp_executeSql @sqltemp , N'@TotalRecords int output', @TotalRecords outputGo with the flow & have fun! Else fight the flow |
 |
|
|
darenkov
Yak Posting Veteran
90 Posts |
Posted - 2005-02-09 : 20:53:37
|
| Yay it works!Thanks for all of your help and patience Spirit1. |
 |
|
|
|