I have a problem with assigning a database name to a variable. When I do this, I get a message that there is no such database.
What am I doing wrong that this script doesn't work?
DECLARE @db_name varchar(10)
SET @db_name = 'xxx'
ALTER TABLE [@db_name].[dbo].[Table_Name]
DROP CONSTRAINT [Constraint_Name]
GO
ALTER TABLE [@db_name].[dbo].[Table_Name] WITH CHECK
ADD CONSTRAINT [Constraint_Name] CHECK (QUERY)
GO
ALTER TABLE [@db_name].[dbo].[Table_Name] CHECK CONSTRAINT [Constraint_Name]
GO
I'm getting this error message:
Msg 2702, Level 16, State 2, Line 5
Database '@db_name' does not exist.Msg 2702, Level 16, State 2, Line 8
Database '@db_name' does not exist.Msg 2702, Level 16, State 2, Line 11
Database '@db_name' does not exist.