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
2009-11-03 03:20:13
+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
2009-11-03 03:25:27
Most system tables will have enough rows for this.
Philip Kelley
2009-11-03 03:46:27
but I don't have these values in a table. Creating a table for them would defeat the purpose
bochur1
2009-11-03 04:21:08
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
2009-11-03 14:12:56
Ok Charles, not I get it. Great!!!
bochur1
2009-11-04 17:21:52
+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
2009-11-03 03:44:39
A:
- Create an Excel Spreadsheet with your data.
- Import the speadsheet into Sql Server.
Cape Cod Gunny
2009-11-03 04:08:14
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
2009-11-03 05:48:26
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
2009-11-03 06:02:25
Like Charles' solution but maybe better since creates the table on the fly. I love it. thanks.
bochur1
2009-11-04 17:23:12
even you can take the help of some system tables like SYS.COLUMNS,master..spt_values etc. for accomplishing ur task
priyanka.sarkar
2009-11-05 03:40:37