-2

I want to use a variable to fetch the select query using database name, where the database name would be stored in the variable. But i'm not able to achieve this using the below approach. Can one please let me know how to achieve this.

DECLARE @PrePublishedDB VARCHAR(100)
SET @PrePublishedDB = 'MyDatabase'

SELECT COUNT(*) FROM @PrePublishedDB.BaseVehicle
Manju
  • 2,520
  • 2
  • 14
  • 23

1 Answers1

1
execute ('SELECT COUNT(*) FROM ' + @PrePublishedDB + '.BaseVehicle')