Skip to content

INSERT INTO ... SELECT create with .add()#301

Merged
brianc merged 1 commit into
brianc:masterfrom
danrzeppa:dan-mssqlinsertselect
Feb 25, 2016
Merged

INSERT INTO ... SELECT create with .add()#301
brianc merged 1 commit into
brianc:masterfrom
danrzeppa:dan-mssqlinsertselect

Conversation

@danrzeppa

Copy link
Copy Markdown
Collaborator

Before this pull request, this code

var query=post.insert(post.id)
var select=user.select(user.id)
query.add(select)

would generate (properly quoted)

INSERT INTO post (id) (SELECT user.id FROM user)

Notice the parentheses around the SELECT clause. In many cases, this isn't a problem, but in Microsoft SQL server, when you try to add an ORDER BY clause onto your select, it is a syntax error when there are parentheses around the SELECT statement.

This pull request makes the above query generate:

INSERT INTO post (id) SELECT user.id FROM user

which is exactly what is generated when you write the query like:

var query=post.insert(post.id).select(user.id).from(user)

So it actually generates more consistent SQL regardless of the way that the query was constructed.

@brianc

brianc commented Feb 25, 2016

Copy link
Copy Markdown
Owner

This is great! I'll get this merged & fix the jslint error too. 😄

brianc added a commit that referenced this pull request Feb 25, 2016
INSERT INTO ... SELECT create with .add()
@brianc brianc merged commit 39be3f3 into brianc:master Feb 25, 2016
@danrzeppa danrzeppa deleted the dan-mssqlinsertselect branch June 9, 2018 15:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants