需要在表的某些列中插入空值

时间:2019-04-29 17:21:44

标签: sql sql-server

我正在尝试将值插入表2中的表1中 表1中有某些列,我不希望表2中的值 相反,我想将NULL插入表1中的值。

下面是示例。

insert into dbo.Student
[Number], [Timestamp], [Education], [Roll No],
[Name], [Age],[marks])

Select
[Number], [Timestamp],    
convert(nvarchar(max),dbo.Table1([a.Description Description])) as [Education],
Convert(nvarchar(50),dbo.Table2([a.Description Description])) as [Roll No], 
convert(nvarchar(50),dbo.Table3([a.Description Description])) as [Name],    
dbo.Table4([a.Description Description]) as [Age],    
convert(nvarchar(50),dbo.Table5([a.Description Description])) as [marks]    
from dbo.Data a

我想将[Number],[Timestamp],[Education]列更新为NULL 我想从剩下的列中立即获取值。

请帮忙

4 个答案:

答案 0 :(得分:1)

您可以通过多种方式做到这一点:

提供NULL

insert into dbo.Student
    [Number], [Timestamp], [Education], [Roll No],
    [Name], [Age],[marks])
Select
    NULL as [Number], NULL as [Timestamp], NULL as [Education],    
    Convert(nvarchar(50),dbo.Table2([a.Description Description])) as [Roll No],    
    convert(nvarchar(50),dbo.Table3([a.Description Description])) as [Name],        
    dbo.Table4([a.Description Description]) as [Age],    
    convert(nvarchar(50),dbo.Table5([a.Description Description])) as [marks]
from dbo.Data a

避免在这些字段中插入数据

insert into dbo.Student
    [Roll No],
    [Name], [Age],[marks])
Select
    Convert(nvarchar(50),dbo.Table2([a.Description Description])) as [Roll No],    
    convert(nvarchar(50),dbo.Table3([a.Description Description])) as [Name],        
    dbo.Table4([a.Description Description]) as [Age],    
    convert(nvarchar(50),dbo.Table5([a.Description Description])) as [marks]
from dbo.Data a

插入后更改数据

执行insert into dbo.Student ...后,请执行以下操作:

update dbo.student set 
   [Number] = NULL,
   [Timestamp] = NULL,
   [Education] = NULL
where ...

您必须小心where子句,并确保仅更改最近输入的记录。

答案 1 :(得分:0)

您只需要在列中放置NULL。

insert into dbo.Student
    [Number], [Timestamp], [Education], [Roll No],
    [Name], [Age],[marks])
Select
    NULL AS [Number], NULL AS [Timestamp],      
    NULL as [Education],    
    Convert(nvarchar(50),dbo.Table2([a.Description Description])) as [Roll No],    
    convert(nvarchar(50),dbo.Table3([a.Description Description])) as [Name],        
    dbo.Table4([a.Description Description]) as [Age],    
    convert(nvarchar(50),dbo.Table5([a.Description Description])) as [marks]
from dbo.Data a

答案 2 :(得分:0)

您不能只包含不想包含值的列。因此从您的示例中可以看到:

insert into dbo.Student

[Roll No], [Name], [Age],[marks])

Select

Convert(nvarchar(50),dbo.Table2([a.Description Description])) as [Roll No],

convert(nvarchar(50),dbo.Table3([a.Description Description])) as [Name],

dbo.Table4([a.Description Description]) as [Age],

convert(nvarchar(50),dbo.Table5([a.Description Description])) as [marks]

from dbo.Data a

以下是几个相关的问题:

how to insert data in only few column leaving other columns empty or as they are in a mysql table record?

Adding only one value to the table in sql

答案 3 :(得分:0)

您可以简单地从“插入和选择”部分避免这三列,如下所示-

INSERT INTO dbo.Student
--[Number], [Timestamp], [Education], 
([Roll No],[Name], [Age],[marks])
SELECT
--[Number], [Timestamp],      
--convert(nvarchar(max),dbo.Table1([a.Description Description])) as [Education],    
CONVERT(NVARCHAR(50),dbo.Table2([a.Description Description])) AS [Roll No],    
CONVERT(NVARCHAR(50),dbo.Table3([a.Description Description])) AS [Name],        
dbo.Table4([a.Description Description]) AS [Age],    
CONVERT(NVARCHAR(50),dbo.Table5([a.Description Description])) AS [marks]
FROM dbo.Data a