Dynamic insert into an table on sql server

gosi75
gosi75 used Ask the Experts™
on
I need some help to finish my code as this programming is new to me. I have 110 Entity_SQN number that I need to generate a new line based on current values for that SQN Number in the DIM_Category table, where I need to alter only 2 column values in the new line.  

Example: for each Entity_SQN I need a new line where Detail_SQN should be 0, Grouping1,Grouping2,Description, Entity and Entity_SQN should be the same as in DIM_Category for that Entity_SQN and Text should have the value 'No Category'

I would appreciate if someone could help me finish the code, what I'm having problems with is how do I insert the values from the DIM_Category table?

DECLARE @Detail_SQN int,
		@Grouping1 varchar(100),
		@Grouping2 varchar(100),
		@Description varchar(255),
		@Text varchar(255),
		@Entity varchar(37),
		@Entity_SQN int
SET @Detail_SQN = 0
set @Grouping1 = ?
set @Grouping2 = ?
set @Description = ?
set @Text = 'No Category'
set @Entity = ?
set @Entity_SQN = ?


--This query gives me the correct result to add into the DIM_Category table
Select distinct @Detail_SQN,Grouping1,Grouping2,Description,@Text,Entity,Entity_SQN
FROM DIM_Category
where Entity_SQN in 
(4727,4739,4747,4750,4751,4752,4754,4755,4759,4764,4765,4766,4771,4779,4784,4788,4790,4792,4793,4800,
4801,4802,4803,4804,4806,4807,4811,4815,4816,4817,4823,4832,4833,4835,4856,4857,4875,4888,4893,4913,
4947,4952,5028,5032,5033,5035,5036,5037,5039,5040,5041,5046,5049,5055,5059,5060,5061,5062,5258,5259,5261,
5262,5264,5265,5274,5279,5366,5367,5369,5663,5666,5667,5670,5673,5674,5675,5676,5681,5683,5685,5687,5688,5690,
5691,5692,5693,5694,5695,5696,5697,5699,5705,5734,5743,5744,5772,5848,5871,5883,5894,5914,5956,5957,5966,5983,6027,
6070,6096,6125,6148)

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2013
Awarded 2012
Commented:
You have to adjust the table name as well as the column names:

INSERT INTO YourDetailTableHere
(YOurColmnnames here)
SELECT distinct 0,Grouping1,Grouping2,Description,'No Category',Entity,Entity_SQN
FROM DIM_Category
where Entity_SQN in 
(4727,4739,4747,4750,4751,4752,4754,4755,4759,4764,4765,4766,4771,4779,4784,4788,4790,4792,4793,4800,
4801,4802,4803,4804,4806,4807,4811,4815,4816,4817,4823,4832,4833,4835,4856,4857,4875,4888,4893,4913,
4947,4952,5028,5032,5033,5035,5036,5037,5039,5040,5041,5046,5049,5055,5059,5060,5061,5062,5258,5259,5261,
5262,5264,5265,5274,5279,5366,5367,5369,5663,5666,5667,5670,5673,5674,5675,5676,5681,5683,5685,5687,5688,5690,
5691,5692,5693,5694,5695,5696,5697,5699,5705,5734,5743,5744,5772,5848,5871,5883,5894,5914,5956,5957,5966,5983,6027,
6070,6096,6125,6148)

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial