Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL Server 2008 R2 - Execution Plan 3 29
Set form below another form 3 26
SQl query 19 13
sql query Help 12 29
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

706 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now