?
Solved

SQL Server Insert Loop

Posted on 2009-02-19
2
Medium Priority
?
1,190 Views
Last Modified: 2012-05-06
hey folks,
  I want to do a "loop" to insert records.  One of the fields needs to be fr_(x) on the insert.
See my code below and let me know how I can do the insert for  = 'fr_' (1 - 9)

declare @galleryType nVarchar(30), @galleryPlayID nVarchar(30),@galleryContent nvarchar(MAX)
set @galleryType = 'test'
set @galleryPlayID = 'fr_' + (1 to x)
set @galleryContent='Content'
select @galleryType as galleryType, @galleryContent as galleryContent


INSERT INTO tblResortGallery
   (galleryType,
    galleryPlayID
    galleryPlayID,
    galleryContent)
select
   @galleryType,
   @galleryPlayID,
   @galleryContent
0
Comment
Question by:lrbrister
2 Comments
 
LVL 14

Accepted Solution

by:
puranik_p earned 2000 total points
ID: 23689217
tell me if this is what you want...
declare @galleryType nVarchar(30), @galleryPlayID nVarchar(30),@galleryContent nvarchar(MAX)
set @galleryType = 'test'
set @galleryContent='Content'
 
DECLARE @x int
SET @x = 1
 
WHILE @x <= 9 -- put number of rows to be inserted here
BEGIN
SET @galleryPlayID =  'fr_' + convert(varchar(10),@x)
INSERT INTO tblResortGallery
   (galleryType,
    galleryPlayID,
    galleryContent)
select
   @galleryType,
   @galleryPlayID,
   @galleryContent
SET @x = @x + 1
END

Open in new window

0
 

Author Closing Comment

by:lrbrister
ID: 31549097
Perfect
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

830 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