问题描述
如何检索刚刚插入的行?
How can I retrieve the row just inserted?
INSERT INTO LETTRE_VOIT
select rsVoit.NOID, NO_ORDRE, rsOrdre.CODE_DEST, rsOrdre.MODAL_MODE, rsOrdre.MODAL_PORT,
CASE rsOrdre.MODAL_PORT
WHEN 'false' THEN 'D'
ELSE 'P'
END,
rsOrdre.LIVRS_EXPRS,
CASE rsOrdre.LIVRS_EXPRS
WHEN 'false' THEN 'L'
ELSE 'E'
END,
rsOrdre.ENLEV_UNITE, LIBELLE, NBR_COLIS,POID,ENLEV_CREMB,ENLEV_DECL
from ORDRE rsOrdre
inner join
(
select CODE_DEST,MODAL_MODE, MODAL_PORT, LIVRS_EXPRS,ENLEV_UNITE, ROW_NUMBER() over (order by CODE_DEST) as NOID
from ORDRE
group by CODE_DEST,MODAL_MODE,MODAL_PORT,LIVRS_EXPRS,ENLEV_UNITE
) rsVoit on rsVoit.CODE_DEST = rsOrdre.CODE_DEST and rsVoit.MODAL_MODE = rsOrdre.MODAL_MODE
and rsVoit.MODAL_PORT = rsOrdre.MODAL_PORT and rsVoit.LIVRS_EXPRS = rsOrdre.LIVRS_EXPRS
and rsVoit.ENLEV_UNITE = rsOrdre.ENLEV_UNITE
LEFT JOIN T_UNITE ON rsOrdre.ENLEV_UNITE = T_UNITE.NOID
WHERE (STATUT_ORDRE = 3) AND IS_PRINT = 'false' AND (TRANSPORTEUR IN (SELECT ParsedString From dbo.ParseStringList(@Trans)))
order by rsVoit.NOID, NO_ORDRE
SELECT * FROM LETTRE_VOIT WHERE ???
例如:
我插入 2,它返回 2
I inserted 2, it return 2
然后我插入了 3,我想返回 3 而不是 5 行.
and then I inserted 3, I want return 3 instead of 5 rows.
提前致谢.
史蒂夫
PS:我可能需要使用存储过程吗?
PS: May be I need to use a stored procedure ?
推荐答案
我不能 100% 确定你到底想要什么......但是 SQL Server 有一个 OUTPUT
子句可以输出INSERT
和 UPDATE
和 DELETE
语句中的内容:
I'm not 100% sure what exactly you want back.... but SQL Server has an OUTPUT
clause that can output stuff from INSERT
and UPDATE
and DELETE
statements:
INSERT INTO dbo.YourTable(col1, col2, ..., colN)
OUTPUT Inserted.Col1, Inserted.IDCol, Inserted.Col17
VALUES(val1, val2, ...., valN)
在这里,您要为 IDCol
(例如 INT IDENTITY 列)、Col1
和 Col17
插入值和插入值.
Here, you're inserting values and the inserted values for the IDCol
(e.g. an INT IDENTITY column), Col1
and Col17
.
如果只是将结果返回到 Mgmt Studio 中的网格就足够了 - 然后使用 OUTPUT
子句!阅读更多关于在线图书上的OUTPUT
子句
If just getting back the results into your grid in Mgmt Studio is good enough - then use the OUTPUT
clause! Read more about the OUTPUT
clause on Books Online
这篇关于选择刚刚插入的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!