Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2005 > SQL Server 2005
|
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 23rd, 2008, 07:44 PM
Registered User
 
Join Date: Oct 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem with clause Top in store procedure

Hi

I have this code

declare @count_rows int
declare @sql as varchar(8000)
set @count_rows = 2
set @sql = (select top 1 eff_dt from (select top 2 * from dbo.tabla where cod = 99 order by src_eff_dt) q order by eff_dt desc)
print 'value=' + @sql

result: value= Nov 29 2005 6:07 PM

but when I put parameters

declare @count_rows int
declare @sql as varchar(8000)
set @count_rows = 2
set @sql = (select top 1 eff_dt from (select top ' + (@count_rows) + * from dbo.tabla where cod = 99 order by src_eff_dt) q order by eff_dt desc)
print 'value=' + @sql

Could not find stored procedure, I would like confirm in this problem for SQL version 2000

Thanks a lot
Javier

 
Old October 23rd, 2008, 08:28 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

You have mixed up a couple of concepts.

Since you are still using SQL Server 2000, you'll have to do something like:
Code:
CREATE PROC whatevername 
    @count_rows INT
AS
declare @sql as varchar(8000)
set @sql = 'select MAX(q.eff_dt) AS TheDt from '
         + ' (select top ' + @count_rows + ' from dbo.tabla where cod = 99 order by src_eff_dt) AS q'
EXEC ( @sql )
print 'value=' + @sql
 
Old October 24th, 2008, 09:37 AM
Registered User
 
Join Date: Oct 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks for your recommendation but I have problems with the type of data

Msg 245, Level 16, State 1, Procedure whatevername, Line 6
Syntax error converting the varchar value 'select MAX (q.eff_dt) AS TheDt from (select top' to a column of data type int.

 
Old October 24th, 2008, 10:17 AM
Registered User
 
Join Date: Oct 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks I modified something and now working fine






Similar Threads
Thread Thread Starter Forum Replies Last Post
store procedure if else problem krshekhar SQL Language 0 February 20th, 2008 05:31 AM
Selecting top record of a group by clause lic023 Access 7 June 7th, 2006 11:25 AM
Store Procedure Problem jazzcatone ASP.NET 2.0 Basics 3 March 28th, 2006 12:41 AM
Passing an "IN" clause to a stored procedure atcs2152 SQL Server 2000 5 February 7th, 2006 10:55 PM
Stored Procedure - Dynamic Where Clause Terry_Pino BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 1 July 2nd, 2004 04:39 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.