views:

114

answers:

6

I have situations that I need to write multiple rows of the same value to setup some tables. Say I have to add 120 rows with two columns populated. I am looking for a shortcut, instead of having the Insert line repeated n times. How to do this?

A: 

Put the values in an unused table for safe keeping. From there you can insert from this table to the tables you need to setup.

Jeff O
+1  A: 

How about

Insert Table( colsnames )
Select Top 120 @value1, @Value2, etc.
From AnyTableWithMoreThan120Rows

Just make sure the types of the values in the @Value list matches the colNames List

Charles Bretana
Most system tables will have enough rows for this.
Philip Kelley
but I don't have these values in a table. Creating a table for them would defeat the purpose
bochur1
The insert is not getting the data from the table, it is just using the table to get a defined number of rows. The select clause should only contain references to parameter values passed in. Like if you executed [Select 'MyName' from tablewith1000Rows] you would get 1000 rows with 'MyName' in them, and no data from the table
Charles Bretana
Ok Charles, not I get it. Great!!!
bochur1
+1  A: 

what about

insert into  tbl1
 (col1,col2)
(select top 120 @value1,@value2 from tbl2)

if in sql server 2008 . new in sql server 2008 to insert into a table multiple rows in a single query .

insert into  tbl1
 (col1,col2)
values
(@value1,@value2),(@value1,@value2),.....(@value1,@value2)
anishmarokey
A: 
  1. Create an Excel Spreadsheet with your data.
  2. Import the speadsheet into Sql Server.
Cape Cod Gunny
A: 

In SQL Server Management Studio, you can use the "GO" keyword with a parameter:

INSERT INTO YourTable(col1, col2, ...., colN)
VALUES(1, 'test', ....., 25)
GO 120

But that works only in Mgmt Studio (it's not a proper T-SQL command - it's a Mgmt Studio command word).

Marc

marc_s
A: 

You can even try with something like this(just an example)

declare @tbl table(col1 varchar(20),col2 varchar(20))
; with generateRows_cte as
(
    select 
     1 as MyRows

       union all
        select 
      MyRows+1

        from    generateRows_cte   
        where   MyRows < 120
)
insert into @tbl(col1,col2)
select 
'col1' + CAST(MyRows as varchar),'col2' + CAST(MyRows as varchar)
from generateRows_cte OPTION (MAXRECURSION 0)
select * from @tbl

Note:- Why not you are trying with Bulk insert into SqlServer from a dataset ? I didnot notice first that u have a front end too(VB)!

priyanka.sarkar
Like Charles' solution but maybe better since creates the table on the fly. I love it. thanks.
bochur1
even you can take the help of some system tables like SYS.COLUMNS,master..spt_values etc. for accomplishing ur task
priyanka.sarkar