添加列到表问题 [英] Add column to table problem

查看:76
本文介绍了添加列到表问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



如你所知,我是SQL的新手。我尝试使用while循环向列添加数据,但我无法实现它。我想向所有医生添加10-20-30等因素信息。它给出了一个错误,就像您尝试将空值插入主键一样。你能帮帮我吗?



这是我的代码;

DECLARE @N int;

SET @N = 1;

WHILE @@ FETCH_STATUS = 0

BEGIN

插入医生(因素)值(@ N * 10);

SET @N = @ N + 1;

END



谢谢..

Hi everyone,

As you understand i''m new in SQL. I try to add data to a column with while loop but i can not achieve it. I wanna add 10-20-30 etc. factor info to all doctors. It gives an error like you are trying to insert null value to primary key. Could you help me?

Here is my code;
DECLARE @N int;
SET @N=1;
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO Doctor (FACTOR) VALUES (@N*10);
SET @N = @N+1;
END

Thanks..

推荐答案

INSERT语句不向列添加数据 - 它向表中添加一行。

因此,当您执行它时,SQL会尝试创建一个仅使用您提供的数据的新行:FACTOR列的数据。不幸的是,它不能这样做,因为它想要主键列中的非空值,你没有指定。



我是不完全确定你想要实现什么,因为我不知道你的其余数据库定义,但可能是你想要使用UPDATE语句 - 但要注意,如果你使用它而不指定你想要的行要使用WHERE子句进行更改,它将更新表中每个行的列。
The INSERT statement does not "add data to a column" - it adds a row to the table.
So when you execute it, SQL tries to create a new row using just the data you have provided: the data for the FACTOR column. Unfortunately, it can''t do that, because it wants a non-null value in the Primary Key column, which you do not specify.

I''m not exactly sure what you are trying to achieve because I have no idea of the rest of you database definition, but it may be that you want to use the UPDATE statement instead - but be aware that if you use that without specifying which rows you want to change using the WHERE clause, it will update the column for every row in the table.


这篇关于添加列到表问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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