使用 SQL Server 2000 插入多个值时出现问题

发布于 2024-10-24 04:32:31 字数 286 浏览 1 评论 0原文

我有以下插入语句,该语句在运行 SQL Server 2008 的系统上运行良好,但我在运行 SQL Server 2000 的系统上尝试了同样的操作,但它给出了错误...

是否有一个简单的解决方案,因为我有超过 3000 条记录插入,我不想一次只做一个!

INSERT INTO uk_postcodes (outcode, lat, lng) 
VALUES ('AB12', '57.098381', '-2.172400'),('AB13', '57.108', '-2.237')

I have the following insert statement which works fine on one system running SQL Server 2008 but I've tried the same thing on a system running 2000 and it gives an error...

Is there an easy fix for this since I have over 3000 records to insert and I don't want to have to do them all one at a time!

INSERT INTO uk_postcodes (outcode, lat, lng) 
VALUES ('AB12', '57.098381', '-2.172400'),('AB13', '57.108', '-2.237')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

千仐 2024-10-31 04:32:31

使用 SQL Server 2000,有两种方法可以做到这一点 -

一种是

INSERT INTO uk_postcodes (outcode, lat, lng) 
VALUES ('AB12', '57.098381', '-2.172400');
INSERT INTO uk_postcodes (outcode, lat, lng) 
VALUES ('AB13', '57.108', '-2.237');

第二种方法是使用 UNION ALL -

INSERT INTO uk_postcodes (outcode, lat, lng) 
SELECT 'AB12', '57.098381', '-2.172400'
UNION ALL 
SELECT 'AB13', '57.108', '-2.237'

Using SQL server 2000, there are two ways to do this -

one is

INSERT INTO uk_postcodes (outcode, lat, lng) 
VALUES ('AB12', '57.098381', '-2.172400');
INSERT INTO uk_postcodes (outcode, lat, lng) 
VALUES ('AB13', '57.108', '-2.237');

Second way is using UNION ALL-

INSERT INTO uk_postcodes (outcode, lat, lng) 
SELECT 'AB12', '57.098381', '-2.172400'
UNION ALL 
SELECT 'AB13', '57.108', '-2.237'
不再让梦枯萎 2024-10-31 04:32:31

通过一些字符串替换,您将能够将脚本转换为以下应该可以工作的内容:

INSERT INTO uk_postcodes (outcode, lat, lng) 
select 'AB12', '57.098381', '-2.172400' union select 'AB13', '57.108', '-2.237'

With a bit of string replacing you will be able to convert the script into the following which should work:

INSERT INTO uk_postcodes (outcode, lat, lng) 
select 'AB12', '57.098381', '-2.172400' union select 'AB13', '57.108', '-2.237'
书信已泛黄 2024-10-31 04:32:31

这两个答案都会起作用。欲了解更多信息:

http://en.wikipedia.org/wiki/Insert_(SQL) )
ANSI SQL-92 添加了多记录插入语法,使用逗号分隔 VALUES,这正是您尝试使用的。特别是对于 SQL-Server,自 2008 版本以来添加了对此语法的支持

为了速度,过长的字符串会出现一些问题,因此使用该

SELECT .. UNION
SELECT ..

表单,但在大约 100 或 1000 条记录后中断并开始另一个 INSERT。

* 表值构造函数不仅限于 INSERT 语句。 SQL Server 2008 更进一步,允许定义<派生表>,例如

SELECT MAX(Amount)
FROM (
     VALUES (1),(2),(192),(99)
) Tbl(Amount)

Both answers give will work. For more information:

http://en.wikipedia.org/wiki/Insert_(SQL)
ANSI SQL-92 added the multi-records insert syntax, separating VALUES using comma, which is what you are trying to use. For SQL-Server specifically, support for this syntax was added since version 2008.

For speed, there are some issues with overly long strings, so use the

SELECT .. UNION
SELECT ..

form, but break and start another INSERT after about 100 or 1000 records.

* table value constructors are not limited to the INSERT statement only. SQL Server 2008 goes one further to allow it to define a <derived table>, e.g.

SELECT MAX(Amount)
FROM (
     VALUES (1),(2),(192),(99)
) Tbl(Amount)
橘香 2024-10-31 04:32:31

假设您的所有数据都是这样,请尝试将所有 "),(" 替换为 ");INSERT INTO uk_postcodes (outcode, lat, lng) VALUES ("

Assuming that you all your data is like that try replacing all "),(" with ");INSERT INTO uk_postcodes (outcode, lat, lng) VALUES ("

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文