Link to home
Start Free TrialLog in
Avatar of ian_r
ian_rFlag for United Kingdom of Great Britain and Northern Ireland

asked on

IF statement execution dependant on record count


I want to perform a series of actions on a table, but only if there are records in the table.

The table name will eventually be passed as parameter to the stored procedure, so the table name in the SELECT COUNT statement can change.

I'm struggling with the syntax and would appreciate some help.  I'm getting the following error:

Server: Msg 137, Level 15, State 2, Line 17
Must declare the variable '@tableName'.

I have declared the variable so I must be doing something else wrong.  Does anyone have any suggestions on how to fix this, or perhaps a better way to achieve the same thing?

Code being used:

DECLARE @type int, @tableName varchar(200), @numRecords int

SET @type = 7

IF @type = 7
BEGIN
    SET @tableName = 'table1'
END
ELSE
BEGIN
   SET @tableName = 'table2'
END

SELECT @numRecords= COUNT(*) FROM @tableName

-- only try and transfer records if some exist
IF (@numRecords) > 0
BEGIN
      PRINT 'There are records in the table'
END
Avatar of indu_mk
indu_mk

Replace SELECT @numRecords= COUNT(*) FROM @tableName
with
declare @sql varchar(8000)
set @sql = 'SELECT @numRecords= COUNT(*) FROM ' + @tableName
exes(@sql)
Try this

DECLARE @type int, @tableName varchar(200), @numRecords int
Declare @SQL varchar(255)

SET @type = 7

IF @type = 7
BEGIN
    SET @tableName = 'table1'
END
ELSE
BEGIN
   SET @tableName = 'table2'
END

Set @SQL = 'SELECT @numRecords = COUNT(*) FROM [' + @tableName + ']'
Execute (@SQL)

-- only try and transfer records if some exist
IF (@numRecords) > 0
BEGIN
     PRINT 'There are records in the table'
END



hongjun
sorry for the typo, its exec not exes
Avatar of ian_r

ASKER

Thanks.  The two suggestions look the same to me, and I've just tried the code that hongjun supplied, but I'm now getting the error:

Server: Msg 137, Level 15, State 1, Line 1
Must declare the variable '@numRecords'.

Which is strange because @numRecords is declared.  I'll continue to play around with it, unless anyone know's why it's failing?

Maybe I've just been staring at it for too long...
Try this

DECLARE @type int, @tableName varchar(200)
Declare @SQL varchar(255)

SET @type = 7

IF @type = 7
BEGIN
    SET @tableName = 'table1'
END
ELSE
BEGIN
   SET @tableName = 'table2'
END

Execute ('
      Declare @numRecords int
      SELECT @numRecords=COUNT(*) FROM ' + @tableName +
      '-- only try and transfer records if some exist
      IF (@numRecords) > 0
      BEGIN
          PRINT ''There are records in the table''
      END
'
)
ASKER CERTIFIED SOLUTION
Avatar of indu_mk
indu_mk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you shouldn't write procedures like this ...
this is a very poor style and requires dynamic sql to provide the result....
which can lead to all sorts of security and performance issues...

you need to re-evaluate your requirements...

what are you actually trying to achieve?
in what circumstance do you need the information?
  (dynamic sql can be ok in a System management sense...)