how do i get the row count

  • Hi,

    I have to insert a row in a table and update in another table if it already exists.

    Infact i have to find out if there are any rows inside the second table , if exists then update, else insert

    declare @count nvarchar(10)

    declare @maxdattime datetime

    declare @UnitNo nvarchar(50)

    declare @t1 nvarchar(50)

    declare @STR nvarchar(1000)

    set @t1='gpsdata'

    set @UnitNo='352022000637171'

    select @STR='declare @count nvarchar(10);select @count=count(*) from '+@t1+' where unit_no=''352022000637171'';select @count'

    print @STR

    exec ( @STR)

    I have given the table name as a variable, because i have to get that too.

    Its just a part of the original sp

    Now i want to have the count value

    How do i get it

    Kindly help

    regards

    cmrhema

  • I guess you can use @@rowcount variable to get the count. It holds the count of rows affected by the last statement.

    Renuka__

    [font="Verdana"]Renuka__[/font]

  • Use sp_executesql with output parameters

    .

    .

    .

    select @STR='select @count=count(*) from '+@t1+' where unit_no=''352022000637171'';'

    execute sp_executesql

    @STR,

    N'@count nvarchar(10) output',

    @count = @count output

    ____________________________________________________

    Deja View - The strange feeling that somewhere, sometime you've optimised this query before

    How to get the best help on a forum

    http://www.sqlservercentral.com/articles/Best+Practices/61537
  • You can simplify your statement by doing this construction:

    declare @SQLCMD nvarchar(1000)

    declare @retcode bigint

    set @SQLCMD = 'select count(*) from {tablename}'

    exec @retcode = sp_executeSQL @SQLCMD

    print @retcode

    Wilfred
    The best things in life are the simple things

  • Many thanks to Mark and Wilfred, both of u replied within minutes of each other, But the moment I receied Mark's reply I tried out and IT WORKED.

    Infact it was solved yesterday itself.

    Many thanks to Mark and this forum which helped me to solve the problem so quickly.

    Thank you

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply