all groups > sql server programming > july 2006 >
You're in the

sql server programming

group:

Is there a better solution than this SP?



Re: Is there a better solution than this SP? Chris Lim
7/31/2006 3:32:21 AM
sql server programming: [quoted text, click to view]

You could probably consolidate all the DECLARE statements into one.

e.g.
DECLARE mycust SCROLL CURSOR
FOR SELECT unicode, code, name
FROM customer
WHERE unicode LIKE '%'+ ISNULL(@keyword, '') + '%'
ORDER BY
CASE @sorting
WHEN '' THEN unicode
WHEN 'name' THEN name
WHEN 'code' THEN code
...etc...
END
Re: Is there a better solution than this SP? Uri Dimant
7/31/2006 1:25:12 PM
Hi
What is the final result? Can you post DDL+ sample data+ expected result?
I'm sure it could be done without using a cursor



[quoted text, click to view]

Re: Is there a better solution than this SP? Locky
7/31/2006 1:44:11 PM
[quoted text, click to view]

declare @maxrow int
set @maxrow = @pageLength*(@pageno+1)

declare @tab table (_ID_ int not null identity(1,1),unicode
char(20),@code char(20),name char(20))

set rowcount @maxrow
insert into @tab(unicode,code,name)
select unicode,code,name from customer where ....
order by name
set rowcount 0

select unicode,code,name from @tab where _ID_>= @StartRow and _ID_ <
(@StartRow+@PageLength)
Re: Is there a better solution than this SP? Uri Dimant
7/31/2006 2:21:13 PM
Hi
Take a look at http://www.aspfaq.com/show.asp?id=2120



[quoted text, click to view]

Is there a better solution than this SP? Man-wai Chang
7/31/2006 6:12:12 PM
ALTER procedure [dbo].[fetch_customer]
@pageno integer,
@pagelength integer,
@sorting varchar(32),
@keyword varchar(100)
as
set nocount on

declare @startrow integer

if @pageno<0 begin
declare @ttl integer
select @ttl=count(*) from customer
set @startrow=@ttl/@pagelength*@pagelength+1
end
else begin
set @startrow=(@pageno-1)*10+1
end

create table #result (
unicode char(20),
code char(20),
name char(60),
rcnb integer
)

if @sorting=' ' begin
if @keyword=' ' begin
declare mycust scroll cursor
for select unicode, code, name from customer
order by unicode
end else begin
declare mycust scroll cursor
for select unicode, code, name from customer
where unicode like '%'+@keyword+'%'
order by unicode
end
end else if @sorting='name' begin
if @keyword=' ' begin
declare mycust scroll cursor
for select unicode, code, name from customer
order by name
end else begin
declare mycust scroll cursor
for select unicode, code, name from customer
where unicode like '%'+@keyword+'%'
order by name
end
end else if @sorting='code' begin
if @keyword=' ' begin
declare mycust scroll cursor
for select unicode, code, name from customer
order by code
end else begin
declare mycust scroll cursor
for select unicode, code, name from customer
where unicode like '%'+@keyword+'%'
order by code
end
end

declare @unicode char(20), @code char(20), @name char(60)
declare @ii integer

set @ii=0
open mycust
fetch absolute @startrow from mycust into @unicode, @code, @name
while ((@@fetch_status=0) and (@ii < @pagelength)) begin
insert into #result values ( @unicode, @code, @name, @startrow+@ii )
set @ii = @ii+1
fetch next from mycust into @unicode, @code, @name
end

close mycust
deallocate mycust
set nocount off
select * from #result
drop table #result


--
SoftMedia Technology Co., Ltd.
Website: http://www.softmedia.hk Tel: (852)2743 4228
Re: Is there a better solution than this SP? Man-wai Chang
7/31/2006 7:07:55 PM
[quoted text, click to view]

This is neat. Thanks!

--
.~. Might, Courage, Vision, SINCERITY. http://www.linux-sxs.org
/ v \ Simplicity is Beauty! May the Force and Farce be with you!
/( _ )\ (Ubuntu 6.06) Linux 2.6.17.6
^ ^ 19:08:01 up 15 days 2:31 0 users load average: 1.00 1.04 1.01
Re: Is there a better solution than this SP? Man-wai Chang
7/31/2006 7:08:17 PM
[quoted text, click to view]

Neat! Thanks!

--
.~. Might, Courage, Vision, SINCERITY. http://www.linux-sxs.org
/ v \ Simplicity is Beauty! May the Force and Farce be with you!
/( _ )\ (Ubuntu 6.06) Linux 2.6.17.6
^ ^ 19:08:01 up 15 days 2:31 0 users load average: 1.00 1.04 1.01
Re: Is there a better solution than this SP? Man-wai Chang
7/31/2006 7:08:51 PM
[quoted text, click to view]

It's for browsing the table page by page, used by a Foxpro form.

--
.~. Might, Courage, Vision, SINCERITY. http://www.linux-sxs.org
/ v \ Simplicity is Beauty! May the Force and Farce be with you!
/( _ )\ (Ubuntu 6.06) Linux 2.6.17.6
^ ^ 19:08:01 up 15 days 2:31 0 users load average: 1.00 1.04 1.01
Re: Is there a better solution than this SP? Man-wai Chang
7/31/2006 9:29:07 PM
[quoted text, click to view]

Thanks...

--
.~. Might, Courage, Vision, SINCERITY. http://www.linux-sxs.org
/ v \ Simplicity is Beauty! May the Force and Farce be with you!
/( _ )\ (Ubuntu 6.06) Linux 2.6.17.6
^ ^ 21:29:02 up 15 days 4:52 1 user load average: 1.02 1.04 1.00
AddThis Social Bookmark Button