Solved

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

Posted on 2010-08-28
5
324 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 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

691 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