Jackal
Jackal

Reputation: 3521

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. Also it has 2 rows, so i might not be sure if this only works for one

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

Upvotes: 0

Views: 57

Answers (3)

Ilyes
Ilyes

Reputation: 14926

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 NULLs and replace it with a default value instead of NULL since your column IS NOT NULL.

For the error you get, that because you just specify (IdArtigo) column, that mean you will insert a row have NULLs except for IdArtigo column which you specify in the INSERT clause. So you are trying to insert a NULL value to TipoDocumento column, cause you did not specify which value to insert it to it.

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

Upvotes: 2

fdkgfosfskjdlsjdlkfsf
fdkgfosfskjdlsjdlkfsf

Reputation: 3301

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:

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

Upvotes: 2

Jackal
Jackal

Reputation: 3521

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

Upvotes: 1

Related Questions