Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Macros in SQL Server Management Studio

Is there any way to implement text editing macros in SSMS? I would, e.g. like to convert the the code as shown below, but with a key-press, not a long-winded regex search and replace.

This:

INSERT INTO [TABLE]
           ([fieldOne]
           ,[fieldTwo])
     VALUES
           (<fieldOne, datetime,>
           ,<fieldTwo, real(24,0))

Must become this:

INSERT INTO [TABLE]
           ([fieldOne]
           ,[fieldTwo])
     VALUES
           (@fieldOne
           ,@fieldTwo)

I know SSMS doesn't natively support this, but I also know that it is extensible, if undocumented, and there is also room for a totally external application that will take copied text, transform it, and paste it back, without having to open an editor, paste, edit, copy, and paste back to SSMS.

Editing the stored templates is not an option, as these templates are dynamically generated, and using Ctrl+Shift+M is not an option either, as I still have to type each parameter name, but without the convenience of copying and pasting in the query editor.

There is no SSMS solution! I am looking for some sort of external voodoo that can help me do this.

like image 625
ProfK Avatar asked Mar 15 '09 10:03

ProfK


1 Answers

What about an AutoHotKey script?

Depending on the complexity of your templates, you could either

  1. use AutoHotKey to play back the keystrokes needed for the regex search and replace, or
  2. copy the template to the clipboard and manipulate it directly within AutoHotKey before pasting it back.

I'm sure the first option will work. I've not tried the second.

This question gives an indication of how an AutoHotKey script can be written to listen for keyboard chords.

like image 160
Ed Harper Avatar answered Oct 13 '22 07:10

Ed Harper