SQL 选择刚刚插入的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8342425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select the rows that just inserted
提问by user609511
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 ???
For example:
例如:
I inserted 2, it return 2
我插入了 2,它返回 2
and then I inserted 3, I want return 3 instead of 5 rows.
然后我插入了 3,我想返回 3 而不是 5 行。
Thanks in advance.
提前致谢。
Stev
史蒂夫
PS: May be I need to use a stored procedure ?
PS:我可能需要使用存储过程吗?
回答by marc_s
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:
我不是你想知道究竟回100%......但SQL Server有一个OUTPUT
条款,从可输出的东西INSERT
和UPDATE
和DELETE
语句:
INSERT INTO dbo.YourTable(col1, col2, ..., colN)
OUTPUT Inserted.Col1, Inserted.IDCol, Inserted.Col17
VALUES(val1, val2, ...., valN)
Here, you're inserting values and the inserted values for the IDCol
(e.g. an INT IDENTITY column), Col1
and Col17
.
在这里,您要插入值和插入的值IDCol
(例如 INT IDENTITY 列)Col1
和Col17
。
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
如果在 Mgmt Studio 中将结果返回到您的网格就足够了 - 然后使用该OUTPUT
子句!阅读有关在线图书条款的更多信息OUTPUT
回答by subodh
If you are using SQL Server
如果您使用的是 SQL Server
And you know how many row inserted then go through
你知道插入了多少行然后通过
SELECT top 2 * FROM LETTRE_VOIT order by primaryKeyId desc
put the number of row inserted at place of 2
.
将插入的行数放在 的位置2
。
It may be help you, If you know the number of inserted rows, and then you can provide the numbers with top keyword
它可能对你有帮助,如果你知道插入的行数,那么你可以提供带有 top 关键字的数字
回答by coconut
How about saving the records in a variable before inserting them or adding a date field and retrieve them by date?
在插入记录或添加日期字段并按日期检索它们之前,如何将记录保存在变量中?