简体   繁体   English

有没有办法在临时表中插入相同的行值? SQL服务器

[英]Is there a way to insert the same row values in a temp table? SQL Server

I have a temp table #t that has a column already called ID with about 75 values.我有一个临时表#t,其中有一列已经称为 ID,其中包含大约 75 个值。 I've inserted another column called status and I want all of the values in the 'status' column to equal "A".我插入了另一个名为 status 的列,我希望“status”列中的所有值都等于“A”。 Is there a way I can do this without having to manually insert A for each row?有没有一种方法可以做到这一点,而不必为每一行手动插入 A?

Would want it to look like this but for all 75 rows希望它看起来像这样,但对于所有 75 行

|ID|   |Status|
----------------
|24|   |  A   |

Not sure if I understand your question correctly but you could do it with something like不确定我是否正确理解了您的问题,但您可以使用类似

update #t set Status = 'A'

You can do that by setting the default value for status column.您可以通过设置状态列的默认值来做到这一点。

Create table #test(id int, status varchar(1) default 'A');
insert into #test (id) values (1),(2),(3);

Select * From #test;

id  status
1   A
2   A
3   A

If your table is already created you may set the default value as the following:如果您的表已经创建,您可以将默认值设置如下:

ALTER TABLE #test ADD CONSTRAINT df_val DEFAULT 'A' FOR status;

See a demo from db<>fiddle .查看db<>fiddle的演示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用@temp表变量插入SQL Server - Insert into SQL Server using @temp table variable 如何插入SQL Server中的现有临时表 - How to insert into an existing temp table in SQL Server SQL Server:有没有办法同时使用SELECT和VALUES插入表值? - SQL Server : is there a way to insert into a table values using both SELECT and VALUES at the same time? 当每行的一个值都相同时,是否有一种快速的方法可以将多行插入到 SQL Server 中 - Is there a quick way to insert multiple rows into SQL Server when one of the values is going to be the same for each row SQL Server-遍历临时表并计算多个值以将行插入第二个表 - SQL Server - loop through temp table and calculate multiple values to insert rows into second table 是否有自动方法在SQL Server中创建临时表 - Is there an automated way to create a temp table in SQL Server 使用C#在SQL Server上的临时表中插入3万行的最快方法 - Fastest way to insert 30 thousand rows in a temp table on SQL Server with C# SQL 如何根据相同的行值从另一个表插入? - SQL how to insert from another table based on same row values? SQL Server:同一表中具有相同行值的两列 - SQL Server : two columns with the same row values in the same table SQL SERVER为现有表的每一行插入新列值 - SQL SERVER Insert New Column Values for every row of existing table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM