Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1025847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:59:29+00:00 2026-05-16T11:59:29+00:00

I try to insert some dummy data inside my table using a WHILE ,

  • 0

I try to insert some dummy data inside my table using a WHILE, but it run really really slow.

I was thinking maybe I am writing not properly the code, could yo please have a look and confirm it?

-- Insert dummy data

DECLARE
    @i          int,
    @Content    int;
SET @i = 5001;

WHILE @i > 5000 AND @i < 10000
BEGIN
    SET @Content = ROUND(((10000-5000)*RAND()+5000),0)
    INSERT INTO dbo.CmsImagesContents
    (ContentId, Title, AltTag, Caption)
    VALUES
    (@Content,'Test Title', 'Test AltTag', 'Test Caption');
    SET @i = @i + 1;
END
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:59:29+00:00Added an answer on May 16, 2026 at 11:59 am

    Rather than doing 4999 separate insert statements in a loop, you’ll get much better performance if you do a single insert of all 4999 rows. So, if you have a table #T containing 4999 rows you would simply call the following:

    INSERT INTO DBO.CmsImagesContents(ContentId, Title, AltTag, Caption)    
    SELECT (ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) % 5000) + 5000 AS ContentID, 
            'Test Title' AS Title, 'Test AltTag' AS AltTag, 'Test Caption'  AS Caption
    FROM #T1
    

    If you need to create such a table of 4999 rows in the first place then the following SQL would work for you:

    CREATE TABLE #T1
    (
        N INT NOT NULL PRIMARY key
    );
    
    WITH L0 AS (SELECT 1 AS N UNION ALL SELECT 1), 
        L1 AS (SELECT A.N FROM L0 AS A CROSS JOIN L0 AS B),
        L2 AS (SELECT A.N FROM L1 AS A CROSS JOIN L1 AS B),
        L3 AS (SELECT A.N FROM L2 AS A CROSS JOIN L2 AS B),
        L4 AS (SELECT A.N FROM L3 AS A CROSS JOIN L3 AS B),
        Nums AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS n FROM L4)
    
    INSERT INTO #T1( N )
    SELECT N
    FROM Nums
    WHERE n < 10000 AND n>5000;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Hibernate 3.2.1 and database SQLServer2000 while I'm try to insert some data
I have the following table. When i try to insert records using LINQ to
I get the following error message when I try to INSERT some data: SQLSTATE[23000]:
I'm trying to insert some values on a data base, but it doesn't work
I have problem when I try insert some data to Informix TEXT column via
I'm trying to insert some data into a table from a csv document which
I tried to insert some data into my database (sql server/local file) but it
I'm using Nodejs to insert some data via MongoDB as so: var db =
I want to insert some session data in a database. I have one table
I try to bulk insert some datetime values in this format: 31/12/2005 00:00:00 using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.