Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Is there a better way
Message
De
09/09/2009 18:12:37
Cetin Basoz
Engineerica Inc.
Izmir, Turquie
 
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Versions des environnements
SQL Server:
SQL Server 2000
Application:
Desktop
Divers
Thread ID:
01423336
Message ID:
01423364
Vues:
56
>I'm wondering if there is a better way to write this function? I'm populating two variables which I then use in the returnvalue. Basically there could 1 or many records for each meetingnumber in the meetproc table. There is a int field in the meetproc table that stores a card number if assigned or a 0 if no card has been assigned. This function is getting called from the main stored procedure, so for each record returned in the main query, this function is calling two more queries. It works correctly and returns valid data, I'm just in the process of trying to optimize for a big load. Thanks in advance.
>
>
>ALTER FUNCTION [dbo].[Get_CardsAssignedNumeric]  (@tiMeetID int)  
>RETURNS varchar(10) AS  
>BEGIN 
>     Declare @returnValue varchar(10)
>     set @returnValue=' '
>     declare @TotalProcsAssigned int
>     declare @TotalCardsAssigned int 
>     set @TotalProcsAssigned = (Select count(*) from dbo.meetproc where meetingnumber=@tiMeetID)
>     set @TotalCardsAssigned = (Select count(*) from dbo.meetproc where meetingnumber=@tiMeetID and cardassigned > 0)
>     set @ReturnValue=cast(@TotalCardsAssigned as varchar(2))+' of '+cast(@TotalProcsAssigned as varchar(2))
>
>     return (@returnValue)
>END
>
You could do it as a TVF so you wouldn't need any variables and return the result as a 'table'. ie:
Create FUNCTION [dbo].[Get_CardsAssignedNumeric]  (@tiMeetID int)  
RETURNS table AS  
BEGIN 
select 
         TotalProcsAssigned, 
         TotalCardsAssigned, 
         cast(TotalCardsAssigned as varchar(2))+' of '+cast(TotalProcsAssigned as varchar(2)) as ReturnValue
FROM 
  (Select count(*) as TotalProcsAssigned, 
             sum( when cardassigned > 0 then 1 else 0 end ) as TotalCardsAssigned
             from dbo.meetproc 
             where meetingnumber=@tiMeetID) tmp
END
Sample use:
select * from [dbo].[Get_CardsAssignedNumeric](1)
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform