Good evening! I'm trying to use the columns of the second while
to insert into a table, but I can not get the value of the columns. I already used several combinations of quotes and nothing. Is this possible?
DECLARE @query nvarchar(max)
DECLARE @tag varchar(20), @tagnova varchar(20)
DECLARE @temp TABLE (tag nvarchar(20))
DECLARE @temp2 TABLE (time datetime,point nvarchar(128),alm_type varchar(128),unit varchar(128),pv varchar(128),limit nvarchar(128))
INSERT INTO @temp
SELECT tag FROM nas.dbo.tb_cadastro
WHILE EXISTS (SELECT tag FROM @temp)
BEGIN
SET @tag = (SELECT TOP 1 tag FROM @temp)
SET @tagnova = SUBSTRING(@tag, 0, CHARINDEX('@', @tag))
INSERT INTO @temp2
SELECT Time,point,alm_type,unit,pv,limit FROM [EventHistDB].[dbo].[Event_val2] WHERE point = ''+@tagnova+'' and (kind = 'ALM') and (alm_type = 'PVHH' OR alm_type = 'PVLL')
WHILE EXISTS (SELECT Time as time, point as point, alm_type as alm_type, unit as unit, pv as pv, limit as limit FROM @temp2)
BEGIN
SET @query = 'INSERT INTO [nas].[dbo].[tb_eventos] (room, tag, excur_start, max_min, limite, tipo, unit)
VALUES (SUBSTRING('''+@tagnova+''', CHARINDEX(''_T'','''+@tagnova+''')+1, (LEN('''+@tagnova+''') - CHARINDEX(''_T'','''+@tagnova+''')))
,'''+@tag+'''
,time
,pv
,limit
,alm_type
,unit)';
print (@query)
END
DELETE FROM @temp WHERE tag = @tag;
END