?
Solved

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

Posted on 2010-08-28
5
Medium Priority
?
326 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article I will describe the Backup & Restore 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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

800 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