INSERT VALUES 语句
在http://www.cnblogs.com/insus/articles/1921391.html这个例子,我们创建了一个表,现在来演示SQL Server的INSERT VALUES语法,
如果使用INSERT早期语法,插入十笔记录,
代码
INSERT INTO [dbo].[OrderDemo] ([Name]) VALUES ('Title1');
INSERT INTO [dbo].[OrderDemo] ([Name]) VALUES ('Title2');
INSERT INTO [dbo].[OrderDemo] ([Name]) VALUES ('Title3');
INSERT INTO [dbo].[OrderDemo] ([Name]) VALUES ('Title4');
INSERT INTO [dbo].[OrderDemo] ([Name]) VALUES ('Title5');
INSERT INTO [dbo].[OrderDemo] ([Name]) VALUES ('Title6');
INSERT INTO [dbo].[OrderDemo] ([Name]) VALUES ('Title7');
INSERT INTO [dbo].[OrderDemo] ([Name]) VALUES ('Title8');
INSERT INTO [dbo].[OrderDemo] ([Name]) VALUES ('Title9');
INSERT INTO [dbo].[OrderDemo] ([Name]) VALUES ('Title10');
INSERT INTO [dbo].[OrderDemo] ([Name]) VALUES ('Title2');
INSERT INTO [dbo].[OrderDemo] ([Name]) VALUES ('Title3');
INSERT INTO [dbo].[OrderDemo] ([Name]) VALUES ('Title4');
INSERT INTO [dbo].[OrderDemo] ([Name]) VALUES ('Title5');
INSERT INTO [dbo].[OrderDemo] ([Name]) VALUES ('Title6');
INSERT INTO [dbo].[OrderDemo] ([Name]) VALUES ('Title7');
INSERT INTO [dbo].[OrderDemo] ([Name]) VALUES ('Title8');
INSERT INTO [dbo].[OrderDemo] ([Name]) VALUES ('Title9');
INSERT INTO [dbo].[OrderDemo] ([Name]) VALUES ('Title10');
另外,SQL Server 2008 对VALUES子句进行了增强,允许在一中指定由逗号分隔开多行记录。
INSERT INTO [dbo].[OrderDemo] ([Name])
VALUES ('Title1'),('Title2'),('Title3'),('Title4'),('Title5'),('Title6'),('Title7'),('Title8'),('Title9'),('Title10');
VALUES ('Title1'),('Title2'),('Title3'),('Title4'),('Title5'),('Title6'),('Title7'),('Title8'),('Title9'),('Title10');
最后,由于插入值有一定的规律,可以使用WHILE循环插入。
DECLARE @key INT = 1
WHILE @key <= 10
BEGIN
INSERT INTO [dbo].[OrderDemo] ([Name]) VALUES ('Title' + CONVERT(NVARCHAR(10),@key))
SET @key = @key + 1
END
WHILE @key <= 10
BEGIN
INSERT INTO [dbo].[OrderDemo] ([Name]) VALUES ('Title' + CONVERT(NVARCHAR(10),@key))
SET @key = @key + 1
END
以前也增有演示过另外一个相似的例子,http://www.cnblogs.com/insus/articles/1914346.html