[英]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.