Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 插入到导致列中为空的_Sql_Sql Server_Tsql - Fatal编程技术网

Sql 插入到导致列中为空的

Sql 插入到导致列中为空的,sql,sql-server,tsql,Sql,Sql Server,Tsql,我试图在SQL Server 2014中连接两列,并在它们之间添加逗号和空格 以下是我正在使用的代码(出于隐私原因,编辑表名) 这是正确地连接字符串,但在此过程中,会导致所有其他列和所有其他行读取为NULL,并从表中删除数据 大约有30列已经包含数据 有什么想法吗?如果您希望使用此函数添加一个新列CONCAT(MailingLine1',',',,MailingState),这样您就可以使用计算列了 --if [EnityName] already exists in the table yo

我试图在SQL Server 2014中连接两列,并在它们之间添加逗号和空格

以下是我正在使用的代码(出于隐私原因,编辑表名)

这是正确地连接字符串,但在此过程中,会导致所有其他列和所有其他行读取为NULL,并从表中删除数据

大约有30列已经包含数据


有什么想法吗?

如果您希望使用此函数添加一个新列
CONCAT(MailingLine1',',',,MailingState)
,这样您就可以使用计算列了

--if [EnityName]  already exists in the table you have to drop it first
alter table TABLE
drop column [EntityName]

alter table TABLE
add [EntityName] as CONCAT(MailingLine1, ', ', MailingState)
如果不想添加计算列,可以按照@GordonLinoff-answer(使用UPDATE命令)进行操作


否则,如果您希望插入新行而不是更新行,问题是您只在
EntityName
列中插入值,而忽略所有其他列,则可以按以下方式分配值(假设有3列)


其中
Value1、Value2
可以是表中的列,也可以是固定值,即
'John'
为其他卷设置默认值,即非空值。(见示例)


我猜您想要的是
更新
,而不是
插入

update Table
    set EntityName = CONCAT(MailingLine1, ', ', MailingState);

嗯……是的,您在insert中没有给其他列任何值,所以它将默认值(在本例中为
NULL
惰性赋值到表(EntityName)值(“值1”)
您能显示您的模式和示例数据吗?@JeanPaul98不,这不是问题。Op需要给他的表的其他列赋值:
INSERT-INTO-dbo.table(Column1,Column2,Column3)从dbo中选择SomeExpression1,SomeExpression2,SomeExpression3.Table1
@Hadi,或者Op想要
更新
@Lamak a ok现在检查一下大约有30个columns@LexJamesRussell你必须把它们都列出来,您可以使用脚本作为函数快速生成查询只要右键单击表格->脚本作为->插入到@LexJamesRussell,因为您是stackoverflow的新手,我认为最好访问以了解有关此网站的更多信息并获得您的第一个“通知”徽章。同意。因为OP是从同一个表名中提取的。
Insert into Table (EntityName,EntityName2,EntityName3)

SELECT CONCAT(MailingLine1, ', ', MailingState),Value1,Value2 FROM Table
ALTER TABLE {table_name} ADD DEFAULT {dafault_value} FOR {column_name}
update Table
    set EntityName = CONCAT(MailingLine1, ', ', MailingState);