Solved

SQL Command

Posted on 2011-02-24
4
275 Views
Last Modified: 2012-05-11
How can I insert 50 records in a table as follows using sql command.

table1
ItemNo(varchar)   name (varchar)
A_1                       some Text
A_2                       some Text
...
A_50                     some Text

ayha
0
Comment
Question by:ayha1999
  • 2
4 Comments
 
LVL 32

Expert Comment

by:ewangoya
ID: 34972781
declare @No integer
set @No = 1

while @No <= 50
begin
  insert into table1(ItemNo, name)
  select 'A_1' + cast(@No as varchar), 'some text'
  set @No = @No + 1
end
0
 
LVL 32

Accepted Solution

by:
ewangoya earned 250 total points
ID: 34972786
sorry
declare @No integer
set @No = 1

while @No <= 50
begin
  insert into table1(ItemNo, name)
  select 'A_' + cast(@No as varchar), 'some text'
  set @No = @No + 1
end

Open in new window

0
 
LVL 20

Expert Comment

by:informaniac
ID: 34972821
U could also take a look at SqlBulkCopy, which is quite fast.
0
 
LVL 40

Expert Comment

by:Sharath
ID: 34973399
You can try CTE.
declare @table as table (StartValue int, EndValue int)
insert into @table values (1,10) -- change this number to required value
;with CTE as (
select StartValue,EndValue from @table union all
select StartValue+1,EndValue from CTE where StartValue+1 <= EndValue)
select 'A_'+CONVERT(varchar,StartValue) ItemNo,'some text' name 
  from CTE
/*
ItemNo	name
A_1	some text
A_2	some text
A_3	some text
A_4	some text
A_5	some text
A_6	some text
A_7	some text
A_8	some text
A_9	some text
A_10	some text
*/

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

840 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