sql查询,将一个表中的一条记录中的行插入到同一个表中,只需稍加修改

时间:2022-01-18 15:42:29

I have an sql server table in which i have 30 columns

我有一个sql server表,其中有30列

I need to know if the following can be done or not

我需要知道是否可以做以下的事情

I need to write an sql qyery to copy records in the table into the same table with the 28 column's data to be the same and the rest 2 column's data changed.

我需要编写一个sql qyery,将表中的记录复制到相同的表中,28列的数据保持不变,其余2列的数据更改。

Can it be written in one sql query?

它可以在一个sql查询中编写吗?

Insert into table(30 cloumns) select 30 columns from table

插入表(30个cloumns),从表中选择30列。

The above statement inserts all the 30 column's data as same. But, I need first 2 column's data changed

上面的语句将30列的所有数据插入到相同的位置。但是,我需要改变前两列的数据

How do I do it?

我该怎么做呢?

1 个解决方案

#1


0  

Query table for which you want to copy using the where clause and select all columns and change the columns which you want to change.

您希望使用where子句复制的查询表,并选择所有列并更改希望更改的列。

Look at the [SEX_NO],[BLOOD_GROUP] column which are having new values.

查看[SEX_NO]、[BLOOD_GROUP]列,它们具有新的值。

INSERT INTO [dbo].[CONTACTS]
SELECT [CONTACT_ID]
      ,[CONTACT_TYPE_ID]
      ,[FIRST_NAME]
      ,[MIDDLE_NAME]
      ,[LAST_NAME]
      ,[FULL_NAME]
      ,[OTHER_NAME]
      ,'1' [SEX_NO] 
      ,[DOB]
      ,'O+' [BLOOD_GROUP]
      ,[BIRTH_PLACE]
      ,[HOME_ADDRESS1]
      ,[HOME_ADDRESS2]
      ,[HOME_TEL]
      ,[OFFICE_TEL]
      ,[DEGREE_NO]
      ,[EMAIL_ADDR1]
      ,[EMAIL_ADDR2]
      ,[MOBILE_NO1]
      ,[MOBILE_NO2]
      ,[FAX_NO]
      ,[MEMBER_ID]
      ,[IS_FAV]
  FROM    [dbo].[CONTACTS]
  where CONTACT_ID = 1

#1


0  

Query table for which you want to copy using the where clause and select all columns and change the columns which you want to change.

您希望使用where子句复制的查询表,并选择所有列并更改希望更改的列。

Look at the [SEX_NO],[BLOOD_GROUP] column which are having new values.

查看[SEX_NO]、[BLOOD_GROUP]列,它们具有新的值。

INSERT INTO [dbo].[CONTACTS]
SELECT [CONTACT_ID]
      ,[CONTACT_TYPE_ID]
      ,[FIRST_NAME]
      ,[MIDDLE_NAME]
      ,[LAST_NAME]
      ,[FULL_NAME]
      ,[OTHER_NAME]
      ,'1' [SEX_NO] 
      ,[DOB]
      ,'O+' [BLOOD_GROUP]
      ,[BIRTH_PLACE]
      ,[HOME_ADDRESS1]
      ,[HOME_ADDRESS2]
      ,[HOME_TEL]
      ,[OFFICE_TEL]
      ,[DEGREE_NO]
      ,[EMAIL_ADDR1]
      ,[EMAIL_ADDR2]
      ,[MOBILE_NO1]
      ,[MOBILE_NO2]
      ,[FAX_NO]
      ,[MEMBER_ID]
      ,[IS_FAV]
  FROM    [dbo].[CONTACTS]
  where CONTACT_ID = 1