In this article we will discuss, How to create while loop SQL server We will be using Employee table. Below example we need to insert 1000 products to a item table using while loop.
Step 1: Create a table using the following script with data:
CREATE TABLE[dbo].[Item](
[ItemId] [int] NOTNULL,
[ItemName] [nvarchar](50) NOT NULL,
[Description] [nvarchar](50) NOT NULL,
CONSTRAINT[PK_Item] PRIMARY KEYCLUSTERED
(
[ItemId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Step 2: Create records using the following script with data.
DECLARE @ItemId int
SET @ItemId =1
WHILE(@ItemId <= 1000)
BEGIN
INSERT INTO Item VALUES
(@ItemId, 'Product - ' + CAST(@ItemId as nvarchar(20)),
'Product - ' + CAST(@ItemId as nvarchar(20)) + ' Description')
SET @ItemId = @ItemId + 1
END
Output:
Post your comments / questions
Recent Article
- How to fix HAXM is not installed |in Android Studio
- How to fix CMOS Checksum Error in Computer or Laptop | SOLVED
- Reactivating windows after a Hardware change on PC or Laptop
- FIXED: Windows reported that the hardware of your device has changed. Error code :0xc004F211
- "redirect" is not defined pylance("reportUndefinedVariable)
- This action cannot be completed because the file is open in SQL Server(SQLEXPRESS) - FIXED
- Unicode error 'unicodeescape' codec can't decode bytes in position 2-3: truncated UXXXXXXXX escape
- Could not find the 'angular-devkit/build-angular:dev-server' builder's node package | Angular Error
Related Article