Re: returning @@RowCount

From: Aaron Bertrand [MVP] (aaron_at_TRASHaspfaq.com)
Date: 04/03/04


Date: Sat, 3 Apr 2004 09:08:33 -0500

If it is variable length, use VARCHAR. CHAR will pad the data with spaces
and this could muck up your display.

-- 
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/
"Shaul Feldman" <sfeldman@writeme.com> wrote in message 
news:%23oT9q0XGEHA.3576@tk2msftngp13.phx.gbl...
> Thank you for reply.
> Another short one - when you use, for example, varchar(x) instead of 
> char(x)
> the SQL server allocates physical x chars, but uses only the size of my
> data, or it uses pointers? I need to know that in order to decide what is
> better - char(x) or varchar(x).
> Thank you.
>
> --
> With the best wishes,
>       Shaul Feldman
>
> "Andrew John" <aj@DELETEmistrose.com> wrote in message
> news:#gOTVaXGEHA.740@tk2msftngp13.phx.gbl...
>> Shaul,
>>
>> The main thing you are doing inefficiently is returning @cnt from the
> stored
>> procedure twice.  Once as an output parameter - normal accepted way,
>> and once as the return value, which normal usage reserves for
> success/failure.
>>
>> You are also doubling up on return codes - as you have two failure
> conditions ?
>> @cnt = 0 and @cnt = -1, assuming you have some code you haven't posted
> that
>> makes the default of -1 meaningful.
>>
>> @@rowcount is set after every statement, so I very much doubt it is
> implemented
>> in an inefficient way.
>>
>> alter procedure stp_Family
>> (
>>  @FamilyName char(50),
>> )
>> as
>>  select *
>>  from
>>   tblCust
>>  where
>>   cust_family like rtrim(@FamilyName)
>>
>>  return @@rowcount
>> go
>>
>> works, or with a spot of error checking:
>>
>> alter procedure stp_Family
>> (
>>  @FamilyName char(50),
>>  @cnt int = -1 output
>> )
>> as
>> declare @Err int
>>
>>  select *
>>  from
>>   tblCust
>>  where
>>   cust_family like rtrim(@FamilyName)
>>
>>  select @cnt = @@rowcount, @Err = @@error
>>  return @Err
>> go
>>
>> Although in this case @Err is unlikely to be very interesting, as it only
> catches non-fatal errors.
>> I'm not fond of your naming convention, but thats a personal issue.  Your
> use of like, rtrim and char
>> is not efficient.  varchar which does not add trailing spaces would be a
> better bet, and if trailing spaces are the
>> only reason you are using like ( rather than say difference), then you 
>> can
> make it a proper =, and
>> get a better guarantee of index usage.
>>
>> Regards
>>  AJ
>>
>>
>> "Shaul Feldman" <sfeldman@writeme.com> wrote in message
> news:OTrfXSVGEHA.3908@TK2MSFTNGP12.phx.gbl...
>> Hello,
>> I'm trying to receive from my stored procedure a ReturnCode that will
> actually represent the number of select records in
>> select query. For instance,
>> alter procedure stp_Family
>> (
>>  @FamilyName char(50),
>>  @cnt int = -1 output
>> )
>> as
>>  select *
>>  from
>>   tblCust
>>  where
>>   cust_family like rtrim(@FamilyName)
>>
>>  set @cnt = @@rowcount
>>  return @cnt
>> go
>> The question is: is there any more efficient way to achieve the same 
>> goal?
>>
>> --
>> With the best wishes,
>>     S. Feldman
>>
>>
>
> 


Relevant Pages

  • Re: Maintaining Field Length in .txt format
    ... If the types are VARCHAR, ... converts them to CHAR types. ... >are on a SQL Server 2000 environment and I can create ... The header information for the feed to be ...
    (microsoft.public.sqlserver.programming)
  • Re: Loading a variable
    ... Sorry I want the variable to be char or varchar so that I ... >> Let's say I have a simple three column table, temp27, ... >> with a couple of rows in sql server 2000. ...
    (microsoft.public.sqlserver.server)
  • Re: Data types
    ... If the data is variably sized, use varchar. ... Programming with fixed length char value is a pain. ... Pro SQL Server 2000 Database Design - ...
    (microsoft.public.sqlserver.programming)
  • RE: Loading a variable
    ... Sorry I want the variable to be char or varchar so that I ... >> Let's say I have a simple three column table, temp27, ... >> with a couple of rows in sql server 2000. ...
    (microsoft.public.sqlserver.server)
  • Re: Char and Varchar
    ... If the maximum length is short (<= 10 characters), ... maximum length, I also use CHAR. ... I use VARCHAR if long and short ...
    (microsoft.public.sqlserver.server)