Query

  • Hi
     I have below code . I want record should be inserted only in tblTest if No_ value in test exists in other table Test1.

    insert into tblTest select [No_],[DocumentNo] from [test] 

    Thanks

  • If you know Test1.No_ column has unique values, then:

    INSERT INTO [tblTest]
    SELECT [test].[No_]
        ,[test].[DocumentNo]
    FROM [test]
    INNER JOIN [Test1]
        ON [Test].[No_] = [Test1].[No_];

    Else:
    INSERT INTO [tblTest]
    SELECT [test].[No_]
        ,[test].[DocumentNo]
    FROM [test]
    WHERE EXISTS (
            SELECT *
            FROM [Test1]
            WHERE [Test1].[No_] = [test].[No_]
            );

    Though, I would prefer to include the column list at the target,.

  • Hi GrassHopper
      I have written below code . Is it ok
    insert into tblTemp select  A.[No_],A.[DocNo_] from [Test] as A

    where exists (Select No_ from tblTemp as B where A.[No_] = B.No_)

    Thanks

  • That looks correct to me, yes.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply