Macros in SQL Server Management Studio - sql-server

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.

What about an AutoHotKey script?
Depending on the complexity of your templates, you could either
use AutoHotKey to play back the keystrokes
needed for the regex search and
replace, or
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.

If you are using SSMS 2005 upwards it has in built support for templates. It isn't exactly full blown macro's, but non the less it is still pretty useful.
The syntax is exactly as you have shown in your first code snippet and you simply press Ctrl+Shift+M to bring up a dialog box that prompts you for the values to go into your bits enclosed in angle brackets.
SQL server generates script in this format if you right click on a table and select "Script Table as" then pick either the insert, update or delete option.
You can also create your own custom templates, or modify one of the existing built in ones (click on View -> Template explorer to get access to the other inbuilt templates).
There is a short article on MSDN that explains how to get started with templates.

Was looking for something else, but found this question. If you're still looking for something, try SSMS toolpack: http://www.ssmstoolspack.com/ It has macros and a bunch of other neat things. And it's free!

For SSMS 2016 you can use my Visual Commander extension. It supports macros recording/playback and custom C#/VB commands for editor text manipulations.

First code is template from old-good MS Query Analyzer. Shortcut to filling is Ctrl+M (but I'm not sure, maybe it is Ctrl+Shift+M).
There should be same feature in newer SSMS.

I usually copy the code into another editor (Notepad++, or Delphi /RAD Studio editor), do my macro stuff, and then paste it back into SSMS.

Related

Why are tagged expressions not found in SQL Server Management Studio?

I understand from my reading that curly brackets denote tagged expression in a "find" in SQL Server Management Studio (SSMS) with "use regular expressions" toggled on. And that backslash n is a placeholder for that matched text in replace. Even before I get to the replace I cannot get the find to work. For instance with this text:
this that
and this "find" regex pattern:
{[a-z]*}
I get "the following specified text was not found". If I remove the curly brackets the find gets a hit on each of those two words as expected. What am I doing wrong? This is SSMS v18.5.
The problem is that you are trying to use an obsolete syntax (i.e. "tagged expressions") that went away with the older builds of SSMS. I'm not sure when it switched over, but I have a test system with SQL Server 2008 R2 on it, along with the version of SSMS that came with it, and it uses the old style SSMS Regular Expression syntax (which I think is proprietary as I haven't ever seen it used anywhere else).
SSMS now uses .NET Regular Expressions and its syntax. You can tell which type you have by making sure that the cursor is in the query editor and hitting CTRL + F. If the search box (attached to the upper, right-hand corner of the query editor) pops up (and the "Find" input field is not labeled), then you are using a modern version of SSMS along with .NET Regular Expressions. If, instead, a "Find and Replace" window pops up (and the input field is labeled as "Find what:"), then you are using an older version of SSMS along with its "special" version of Regular Expressions.
Example
Starting with:
this that
one two three four five
Using (older syntax — tagged expressions):
Find what: {[a-z]+} {[a-z]+}
Replace with: ~\2~\1~
results in:
~that~this~
~two~one~ ~four~three~ five
For newer versions of SSMS, using (newer syntax — capture groups):
Find: ([a-z]+) ([a-z]+)
Replace: ~$2~$1~
with the same starting text produces the exact same result.
Info
In the older versions, there is an arrow button to the right of the "Find" / "Replace" fields that will display a quick list of commonly used syntax, the bottom option being a link to the documentation.
In the newer versions, the "Find" and "Replace" (technically they are actually "Quick Find" and "Quick Replace") dialog does not have any help button. However, if you go to the "Edit" menu, then go to "Find and Replace   ➧", you will see options for "Find in Files" and "Replace in Files". These two options will open a window that is basically the "Find and Replace" window from the older versions, slightly updated, though still titled as "Find and Replace". This "Find and Replace" window has the help buttons on the right of the "Find" and "Replace" fields, both of which have an option for "Regular Expression Help" which takes you to the correct RegEx documentation (linked below).
Older SSMS RegEx documentation
Newer SSMS/Visual Studio RegEx documentation

Is it possible to insert unicode strings with input VB6 control's in a SQL Database?

I am trying to Insert Unicode strings into a table using the Internal vb6 controls(text,List,ect..) .
But when I try to do that , VB6 Control's convert that string , and different string is stored.
Am I forgetting something or is it a visual studio issue?
I found a reference to a software package(3rd-party controls) :
http://www.cyberactivex.com/UniSuiteFree.htm
I used This package and it done , but would also be curious to hear feedback from anyone who has used these or other ones :)
Original VB6 forms components did not do a good job with Unicode, although once you get Unicode strings into VB6 code you are mostly ok. You need to use the 'B' forms of string operators, like midb(), lenb() etc if you are doing any string maniputaion. You also may want to check your SQL parameter declarations to ensure you are using the unicode options. Otherwise it is all do-able.
Try the advise at this MS Support article.
Edit: After prompting from #Bob I read the MS Support article I linked to in more detail and take the point that forms2 is not a great bet for VB6 forms unless you have a more-than-usual level of control over the target machines.
In my own case I used a commercial component named Unitools from Woodbridge Associates but I cannot find their website today. Unitools included Unicode-aware label, text box, combo and list controls. Anyone able to provide a link ?

KDevelop: transform selected text?

I was wondering whether there was a function in KDevelop similar to the one that we used to have in Quanta (miss you, Quanta, btw).
I would like to set a keyboard shortcut, and apply a text transformation upon hitting it. Example:
I select my text to be translated, and I get back: <?=_('my text to be translated'?>
As you can see, this saves me a lot of awkward keystrokes...
I only found this feature in PHPStorm (where it is called "Live Templates"). I defined it like this: <?=t('$SELECTION$')?> and I can call it by hitting CTRL+ALT+J, and select it from the dropdown that appears.
You should be able to do that using a snippet with the following text: <?=_('%{selection}'?>
And you can bind a Shortcut to a snippet.
However my tests showed that this doesn't work correctly - there seems to be a bug somewhere.
As alternative you can write a script that does insert the text. See the documentation.

how to determine which profiles are assigned to a record type

I have 90 profiles,and a bunch of record types. Business wants to know who has access to a current record type (picklists..)
the only way i know how to do this, is to go profile by profile and check the Record Type Settings and see if it's listed.
is there a way to just generate a list or something? either through the point and click or apex?
EDIT:
I tried doing a search on the .profiles but eclipse doesn't support multi-line searches. Though it does support regexes i can't get my regular expression right, OR eclipse doesn't like me. Given my poor programming skills (and how I yell at my computer) it might be the latter. This is the string I want to search on.
<recordType>Event.RTEvent</recordType>
<visible>false</visible>
For something like this, I find that Notepad++ is a huge help. It supports multi-line searches if you enable the extended search mode (a simple radio button at the bottom of the Find menu), allowing you to grab line breaks (\r, \n, etc.). Pretty simple to do this across all of your profiles as well by navigating to the profiles folder of the Eclipse project in Windows Explorer, selecting all files, then right-click and select "Edit with Notepad++". Once they're all open, hit ctrl-F, enable the extended search mode, and search for something like this:
Event.RTEvent</recordType>\n <visible>false
Note that you may need to adjust the number of spaces after the \n depending on how far the data is indented. Click "Find All in All Opened Documents", and it'll give you a list of all occurrences of that string in your profiles files.

File Path Control

How to put a File path control in VBA front panel? I want the user to be able to select the browse button and select the file path rather than putting up dialog boxes all over the place. I need the user to select three or more file paths.
After re-re-reading your Q, it seams you want to steer away from dialog boxes!Oh well, I was going to say
I could post the hack about using MSDIAG on VBA, that explains
how you can patch your registry to
enable its use under VBA,
without having other MS-VB products
installed... but I rather have you
google that one... you can certainly
understand why.
But you don't want Dialog Boxes... you want controls and buttons: Use listboxes!
To populate your listbox, use the Dir command (using method additem of the listbox).
Two phases for achieving that:
first get the Directories (and prefix a "->" or whatever prior to adding it on the listbox, so that the user understands this is not a file);
then get filenames (you can filter by extension with the arguments of Dir, just as you would in DOS).
Finally, under OnClick and OnDoubleClick of the listbox, you must interpret the listbox default property (Item), check for "->" and use ChDir to change directory and repopulate, or you'll have your file selected.
The write up is sooooooo much more complicated than the code... trust me.
Do you mean VBA for Microsoft Office or just general VBA?
In Office, Application.FileDialog(msoFileDialogOpen).
Otherwise, look at the Win32 API function SHBrowseForFolder (in shell32.dll). You can import it for use into VBA using the Declare Function keywords.
There is not direct VBA function for that. You can decide to combine a form (Access form, or a generic microsoft form) with 2 controls: (1) text box (2) browse button (which will finally use the fileDialog command or a windows API).
Perhaps the browse for folder API from the Microsoft MVPs site would suit:
http://www.mvps.org/access/api/api0002.htm
It uses SHBrowseForFolder mentioned by fwzgekg, and does not return a file dialog, it returns a browsable list of folders.
Is this what you want?
FilePath = Application.GetOpenFilename

Resources