Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add mssql's output clause support. #687

Closed
igalklebanov opened this issue Sep 9, 2023 · 1 comment · Fixed by #828
Closed

add mssql's output clause support. #687

igalklebanov opened this issue Sep 9, 2023 · 1 comment · Fixed by #828
Labels
api Related to library's API enhancement New feature or request greenlit Ready for implementation mssql Related to MS SQL Server (MSSQL)

Comments

@igalklebanov
Copy link
Member

igalklebanov commented Sep 9, 2023

Hey 👋

Now that mssql dialect is in the core, it's time to pick and choose which clauses and keywords should get support in Kysely's query API.

The OUTPUT clause, is pretty much returning clause, but with deleted and inserted (both are available in update queries so you could return new and old values) "tables" in context.

This one seems like an easy win. Without it, consumers will have to perform another query in inserts to get IDs.

@igalklebanov igalklebanov added enhancement New feature or request api Related to library's API mssql Related to MS SQL Server (MSSQL) labels Sep 9, 2023
@koskimas koskimas added the greenlit Ready for implementation label Sep 10, 2023
@gittgott gittgott mentioned this issue Oct 9, 2023
@movahhedi
Copy link
Contributor

I currently do it like this:

export async function GetInsertedIdIfUndefined(
	trx: IKyselyTrx,
	insertResult: InsertResult,
) {
	if (insertResult.insertId === undefined) {
		// return db.fn.scopeIdentity();
		const queryResult: any = await sql`SELECT @@IDENTITY as id`.execute(trx);
		// const queryResult: any = await sql`SELECT SCOPE_IDENTITY() as id`.execute(db);
		return queryResult.rows?.[0]?.id;
	}

	return insertResult.insertId;
}

P.S.: Should db.fn.scopeIdentity() be implemented too?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Related to library's API enhancement New feature or request greenlit Ready for implementation mssql Related to MS SQL Server (MSSQL)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants