Search code examples
t-sqlmenussmssubmenuright-click

Is there a right-click way to INSERT INTO SELECT in SSMS, or is the menu tweakable?


I'd like to know whether it is possible to do a insert into select statement with the use of right-click in SQL Server Manegement Studio (SSMS). And if not, is that tweakable?

When right-clicking on a table in the database (example here, I know I can right-click the target table, select 'Script Table as'/'Select to',paste the script,right-click the source-table, select 'Script Table as'/'Select to' and paste that in the just pasted piece of script instead of the VALUES-part. I want somehow to bypass these steps and possibly there is a simple way to do it already. But then I have to see 'Insert into select', or something. I'd like SSMS to generate for me a script something like this:

USE [DatabaseName]
GO

INSERT INTO [TableAA_New]
       ([A]
       ,[B]
       ,[C])

SELECT  [A]
        ,[B]
        ,[C]
  FROM [TableAA]
GO

So, my question is whether there is some right-click option that I don't see?

If there's not, is there some way of adding such a possibility to the right-click options? Can I tweak those? Actually a kind of extending the right-click menu in SSMS... Can anyone tell me or get me in the right direction?


Solution

  • Not 100% this is what you mean, But yes you can

    if you have

    SELECT * FROM MyTable
    

    Select it then right click it. You get a context menu with an option "Design Query in Editor..."

    This pops up a UI for building SQL. At the top of this window is a section where you can see the tables involved. If you right click on the white space in this top window you get another context menu with "Change type" this allows you to change your statement into an insert statement.

    If you select insert values then you can input values that you want to insert into the table (on the UI)

    But i expect you want "insert results". Selecting this means you then need to select the target table. You then need to map values from one table to another by updating the append column on the UI. If the target tables has the same columns as the source then the append column will be automatically updated to match the two columns.

    Once done you get

    INSERT INTO OtherTable
             (A,B,C)
    SELECT A,B,X
    FROM MyTable
    

    Hope this helps