需要一些帮助来尝试通过一些简单的数据检查来执行简单的SQL INSERT [英] Need some help trying to do a simple SQL INSERT with some simple data checking

查看:41
本文介绍了需要一些帮助来尝试通过一些简单的数据检查来执行简单的SQL INSERT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表中有以下数据.

Name                LeftId    RightId
------------------------------------------
Cat                     1  
Cat                     2
Dog                     4
Dog                     5
Dog                               7
Dog                               69
Gerbil                  12        13 

我需要将它们插入到新表中,作为...

I need to insert them into a new table as...

Id         Name
-------------------------------
1          Cat
2          Cat
4          Dog
5          Dog
7          Dog
69         Dog
12         Gerbil    (or 13. don't care which number out of the two)

因此,字段LeftId和RightId可以包含NULL或NVARHCAR值(即使它们在上面被列为数字,..它们也始终是数字..只是导入将其创建为nvarchar(255).

So the fields LeftId and RightId can contain NULLS OR an NVARHCAR value (even though they are listed as numbers, above.. they will always be numbers .. just the import created the field as nvarchar(255).

如果需要,我可以修改源表字段.

I can modify the source table fields, if required.

任何人都可以帮忙吗?

我猜测insert语句中将有一个select查询,并使用CASE语句.不过,我不确定如何做到最好.

I'm guessing the insert statement will have a select query in it and use a CASE statement. I'm just not sure how to best do that, though.

干杯:)

推荐答案

您可以使用IsNull轻松做到这一点:

You can do this easily with IsNull:

INSERT INTO newtable (id,name)
SELECT IsNull(leftid,rightid), name
FROM oldtable

这篇关于需要一些帮助来尝试通过一些简单的数据检查来执行简单的SQL INSERT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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