SQL脚本创建插入脚本 [英] SQL script to create insert script

查看:257
本文介绍了SQL脚本创建插入脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将解释一个模糊的标题。

A bit of a vague title, I will explain.

我正在编写一个SQL脚本来为数据库中表的每一行创建一个插入语句,纯粹是为了能够将这些数据重新应用到另一个数据库。

I am writing an SQL script to create an insert statement for each row of a table in my database, purely to be able to apply that data back to another database.

这是我目前所拥有的:

SELECT 'INSERT INTO products (id,name,description) VALUES ('||ID||','''||name||''','''||description||''');' FROM products

效果很好,输出如下:

INSERT INTO products (id,name,description) VALUES (1,'Lorem','Ipsum');
INSERT INTO products (id,name,description) VALUES (2,'Lorem','Ipsum');
INSERT INTO products (id,name,description) VALUES (3,'Lorem','Ipsum');
INSERT INTO products (id,name,description) VALUES (4,'Lorem','Ipsum');

问题是,如果其中一个字段为空,则该行将无法生成更新脚本,输出文件中该行只是空白。显然,由于有20多个字段,因此某些可选字段意味着几乎不会生成我的任何脚本。

The problem is if one of the fields is empty that row will fail to produce an update script, in the output file the line is just blank. Obviously as there are 20+ fields, some optional this means that hardly any of my scripts are generated.

有没有办法解决此问题?

Is there a way to solve this issue?

推荐答案

对于NULL字段,您可以执行以下操作

In the case of NULL fields you can do something like

Select COALESCE(Name, '') from...

凝聚函数返回第一个非空值。

The coalesce function returns the first nonnull value in the list.

对于真正的空白字段(例如空nvarchar),我相信您的脚本可以正常工作。

For truly blank fields (empty nvarchar for instance) I believe your script above will work.

这篇关于SQL脚本创建插入脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆