Results 1 to 4 of 4

Thread: put Select count statement into a variable -- help!!

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2000
    Location
    bebenia, PA, USA
    Posts
    241
    How do I put a select statement into a variable? Do I declare it and with what?

    SELECT COUNT (*)as 'Your a Lammo' from dbo.vw_Lammo
    Where vendor_err='x' and created_dt>@FromDate

    How would I put this into a variable? or the count of this into a variable in SQL?

  2. #2
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    461
    I am not really sure what you are asking here.

    I get the "impression" you want to know how to set a variable to a record count in TRANSACT-SQL for use in a STORED PROCEDURE.

    If that *IS* what you are asking you do it like this :

    Code:
    delcare @@totalrecords      integer
    
    SELECT @@totalrecords = count(*) FROM table WHERE x = 10
    
    if @@totalrecords > 0
    begin
     <Do whatever you want>
    end
    Note that you MUST have 2 ats (@) before the declaration as Transact-SQL uses single @ to describe common variables and @@ to describe variables set from SELECT statements.

    If this is NOT what you were meaning then you are going to have to explain yourself again. Are you using VB? Is it in a record set? Where are you putting it? What is it doing?

  3. #3
    Addicted Member curlywink's Avatar
    Join Date
    Mar 2000
    Location
    Manila, Philippines
    Posts
    141
    Hi there,

    'wanna try this ...

    Dim rst as Recordset
    Dim YourVariable as Integer

    Set rst = dbAnything.OpenRecordSet("SELECT COUNT (*)as Lammo from dbo.vw_Lammo Where vendor_err='x' and created_dt>@FromDate ")

    YourVariable = rst!Lammo

    Hope this can help ...
    dbAnything = 'ur Database object...


  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Mar 2000
    Location
    bebenia, PA, USA
    Posts
    241
    Thank you Gen x; that was exactly it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width