i'm trying to insert data from one table to another with dynamic column name from @array to @array2
error
The multi-part identifier "s.id" could not be bound.
SQL CODE:
DECLARE @Array TABLE
(
id int not null,
dt varchar(12) not null,
ld varchar(16) not null,
val varchar(12) not null,
ty varchar(4) not null,
PRIMARY KEY CLUSTERED (id,dt)
)
DECLARE @Array2 TABLE
(
id int not null,
dt varchar(12) not null,
ld varchar(16) not null,
min varchar(12) null,
mout varchar(4) null,
PRIMARY KEY CLUSTERED (id,dt)
)
INSERT INTO @Array VALUES
('1','2015-11-11','2015-11-11','20:08','min')
,('2','2015-11-11','2015-11-11','20:08','mout')
,('3','2015-11-11','2015-11-11','20:08','min')
,('4','2015-11-11','2015-11-11','20:08','min')
Select * from @Array s
WHERE NOT EXISTS (select s.id,s.dt,s.ld,s.ty from @Array2
WHERE id != s.id AND dt != s.dt)
INSERT INTO @Array2 (id,dt,ld,s.ty) VALUES(s.id,s.dt,s.ld,s.val)
^
dynamic column name from @Array TABLE
here is SQL Fiddle link, thanks.