Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a select query which returns n records . I want those to insert into the same table with the Different B_ID's my B_ID's will be (4,5,6,7) how i can write easy query. I can do that by for loop . But i want some simpler and optimized query
INsert into S_FACT(S_ID,B_ID,G_ID,AMOUNT)
  (select '150',B_ID,G_ID,AMOUNT from S_FACT where S_ID=2 and  B_ID =(2))
Posted
Updated 21-Mar-16 8:48am
v3

1 solution

Depends how you are running the query, but i would use a CTE to create a table of 4,5,6,7:

SQL
with num as (
 select 4 as num
 union all 
 select num +1
 from test 
 where num < 7)
Insert into S_GLOBALFIELDS_FACT(S_ID,B_ID,G_ID,AMOUNT)
select '150',nums.num,G_ID,AMOUNT 
from S_FACT, nums
where S_ID=2 and B_ID =(2)


That will insert 4 items for each row where B_ID = 2

If you want the B_ID to be selected where B_ID = 4, 5, 6 or 7 then just use a "IN" operator:
SQL
Insert into S_GLOBALFIELDS_FACT(S_ID,B_ID,G_ID,AMOUNT)
select '150',nums.num,G_ID,AMOUNT 
from S_FACT, nums
where S_ID=2 and B_ID IN (4,5,6,7)


Hope that helps ^_^
Andy


UPDATE: How to pass arrays as parameters
Assumptions:
1: You're using sql server 2008 or later.
2: You're using SqlClient Connection in your code.

First you have to create a user defined type:
SQL
CREATE TYPE int_list_table AS TABLE (n int NOT NULL PRIMARY KEY)


This will be stored in your database for future use. You can use this as a stored procedure parameter.

SQL
Create Procedure My_Insert(
@ids as int_list_table
)
AS
BEGIN

  
  Insert into S_FACT(S_ID,B_ID,G_ID,AMOUNT)
  select '150',B_ID,G_ID,AMOUNT 
  from S_FACT s
  inner join @ids i on s.B_ID = i.n
  where S_ID=2

  /* or using "IN"
  Insert into S_FACT(S_ID,B_ID,G_ID,AMOUNT)
  select '150',B_ID,G_ID,AMOUNT 
  from S_FACT 
  where S_ID=2 and  B_ID in (select n from @ids)
  /*
END


You can pass the array as a parameter without specifying a type
C#
using (conn)
{
    SqlCommand cmd = new SqlCommand("dbo.My_Insert", conn);
    cmd.CommandType = CommandType.StoredProcedure;
    SqlParameter dtparam = cmd.Parameters.AddWithValue("@ids", ids);
    dtparam.SqlDbType = SqlDbType.Structured;
}


I hope that helps ^_^
Andy
 
Share this answer
 
v3
Comments
MYQueries1 21-Mar-16 12:45pm    
that 4,5,6,7 just a dummy numbers those won't sequential
it can be 44,22,11,65 any thing basically it's a list of numbers
with num as (
select 4 as num
union all
select num +1
from test
where num < 7)
Insert into S_GLOBALFIELDS_FACT(S_ID,B_ID,G_ID,AMOUNT)
select '150',nums.num,G_ID,AMOUNT
from S_FACT, nums
where S_ID=2 and B_ID =(2)
Andy Lanng 21-Mar-16 13:33pm    
oic. So you need to pass the numbers in as a parameter?
As I said, it depends how you are running the query. Is is a stored procedure or are you using the query as a command text?

If its the later, then it's kinda simple (with restrictions), but if it's the former then I have some follow up questions.
MYQueries1 21-Mar-16 13:35pm    
Former one not the later.What are ur questions?
Andy Lanng 22-Mar-16 5:03am    
Right: First off, You can pass an array to a stored procedure. It does make things a bit easier but it depends of what version of sql you're using (2008 and later is much easier).
I'll update the solution to show you how this can be done

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900