Avatar of davidi1
davidi1
Flag for India asked on

SQL Query Help - Multiple Insert

Team,

Front end ASP has 3 Textboxes & 1 Textarea.

Backend - SQL - has all fields as varchar(255)

In Textarea, users will input NTlogins (Comma Seperated). How do I run a query where i save the data like the following

Textbox1 Textbox2 Texbox3 Ntlogin
Textbox1 Textbox2 Texbox3 Ntlogin1
Textbox1 Textbox2 Texbox3 Ntlogin2
Textbox1 Textbox2 Texbox3 Ntlogin3 etc...,
Microsoft SQL Server 2008Microsoft SQL Server 2005

Avatar of undefined
Last Comment
SAMIR BHOGAYTA

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
anillucky31

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
HainKurt

"How do I run a query where i save the data like the following"
what query are you trying to run? what are you trying to do? need clarification...
davidi1

ASKER
HainKurt - I dont have any query. I dont know how to write a query for this. hence the ask.
prajapati84

From frontend,

Pass the all the four fields as parameters in your stored procedure,
@Textbox1 (textbox1 value)
@Textbox2 (textbox2 value)
@Textbox3 (textbox3 value)
@NTLoginList (comma seperated string)

At backend, in stored procedure
@Textbox1  varchar(255),
@Textbox2 varchar(255),
@Textbox3 varchar(255),
@NTLoginList varchar(max)

Here you need to split the NTLoginList with comma with split function.
Then you can insert the splitted NTLogin items one by one (from the array of NTLogin) with other three values Textbox1,Textbox2,Textbox3 in your database table

Please refer this nice article to split and loop the string value to insert data
http://sqltutorials.blogspot.com/2007/09/sql-function-split.html
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
anillucky31

As per my understanding you want to insert into databse multple entries at one time. 3 of the fields in your insert statment will have same value. but only last column will have different value. Your problem is to seperate those comma separate value and insert them to database.

if that is the case.

I will assue that three textbox values are in these three variables when you pass them to sql server

declare @Text1 varchar(255)
declare@Text2 varchar(255)
declare@Text3 varchar(255)
declare@NTLogin varchar(255)


set @Text1  =' Textbox1'
set @Text2  =' Textbox2'
set @Text3 = ' Textbox3'
set @NTLogin =  'Ntlogin,Ntlogin1,Ntlogin2,)

then you can use this insert statement after creating function as in my previous post.

insert into yourtable(Text1, Text2, Text3, Login)
select @Text1, @Text2,@Text3, item from dbo.fnSplit(@NTLogin, ',')

this will insert data like you desired


SAMIR BHOGAYTA

USE YourDB
GO
INSERT INTO MyTable (FirstCol, SecondCol)
VALUES ('First',1);
INSERT INTO MyTable (FirstCol, SecondCol)
VALUES ('Second',2);
INSERT INTO MyTable (FirstCol, SecondCol)
VALUES ('Third',3);
INSERT INTO MyTable (FirstCol, SecondCol)
VALUES ('Fourth',4);
INSERT INTO MyTable (FirstCol, SecondCol)
VALUES ('Fifth',5);
GO