Insert values x into table A if exist on table B

Asked

Viewed 53 times

-1

I want to Insert in table A of the key field, but only if this key already exists in table B

Example Insert key(123) in table B if there is key(123) in table a

(am using sql procedures managemente studio)


    ALTER procedure [dbo].[InsertHeartRate]
(

    @TimeStamp datetime = '',
    @Value varchar(3) = '',
    @MacAddress varchar(30) = ''
)
as
BEGIN
    Insert into HeartRate (TimeStamp,Value,MacAddress)
    Select @MacAddress
    where (Select @MacAddress
                From banda
                where MacAddress= @MacAddress);
    Values(@TimeStamp,@Value,@MacAddress)
    
END

I’m trying to change the process, but with difficulties, the key would be the macaddress

  • 1
  • thanks, but no, I want to insert in the tebela a, but only when the key 123 ja EXIST in table b.

  • 1

    and isn’t that what the answer shows but with a "no" exist? if you take the not that has in the not exists will not do exactly what you want?

  • yes I thought about this option, but if the value does not exist it lets insert?

  • do a test :) also, put in your question a tag with the database you are using

1 answer

1

Good morning,

Here is a suggestion for testing:

Insert into HeartRate (TimeStamp, Value, MacAddress)
    Select top(1) @TimeStamp, @Value, @MacAddress
    From banda
    where MacAddress = @MacAddress

Select will only return some data if there is a row in the Band table with the value of the parameter for the Macaddress column, and no row will be inserted if no data is returned. And you can remove the top(1) if there is always only one row in the Band table for each Macaddress.

I hope it helps

  • That’s right, so I don’t need the "Values" Thank you to everyone who helped, and sorry for my inexperience

  • I’m glad it worked. Kindly evaluate the possibility of accepting the suggestion as a response

Browser other questions tagged

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