简体   繁体   English

从表中复制一行并在SQL-SERVER中插入一个新值

[英]Copy a row from a table and insert a new value in SQL-SERVER

I have a problem with this query, I want to copy some columns and add a new value into the same table but doesn't seem to work right. 我对此查询有疑问,我想复制一些列并将新值添加到同一表中,但似乎无法正常工作。 I can't figure out what is wrong 我不知道怎么了

GO
IF OBJECT_ID('dbo.spInsertCopyDocumentos') IS NOT NULL
DROP PROCEDURE spInsertCopyDocumentos
GO
CREATE PROCEDURE spInsertCopyDocumentos
    @IdArtigo int,
    @IdNovo int
AS
BEGIN
    SET NOCOUNT ON

    INSERT INTO hDocumentos (IdArtigo) VALUES (@IdNovo)
    SELECT TipoDocumento, NomeDocumento,Dados, Extensao, Observacoes 
    FROM hDocumentos
    WHERE IdArtigo = @IdArtigo
END

It says the TipoDocumento is null but it has values and is an integer... I'm not sure why it says null because if i run the select query it shows the values. 它说TipoDocumento为null,但它有值并且是整数...我不确定为什么它说null,因为如果我运行select查询,它将显示值。 Also it has 2 rows, so i might not be sure if this only works for one 它也有2行,所以我可能不确定这是否仅适用于一个

EDIT. 编辑。

I'm copying files from a different record ID hence why i Need @IdNovo here to insert the new value in the foreign key ID column 我正在从不同的记录ID复制文件,因此,为什么我需要@IdNovo在此处在外键ID列中插入新值

Are you just looking for 您只是在寻找

IF OBJECT_ID('dbo.spInsertCopyDocumentos') IS NOT NULL
DROP PROCEDURE spInsertCopyDocumentos
GO
CREATE PROCEDURE spInsertCopyDocumentos
    @IdArtigo int,
    @IdNovo int
AS
BEGIN
    SET NOCOUNT ON
    -- You can check the params for NULL here and raise an error
    INSERT INTO hDocumentos (IdArtigo) 
    SELECT @IdNovo 
    FROM hDocumentos
    WHERE IdArtigo = @IdArtigo
END

If you want to copy all the data then you need to specify the columns names as 如果要复制所有数据,则需要将列名称指定为

INSERT INTO hDocumentos (IdArtigo, Col1, Col2, ..)
SELECT @IdNovo, ISNULL(Col1, <DefaultValue>), ISNULL(Col2, <DefaultValue>) ...
FROM hDocumentos
WHERE IdArtigo = @IdArtigo

and use ISNULL() to check for NULL s and replace it with a default value instead of NULL since your column IS NOT NULL . 并使用ISNULL()检查NULL并将其替换为默认值而不是NULL因为您的列为IS NOT NULL

For the error you get, that because you just specify (IdArtigo) column, that mean you will insert a row have NULL s except for IdArtigo column which you specify in the INSERT clause. 对于错误,您得到的原因是,您仅指定(IdArtigo)列,这意味着您将插入具有NULL的行,而在INSERT子句中指定的IdArtigo列除外。 So you are trying to insert a NULL value to TipoDocumento column, cause you did not specify which value to insert it to it. 因此,您试图在TipoDocumento列中插入NULL值,原因是未指定将哪个值插入其中。

Finally, your procedure should looks like: 最后,您的过程应如下所示:

IF OBJECT_ID('dbo.spInsertCopyDocumentos') IS NOT NULL
DROP PROCEDURE spInsertCopyDocumentos
GO
CREATE PROCEDURE spInsertCopyDocumentos
    @IdArtigo int,
    @IdNovo int
AS
BEGIN
    SET NOCOUNT ON
    -- Optional if you need to check the paramaters if they NULL or not, that's up to you
    INSERT INTO hDocumentos (IdArtigo, 
                             TipoDocumento, 
                             NomeDocumento, 
                             Dados, 
                             Extensao, 
                             Observacoes) --The number of columns specified her must be the same in the select
    SELECT @IdNovo,
           ISNULL(TipoDocumento, <DefaultValue>), -- since it won't accept nulls
           --But since you are selecting from the same table it won't be a problem 
           NomeDocumento,
           Dados, 
           Extensao, 
           Observacoes 
    FROM hDocumentos
    WHERE IdArtigo = @IdArtigo
END

I'm not sure what hDocumentos looks like, but if you want to insert @IdNovo and the rows from the select it would look something like this: 我不确定hDocumentos是什么样子,但是如果您要插入@IdNovo 并且 select的行看起来像这样:

ALTER PROCEDURE spInsertCopyDocumentos
    @IdArtigo int,
    @IdNovo int
AS
BEGIN
    SET NOCOUNT ON

    INSERT INTO hDocumentos (col1, col2, col3, col4, col5, col6)
    SELECT TipoDocumento, NomeDocumento,Dados, Extensao, Observacoes, @IdNovo
    FROM hDocumentos
    WHERE IdArtigo = @IdArtigo
END

ok i see how it works. 好的,我知道它是如何工作的。 I had to specify the column names on the insert like a normal insert into query 我必须像一般查询插入一样在插入上指定列名

INSERT INTO hDocumentos (TipoDocumento, NomeDocumento, Dados, Extensao, Observacoes,IdArtigo) 
SELECT TipoDocumento, NomeDocumento, Dados, Extensao, Observacoes, @IdNovo
FROM hDocumentos
WHERE IdArtigo = @IdArtigo

暂无
暂无

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

相关问题 Sql Server如果dest表中不存在1个值,则插入新行 - Sql Server Insert a new row if 1 value does not exist in dest table SQL-SERVER 程序如何在一个表中插入行,然后将行插入到另一个表中作为外键链接到另一个表 - SQL-SERVER Procedure How do i insert row one table then insert row into another table link to another table as foreign key Sql Server触发器将值从新行插入另一个表 - Sql Server trigger insert values from new row into another table 使用 SQL-Server 临时表并将唯一 ID 插入到另一个表中 - Temp table and insert the unique Ids into another table, with SQL-Server 如何将表中列的每个值传递到存储过程并将结果保存到SQL-Server中的表中 - How to pass each value of the column from a table to a stored procedure and save the result in a table in SQL-Server 将新行插入表中,但从表中的另一行复制数据 - Insert new rows into table but copy data from another row in the table 使用表1的引用从表2中获取数据[sql-server] - obtain data from table 2 using reference of table 1 [sql-server] SQL Server:如何从临时表中插入多行数据,并在临时表中存储插入的行的ID - SQL-Server: How do I insert multiple rows with data from a temp table + store the ID of the inserted rows in the temp table 从Excel文件填充SQL-SERVER表 - Fill SQL-SERVER table from an excel file 将数据从txt文件导入到SQL-Server表的功能 - Function to import data from a txt file to a SQL-Server table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM