Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i am trying to do multiple insert with same id. I did manage to create parametrised query which is working, but still looks kinda ugly by using the same parameter (@projekt) twice. Can somebody gimme a tip ? Thank you

What I have tried:

SQL
insert into Forma (formaNazwa, FK_projektId) 
values( 
			@forma1,
			(select projektId from Projekt where projektNazwa = @projekt)
		)
		,
		(
			@forma2,
			(select projektId from Projekt where projektNazwa = @projekt)
		)
;


C#
cmd1.Parameters.AddWithValue("@forma1",TextBoxProjectNameAdd.Text.ToString());
cmd1.Parameters.AddWithValue("@forma2",TextBoxProjectNameAdd1.Text.ToString());
cmd1.Parameters.AddWithValue("@projekt",TextBoxProjectNameAdd2.Text.ToString())
Posted
Updated 17-May-18 1:00am
v5
Comments
F-ES Sitecore 17-May-18 4:54am    
Google something like "sql insert with joins" as it would be better to join the tables normally and insert from the result of those joins rather than using the sub-queries you are using.
jaket-cp 17-May-18 5:21am    
Or you could declare @projektId to store the projektId and used that as an insert for each record

1 solution

Nothing wrong with your solution.
But if you want it more "generic" you can try this:
SQL
insert into Forma (formaNazwa, FK_projektId) 
SELECT  formaNazwa.Value,projektId 
FROM    Projekt
CROSS JOIN (VALUES (@forma1),(@forma2)) AS formaNazwa(Value)
WHERE   projektNazwa = @projekt
 
Share this answer
 
v2

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