My following code gives error: (SQL Server 2008R2; Fetch two database name in cursor and then insert data to Table1 in two databases)
DECLARE @SQL NVARCHAR(max)
DECLARE @DB VARCHAR(50)
DECLARE CUR_DB CURSOR FAST_FORWARD FOR
SELECT NAME FROM MASTER.SYS.DATABASES
WHERE DATABASE_ID IN ('5', '81')
;
OPEN CUR_DB;
FETCH NEXT FROM CUR_DB INTO @DB;
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @SQL = 'INSERT INTO @DB.dbo.Table1 VALUES (100, ''abc'', def'', 0)'
EXEC(@SQL)
FETCH NEXT FROM CUR_DB INTO @DB
END;
CLOSE CUR_DB;
DEALLOCATE CUR_DB;
Error: Msg 102, Level 15, State 1, Line 1 Incorrect syntax near '.'. Msg 102, Level 15, State 1, Line 1 Incorrect syntax near '.'.
@DB. Tryprint @Sqlbefore executing.