0

First query I'm getting user_id records like below :

1
14
22
38
39
54
68
74

Now i want to insert above user_id's in another table in single query, like below :

INSERT INTO tbl_Channel_Subscriber(user_id,channel_id,status,EntDate) VALUES(@user_id,@channel_id,@status,@EntDate).

How can i insert new record with single insert query

Chenna Rao
  • 111
  • 3
  • 11

1 Answers1

0

Don't use 2 queries. Do it in one and add the variables to the first query

INSERT INTO tbl_Channel_Subscriber(user_id,channel_id,status,EntDate) 
select user_id, @channel_id, @status, @EntDate
from first_table
juergen d
  • 201,996
  • 37
  • 293
  • 362
  • #juergen d (SELECT DISTINCT(user_id) FROM tbl_Following_Followers WHERE user_id IN(SELECT DISTINCT(followers_ID) FROM tbl_Following_Followers WHERE user_id=14 and status='Follow') OR user_id IN(SELECT DISTINCT(user_id) FROM tbl_Following_Followers WHERE followers_ID=14 and status='Follow')) .This Query i'm getting user_id's , how can i insert this query – Chenna Rao Apr 13 '15 at 09:59
  • INSERT INTO tbl_Channel_Subscriber(user_id,channel_id,status,EntDate)SELECT DISTINCT(user_id), @channel_id,@status,@EntDate FROM tbl_Following_Followers WHERE user_id IN(SELECT DISTINCT(followers_ID) FROM tbl_Following_Followers WHERE user_id=14 and status='Follow') OR user_id IN(SELECT DISTINCT(user_id) FROM tbl_Following_Followers WHERE followers_ID=14 and status='Follow')) – Arun Gairola Apr 13 '15 at 10:10
  • #Arun your given query inserting records successfully but some records not inserted i means if distinct user_id's effected 1,3,21,32,39 but only insert two or three records only insert – Chenna Rao Apr 13 '15 at 10:27