Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Save ROWCOUNT value in a variable

Save ROWCOUNT value in a variable

Scheduled Pinned Locked Moved Database
help
5 Posts 2 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nt_virus
    wrote on last edited by
    #1

    Hello. I want to save the RowCount returned value in a variable and want to print (+1 with 'A' prefix). I created a variable as int.

    declare @TotalIntRow as int

    Now I'm unable to assign the returned value of rowcount in a variable. i use these ..

    @TotalIntRow = select Count(*) from Rcount

    Error :- Msg 137, Level 15, State 2, Line 1
    Must declare the scalar variable "@TotalIntRow".

    set @TotalIntRow = select Count(*) from Rcount

    Error : - Msg 156, Level 15, State 1, Line 1
    Incorrect syntax near the keyword 'select'.

    [I also tried to add a demo value , Set @TotalIntRow = 1; , but showing message number 137 as above mentioned] How could I save the value in it.

    L 1 Reply Last reply
    0
    • N nt_virus

      Hello. I want to save the RowCount returned value in a variable and want to print (+1 with 'A' prefix). I created a variable as int.

      declare @TotalIntRow as int

      Now I'm unable to assign the returned value of rowcount in a variable. i use these ..

      @TotalIntRow = select Count(*) from Rcount

      Error :- Msg 137, Level 15, State 2, Line 1
      Must declare the scalar variable "@TotalIntRow".

      set @TotalIntRow = select Count(*) from Rcount

      Error : - Msg 156, Level 15, State 1, Line 1
      Incorrect syntax near the keyword 'select'.

      [I also tried to add a demo value , Set @TotalIntRow = 1; , but showing message number 137 as above mentioned] How could I save the value in it.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Try this one;

      DECLARE @TotalIntRow AS INT
      SET @TotalIntRow = (SELECT COUNT(*)
      FROM Rcount)
      PRINT @TotalIntRow

      Don't forget that the DECLARE statement should be used to declare the variable, before using it. Hope this helps :)

      I are troll :)

      N 1 Reply Last reply
      0
      • L Lost User

        Try this one;

        DECLARE @TotalIntRow AS INT
        SET @TotalIntRow = (SELECT COUNT(*)
        FROM Rcount)
        PRINT @TotalIntRow

        Don't forget that the DECLARE statement should be used to declare the variable, before using it. Hope this helps :)

        I are troll :)

        N Offline
        N Offline
        nt_virus
        wrote on last edited by
        #3

        Awesome mate. But I cant understand one thing, if I do these steps individually I got scalar error something. why so ? just curious about it. Ain't the values are stored permanently in it? BTW.. If even I run the above scripts do the variable takes additional space or they get removed after application shutdown?

        Begin
        DECLARE @TotalIntRow AS INT
        SET @TotalIntRow = (SELECT COUNT(*)
        FROM Rcount)
        print @TotalIntRow + 1
        Insert into Rcount Values(@TotalIntRow)
        End

        I m getting my partial desired result. I want to add prefix 'A' with the result set .. When I did .. I got , can't convert (variable declared for storing A) to int data type..

        N 1 Reply Last reply
        0
        • N nt_virus

          Awesome mate. But I cant understand one thing, if I do these steps individually I got scalar error something. why so ? just curious about it. Ain't the values are stored permanently in it? BTW.. If even I run the above scripts do the variable takes additional space or they get removed after application shutdown?

          Begin
          DECLARE @TotalIntRow AS INT
          SET @TotalIntRow = (SELECT COUNT(*)
          FROM Rcount)
          print @TotalIntRow + 1
          Insert into Rcount Values(@TotalIntRow)
          End

          I m getting my partial desired result. I want to add prefix 'A' with the result set .. When I did .. I got , can't convert (variable declared for storing A) to int data type..

          N Offline
          N Offline
          nt_virus
          wrote on last edited by
          #4

          Oh my god, I can't believe I did it..

          Begin
          DECLARE @TotalIntRow AS INT
          SET @TotalIntRow = (SELECT COUNT(*)
          FROM Rcount)
          print @TotalIntRow + 1
          Insert into Rcount Values('A'+ Convert(varchar, @TotalIntRow))
          End

          L 1 Reply Last reply
          0
          • N nt_virus

            Oh my god, I can't believe I did it..

            Begin
            DECLARE @TotalIntRow AS INT
            SET @TotalIntRow = (SELECT COUNT(*)
            FROM Rcount)
            print @TotalIntRow + 1
            Insert into Rcount Values('A'+ Convert(varchar, @TotalIntRow))
            End

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Well done dude :cool:

            nt_virus wrote:

            But I cant understand one thing, if I do these steps individually I got scalar error something. why so ? just curious about it. Ain't the values are stored permanently in it?

            Not permanent, they are 'lost' once the command is finished executing. (In this case, once it passes the END statement) That's also the reason why they can't be executed separate, they are interpreted as separate commands if you execute them line by line separately.

            nt_virus wrote:

            BTW.. If even I run the above scripts do the variable takes additional space or they get removed after application shutdown?

            They are gone as soon as the script (the complete collection of lines) are finished, unless you store them in a table somewhere. Most applications execute their script and close the connection to the database when done. Good work :)

            I are troll :)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups