次のコードを実行しようとしています。
use projects
set nocount on
CREATE TABLE #t (dbname VARCHAR(20), col2 datetime)
declare @sql nvarchar(max)
declare @proc_sql nvarchar(max)
declare @table_car table (rowid int not null primary key identity(1,1), carrier varchar(500))
insert into @table_car (carrier)
select distinct t.db_name from table t with(nolock)
inner join table_t1 t1 with(nolock) on t.id = t1.id
declare @rowstoprocess int
declare @currentrow int
declare @dbname varchar(500)
set @rowstoprocess = @@rowcount
set @currentrow = 0
while @currentrow < @rowstoprocess
begin
set @currentrow = @currentrow+1
select @dbname = carrier
from @table_car
where rowid = @currentrow
select @sql = '
insert into #t (dbname, col2)
SELECT DISTINCT db_name(), col_x from'+@dbname+'..table_xyz'
set @proc_sql = replace(@sql, @dbname, @dbname)
exec (@proc_sql)
end
ただし、これを実行すると、エラーが発生します。
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near '.'.
これは 3 行目とは関係ないと思いますが@dbname
、while ループ内で使用されている変数の近くに構文エラーがあります。助けてください