3

(Excuse the amateur nature of the question)

I am trying to pass the servername and database name to a stored procedure which does some logging as follows

exec [dbo].[Logger]
      @Server = @@servername,
      @Database = db_name(db_id()),
      @Task = 'Some task';

but I keep getting this error -

Msg 102, Level 15, State 1, Line 10
Incorrect syntax near 'db_id'.

I have got this to work using declare @dbname varchar(255) = db_name(db_id()); at the top and then using that. But it seems like an unnecessary variable.

Thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
dopplesoldner
  • 8,891
  • 12
  • 44
  • 55

1 Answers1

1

Try this:

declare @servername varchar(100) = @@servername
declare @dbname = db_name(db_id())

exec [dbo].[Logger]
      @Server = @servername,
      @Database = @dbname,
      @Task = 'Some task'