Take the contents of the columns that are inside a while

Asked

Viewed 34 times

0

Good night! I’m trying to use the speakers of the second while to insert into a table, but I can’t get the value of the columns. I’ve used several combinations of quotation marks and nothing. This is 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
  • Ever tried to use a TRIGGER? With it it is possible to manipulate the data used in the command.

  • Talk about replacing the set with a TRIGGER? I will search how to apply in this case.

  • I managed to solve using a cursor.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.