0

テーブルを作成しました

create table AA(
    id int ,
    name varchar(20),
    sname varchar(20)
)

今、私は新しい値を挿入し、挿入された行IDを取得しています:

declare @InsID table (InId int)
insert into AA (name, sname)select 1, 'John', 'Rock'
output inserted.id into @output
Select * from @InsID

しかし、それは私にエラーを示しています: Msg 102, Level 15, State 1, Line 3 Incorrect syntax near 'inserted'. 何が問題と思われるか知っている人はいますか?

4

2 に答える 2

3
declare @InsID table (InId int)

insert into AA (name, sname)
output inserted.id into @InsID
select 'John', 'Rock'

Select * from @InsID
于 2012-04-25T10:27:38.080 に答える
1

テーブルの ID 列が ID 列として定義されていると仮定すると、次のようになります。

SELECT SCOPE_IDENTITY()

詳細については、この記事を参照してください: http://blog.sqlauthority.com/2007/03/25/sql-server-identity-vs-scope_identity-vs-ident_current-retrieve-last-inserted-identity-of-record/

本当に句を使用したい場合はOUTPUT、構文が間違っています。出力は、テーブルの直後、値の前に来る必要があります。

declare @InsID table (InId int)

insert into AA (name, sname)
output inserted.id into @InsID 
select 'John', 'Rock'

Select * from @InsID

それぞれの例はこちらをご覧ください

于 2012-04-25T10:21:52.403 に答える