Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to add create multiple rows in a single instance with SQL Server 2008

Posted on 2010-08-28
5
Medium Priority
?
329 Views
Last Modified: 2012-06-27
Hi,

I am trying to work with SQL Server 2008 and pre-populate a number of rows with some data that has already ben pre-defined.

I am struggling to create/insert into multiple lines all at once using an insert into statement.

When I right click the table and click script as, Insert into I get the following code but do not know where to put the data after that.

If anyone can help that would be very much appreciated.

Many thanks

Simon
INSERT INTO [OGSM].[dbo].[OGSM]
           ([Record]
           ,[Objecive]
           ,[Goal]
           ,[Strategy_1]
           ,[Measure_1]
           ,[Owner_1]
           ,[Strategy_2]
           ,[Measure_2]
           ,[Owner_2]
           ,[Strategy_3]
           ,[Measure_3]
           ,[Owner_3]
           ,[Strategy_4]
           ,[Measure_4]
           ,[Owner_4]
           ,[Strategy_5]
           ,[Measure_5]
           ,[Owner_5]
           ,[Strategy_6]
           ,[Measure_6]
           ,[Owner_6])
     VALUES
           (<Record, int,>
           ,<Objecive, varchar(255),>
           ,<Goal, varchar(255),>
           ,<Strategy_1, varchar(255),>
           ,<Measure_1, varchar(255),>
           ,<Owner_1, varchar(255),>
           ,<Strategy_2, varchar(255),>
           ,<Measure_2, varchar(255),>
           ,<Owner_2, varchar(255),>
           ,<Strategy_3, varchar(255),>
           ,<Measure_3, varchar(255),>
           ,<Owner_3, varchar(255),>
           ,<Strategy_4, varbinary(50),>
           ,<Measure_4, varchar(255),>
           ,<Owner_4, varchar(255),>
           ,<Strategy_5, varchar(255),>
           ,<Measure_5, varchar(255),>
           ,<Owner_5, varchar(255),>
           ,<Strategy_6, varchar(255),>
           ,<Measure_6, varchar(255),>
           ,<Owner_6, varchar(255),>)
GO

Open in new window

0
Comment
Question by:SimonPrice33
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33549037
The markers like

<Record, int,>

are there to let you know that an "int" value goes there to be stored into "Record" column.
Using some dummy values, the below is a sample statement.
INSERT INTO [OGSM].[dbo].[OGSM]
           ([Record]
           ,[Objecive]
           ,[Goal]
           ,[Strategy_1]
           ,[Measure_1]
           ,[Owner_1]
           ,[Strategy_2]
           ,[Measure_2]
           ,[Owner_2]
           ,[Strategy_3]
           ,[Measure_3]
           ,[Owner_3]
           ,[Strategy_4]
           ,[Measure_4]
           ,[Owner_4]
           ,[Strategy_5]
           ,[Measure_5]
           ,[Owner_5]
           ,[Strategy_6]
           ,[Measure_6]
           ,[Owner_6])
     VALUES
(1,'Objective','Goal','Strategy_1','Measure_1','Owner_1','Strategy_2','Measure_2',.....,'Owner_6')
GO

Open in new window

0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33549042
In SQL Server 2008, you can insert multiple rows at once using commas between each set of values.
e.g.

insert sometable (a,b,c,d) values
(1,'a',3,4),
(2,'c',5,6),
(3,'f',9,10)
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 33549044
I don't know what exactly you want. you can use SELECT statement in INSERT like:

Insert into Table1 (column1,column2)
select 'colVal1','ColVal1'
UNION ALL
select 'colVal2','ColVal2'

in select statement, you can directly put the value or put your variable name or SELECT (recordset) from other tables. by this way, you can insert multiple row at the same time.  below is the example script of your table. I have used same variable name in both SELECT statement which shouldn't be same in both SELECT otherwise, same row will be inserted twice.

INSERT INTO [OGSM].[dbo].[OGSM]
           ([Record]
           ,[Objecive]
           ,[Goal]
           ,[Strategy_1]
           ,[Measure_1]
           ,[Owner_1]
           ,[Strategy_2]
           ,[Measure_2]
           ,[Owner_2]
           ,[Strategy_3]
           ,[Measure_3]
           ,[Owner_3]
           ,[Strategy_4]
           ,[Measure_4]
           ,[Owner_4]
           ,[Strategy_5]
           ,[Measure_5]
           ,[Owner_5]
           ,[Strategy_6]
           ,[Measure_6]
           ,[Owner_6])

   SELECT [@Record]
           ,[@Objecive]
           ,[@@Goal]
           ,[@Strategy_1]
           ,[@Measure_1]
           ,[@Owner_1]
           ,[@Strategy_2]
           ,[@Measure_2]
           ,[@Owner_2]
           ,[@Strategy_3]
           ,[@Measure_3]
           ,[@Owner_3]
           ,[@Strategy_4]
           ,[@Measure_4]
           ,[@Owner_4]
           ,[@Strategy_5]
           ,[@Measure_5]
           ,[@Owner_5]
           ,[@Strategy_6]
           ,[@Measure_6]
           ,[@Owner_6]
union all
   SELECT [@Record]
           ,[@Objecive]
           ,[@@Goal]
           ,[@Strategy_1]
           ,[@Measure_1]
           ,[@Owner_1]
           ,[@Strategy_2]
           ,[@Measure_2]
           ,[@Owner_2]
           ,[@Strategy_3]
           ,[@Measure_3]
           ,[@Owner_3]
           ,[@Strategy_4]
           ,[@Measure_4]
           ,[@Owner_4]
           ,[@Strategy_5]
           ,[@Measure_5]
           ,[@Owner_5]
           ,[@Strategy_6]
           ,[@Measure_6]
           ,[@Owner_6]
           
   
GO

Open in new window

0
 
LVL 2

Expert Comment

by:IngCharlie
ID: 33549046
This is a representation of you values <Measure_6, varchar(255),> of type varchar(255)
so if you want Measure_6 to have the value "value"

INSERT INTO [OGSM].[dbo].[OGSM]
([Measure_6])
VALUES ('value')

so just replace the statements <...> with your values and don't forget to use ' ' for non numberic values
0
 
LVL 10

Accepted Solution

by:
Marcjev earned 2000 total points
ID: 33549048
an insert statement is of the form

insert into tablename(fieldname1, fieldname2, [and so on]) values (valieforfield1, valueforfield2, [and so on]).

so if I have a table 'person' with fields firstname, lastname, streetname
I would need something like

insert into person(firstname, lastname, streetname) values ('john', 'doe', 'unknown')

for sql script i would repeat multiple times like and ad a go at the end. Enter this in a sql query window.

insert into person(firstname, lastname, streetname) values ('john', 'doe', 'unknown')
insert into person(firstname, lastname, streetname) values('Elisa', 'Doolitle', 'Firststreet')
go
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

598 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question