Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Trouble with functions / sp

Author  Topic 

SammyWaslow
Starting Member

9 Posts

Posted - 2006-08-29 : 08:54:27
I'm having 3 small queries where I would like to get the top 1 row.
Problem is that the queries are some kind of ranking based upon a count and order by (besides the top 1).

I tried to write a function that would return the value needed, but that doesn't seem to work. The function:

ALTER FUNCTION [dbo].[BC_getCustomerFavorites]
(
-- Add the parameters for the function here
@CustomerId Bigint,
@TypeId TinyInt
)
RETURNS Nvarchar(75)
AS
BEGIN
-- Declare the return variable here
DECLARE @Returnstring nvarchar(75)
DECLARE @Description as nvarchar(75)
Declare @Type as char(10)
Declare @Count as int
SET @returnstring = ''

-- Add the T-SQL statements to compute the return value here
IF @TypeId = 1
-- select favorite book
select @returnstring = top 1 productid
from tblBC_CustomerProducts c
where c.customerid = @CustomerId AND
c.typeid= 1

IF @TypeId = 2
-- select favorite genre
select @returnstring = top 1 p.numeriek1
from tblBC_CustomerProducts c INNER JOIN
tblProduct p ON c.productid = p.productid
where c.customerid = @CustomerID AND
c.typeid= 2
group by p.numeriek1
order by count(*) DESC, newid()

IF @TypeId = 3
-- select favorite author
select @returnstring = top 1 p.text1
from tblBC_CustomerProducts c INNER JOIN
tblProduct p ON c.productid = p.productid
where c.customerid = @CustomerID AND
c.typeid= 2
group by p.text1
order by count(*) DESC, newid()


-- Return the result of the function
RETURN @returnstring

END



Problems with the function are that top 1 seems unsupported, newid() seems unsupported and are the IF's supported in fuctions?

As an alternative I tried to write a SP that would return three rows (one for each query) but the SP uses UNIONS that seem to get bugged by the order by clauses:


LTER PROCEDURE [dbo].[BC_getFavoriteBook]
-- Add the parameters for the stored procedure here
@CustomerId Bigint

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- select favorite book
select top 1 productid as Description, 'book' as Type, 1 as Count
from tblBC_CustomerProducts c
where c.customerid = @CustomerId AND
c.typeid= 1


-- select favorite genre
UNION ALL

select top 1 p.numeriek1 as Description, 'genre' as Type, count(*) as Count
from tblBC_CustomerProducts c INNER JOIN
tblProduct p ON c.productid = p.productid
where c.customerid = @CustomerID AND
c.typeid= 2
group by p.numeriek1
order by count(*) DESC, newid()


-- select favorite author
UNION ALL
select top 1 p.text1 as Description, 'author' as Type, count(*) as Count
from tblBC_CustomerProducts c INNER JOIN
tblProduct p ON c.productid = p.productid
where c.customerid = @CustomerID AND
c.typeid= 2
group by p.text1
order by count(*) DESC, newid()

END


Any chances on getting the function or the SP to work with these queries. Or should I use seperate queries (sp) for all three ?

The queries self are fine, i've tested them and the returned results are correct.

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-08-29 : 09:58:09
NEWID() always return same value in the same scope and execution.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2006-08-29 : 10:46:51
This is incorrect syntax:
select @returnstring = top 1 p.text1

It should be:
select top 1 @returnstring = p.text1

You are not allowed to use non-deterministic functions, like newid(), in a function. If you need to use it, pass it as an input parameter to the function.


CODO ERGO SUM
Go to Top of Page

SammyWaslow
Starting Member

9 Posts

Posted - 2006-08-30 : 03:38:07
quote:
Originally posted by Peso

NEWID() always return same value in the same scope and execution.


Peter Larsson
Helsingborg, Sweden



Not true,

the way I use it here, when several rows have the same value for count(*), the newid() sorts them randomly each time you perform the select. Tried and tested the query itself!
Go to Top of Page

SammyWaslow
Starting Member

9 Posts

Posted - 2006-08-30 : 03:40:10
quote:
Originally posted by Michael Valentine Jones

This is incorrect syntax:
select @returnstring = top 1 p.text1

It should be:
select top 1 @returnstring = p.text1

You are not allowed to use non-deterministic functions, like newid(), in a function. If you need to use it, pass it as an input parameter to the function.



Thanks! Could you perhaps show me how I should pass newid() to a function and alter the function to get the same functionality ?
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-08-30 : 05:57:18
quote:
Originally posted by SammyWaslow

quote:
Originally posted by Peso

NEWID() always return same value in the same scope and execution.


Peter Larsson
Helsingborg, Sweden



Not true,

the way I use it here, when several rows have the same value for count(*), the newid() sorts them randomly each time you perform the select. Tried and tested the query itself!

It seems so, yes! Thanks for the update. It works for me too now.

Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -